Example usage for android.database.sqlite SQLiteStatement bindDouble

List of usage examples for android.database.sqlite SQLiteStatement bindDouble

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteStatement bindDouble.

Prototype

public void bindDouble(int index, double value) 

Source Link

Document

Bind a double value to this statement.

Usage

From source file:Main.java

public static void bindValores(SQLiteStatement statement, ContentValues contentValues) {
    Set<String> chaves = new TreeSet<>(contentValues.keySet());
    int index = 1;

    for (String chave : chaves) {
        Object valor = contentValues.get(chave);

        if (valor == null) {
            statement.bindNull(index);//from   ww  w  .jav  a 2s  .c  om

        } else if (valor instanceof String) {
            statement.bindString(index, (String) valor);

        } else if (valor instanceof Double || valor instanceof Float) {
            statement.bindDouble(index, Double.valueOf(String.valueOf(valor)));

        } else if (valor instanceof Integer || valor instanceof Long) {
            statement.bindLong(index, Long.valueOf(String.valueOf(valor)));
        } else if (valor instanceof byte[]) {
            statement.bindBlob(index, (byte[]) valor);
        }
        index++;
    }
}

From source file:com.denimgroup.android.training.pandemobium.stocktrader.ManageTipsActivity.java

private void doSaveTip() {
    String symbol = etSymbol.getText().toString();
    Double targetPrice = Double.parseDouble(etTargetPrice.getText().toString());
    String reason = etReason.getText().toString();

    //   TOFIX - Read the username from the credentials.properties file

    String sql = "INSERT INTO tip (tip_creator, symbol, target_price, reason) VALUES (?, ?, ?, ?)";

    StockDatabase dbHelper = new StockDatabase(this.getApplicationContext());
    SQLiteDatabase db = dbHelper.openDatabase();
    SQLiteStatement stmt = db.compileStatement(sql);
    stmt.bindString(1, "USERNAME");
    stmt.bindString(2, symbol);/*  www  .j a  v a2  s  .  c o m*/
    stmt.bindDouble(3, targetPrice);
    stmt.bindString(4, reason);
    stmt.execute();
    stmt.close();

    db.close();

    tvTipStatus.setText("Tip saved!");
}

From source file:com.digicorp.plugin.sqlitePlugin.SQLitePlugin.java

/**
 * Executes a batch request and sends the results via sendJavascriptCB().
 *
 * @param dbname//from   w  w  w.java  2  s  . c o m
 *            The name of the database.
 *
 * @param queryarr
 *            Array of query strings
 *
 * @param jsonparams
 *            Array of JSON query parameters
 *
 * @param queryIDs
 *            Array of query ids
 *
 * @param tx_id
 *            Transaction id
 *
 */
private void executeSqlBatch(String dbname, String[] queryarr, JSONArray[] jsonparams, String[] queryIDs,
        String tx_id) {
    SQLiteDatabase mydb = this.getDatabase(dbname);

    if (mydb == null)
        return;

    try {
        mydb.beginTransaction();

        String query = "";
        String query_id = "";
        int len = queryarr.length;

        for (int i = 0; i < len; i++) {
            query = queryarr[i];
            query_id = queryIDs[i];
            if (query.toLowerCase().startsWith("insert") && jsonparams != null) {
                SQLiteStatement myStatement = mydb.compileStatement(query);
                for (int j = 0; j < jsonparams[i].length(); j++) {
                    if (jsonparams[i].get(j) instanceof Float || jsonparams[i].get(j) instanceof Double) {
                        myStatement.bindDouble(j + 1, jsonparams[i].getDouble(j));
                    } else if (jsonparams[i].get(j) instanceof Number) {
                        myStatement.bindLong(j + 1, jsonparams[i].getLong(j));
                    } else if (jsonparams[i].isNull(j)) {
                        myStatement.bindNull(j + 1);
                    } else {
                        myStatement.bindString(j + 1, jsonparams[i].getString(j));
                    }
                }
                long insertId = myStatement.executeInsert();

                String result = "{'insertId':'" + insertId + "'}";
                this.sendJavascriptCB("window.SQLitePluginTransactionCB.queryCompleteCallback('" + tx_id + "','"
                        + query_id + "', " + result + ");");
            } else {
                String[] params = null;

                if (jsonparams != null) {
                    params = new String[jsonparams[i].length()];

                    for (int j = 0; j < jsonparams[i].length(); j++) {
                        if (jsonparams[i].isNull(j))
                            params[j] = "";
                        else
                            params[j] = jsonparams[i].getString(j);
                    }
                }

                Cursor myCursor = mydb.rawQuery(query, params);

                if (query_id.length() > 0)
                    this.processResults(myCursor, query_id, tx_id);

                myCursor.close();
            }
        }
        mydb.setTransactionSuccessful();
    } catch (SQLiteException ex) {
        ex.printStackTrace();
        Log.v("executeSqlBatch", "SQLitePlugin.executeSql(): Error=" + ex.getMessage());
        this.sendJavascriptCB("window.SQLitePluginTransactionCB.txErrorCallback('" + tx_id + "', '"
                + ex.getMessage() + "');");
    } catch (JSONException ex) {
        ex.printStackTrace();
        Log.v("executeSqlBatch", "SQLitePlugin.executeSql(): Error=" + ex.getMessage());
        this.sendJavascriptCB("window.SQLitePluginTransactionCB.txErrorCallback('" + tx_id + "', '"
                + ex.getMessage() + "');");
    } finally {
        mydb.endTransaction();
        Log.v("executeSqlBatch", tx_id);
        this.sendJavascriptCB("window.SQLitePluginTransactionCB.txCompleteCallback('" + tx_id + "');");
    }
}

From source file:com.zetaDevelopment.phonegap.plugin.sqlitePlugin.SQLitePlugin.java

/**
 * Executes a batch request and sends the results via sendJavascriptCB().
 *
 * @param dbname/*from  ww  w  . jav  a 2 s . co  m*/
 *            The name of the database.
 *
 * @param queryarr
 *            Array of query strings
 *
 * @param jsonparams
 *            Array of JSON query parameters
 *
 * @param queryIDs
 *            Array of query ids
 *
 * @param tx_id
 *            Transaction id
 *
 */
private void executeSqlBatch(String dbname, String[] queryarr, JSONArray[] jsonparams, String[] queryIDs,
        String tx_id) {
    SQLiteDatabase mydb = this.getDatabase(dbname);

    if (mydb == null)
        return;

    try {
        mydb.beginTransaction();

        String query = "";
        String query_id = "";
        int len = queryarr.length;

        for (int i = 0; i < len; i++) {
            query = queryarr[i];
            query_id = queryIDs[i];
            if (query.toLowerCase(Locale.getDefault()).startsWith("insert") && jsonparams != null) {
                SQLiteStatement myStatement = mydb.compileStatement(query);
                for (int j = 0; j < jsonparams[i].length(); j++) {
                    if (jsonparams[i].get(j) instanceof Float || jsonparams[i].get(j) instanceof Double) {
                        myStatement.bindDouble(j + 1, jsonparams[i].getDouble(j));
                    } else if (jsonparams[i].get(j) instanceof Number) {
                        myStatement.bindLong(j + 1, jsonparams[i].getLong(j));
                    } else {
                        myStatement.bindString(j + 1, jsonparams[i].getString(j));
                    }
                }
                long insertId = myStatement.executeInsert();

                String result = "{'insertId':'" + insertId + "'}";
                this.sendJavascriptCB("window.SQLitePluginTransactionCB.queryCompleteCallback('" + tx_id + "','"
                        + query_id + "', " + result + ");");
            } else {
                String[] params = null;

                if (jsonparams != null) {
                    params = new String[jsonparams[i].length()];

                    for (int j = 0; j < jsonparams[i].length(); j++) {
                        if (jsonparams[i].isNull(j))
                            params[j] = "";
                        else
                            params[j] = jsonparams[i].getString(j);
                    }
                }

                Cursor myCursor = mydb.rawQuery(query, params);

                if (query_id.length() > 0)
                    this.processResults(myCursor, query_id, tx_id);

                myCursor.close();
            }
        }
        mydb.setTransactionSuccessful();
    } catch (SQLiteException ex) {
        ex.printStackTrace();
        Log.v("executeSqlBatch", "SQLitePlugin.executeSql(): Error=" + ex.getMessage());
        this.sendJavascriptCB("window.SQLitePluginTransactionCB.txErrorCallback('" + tx_id + "', '"
                + ex.getMessage() + "');");
    } catch (JSONException ex) {
        ex.printStackTrace();
        Log.v("executeSqlBatch", "SQLitePlugin.executeSql(): Error=" + ex.getMessage());
        this.sendJavascriptCB("window.SQLitePluginTransactionCB.txErrorCallback('" + tx_id + "', '"
                + ex.getMessage() + "');");
    } finally {
        mydb.endTransaction();
        Log.v("executeSqlBatch", tx_id);
        this.sendJavascriptCB("window.SQLitePluginTransactionCB.txCompleteCallback('" + tx_id + "');");
    }
}

From source file:de.stadtrallye.rallyesoft.model.map.MapManager.java

private void updateDatabase(List<Node> nodes, List<Edge> edges) {
    SQLiteDatabase db = getDb();//from   w w w  .  j a  va  2  s .  c om

    db.beginTransaction();
    try {
        db.delete(Edges.TABLE, null, null);
        db.delete(Nodes.TABLE, null, null);

        SQLiteStatement nodeIn = db.compileStatement("INSERT INTO " + Nodes.TABLE + " ("
                + DatabaseHelper.strStr(Nodes.COLS) + ") VALUES (?, ?, ?, ?, ?)");

        SQLiteStatement edgeIn = db.compileStatement(
                "INSERT INTO " + Edges.TABLE + " (" + DatabaseHelper.strStr(Edges.COLS) + ") VALUES (?, ?, ?)");

        for (Node n : nodes) {
            nodeIn.bindLong(1, n.nodeID);
            nodeIn.bindString(2, n.name);
            nodeIn.bindDouble(3, n.location.latitude);
            nodeIn.bindDouble(4, n.location.longitude);
            nodeIn.bindString(5, n.description);
            nodeIn.executeInsert();
        }

        for (Edge m : edges) {
            edgeIn.bindLong(1, m.nodeA.nodeID);
            edgeIn.bindLong(2, m.nodeB.nodeID);
            edgeIn.bindString(3, m.type.toString());
            edgeIn.executeInsert();
        }

        db.setTransactionSuccessful();
    } catch (Exception e) {
        Log.e(THIS, "Map Update on Database failed", e);
    } finally {
        db.endTransaction();
    }
}

From source file:com.ichi2.anki.SyncClient.java

private void updateSources(JSONArray sources) {
    String sql = "INSERT OR REPLACE INTO sources VALUES(?,?,?,?,?)";
    SQLiteStatement statement = AnkiDatabaseManager.getDatabase(mDeck.getDeckPath()).getDatabase()
            .compileStatement(sql);//w w w  .  jav a  2  s  .  c o m
    int len = sources.length();
    for (int i = 0; i < len; i++) {
        try {
            JSONArray source = sources.getJSONArray(i);
            statement.bindLong(1, source.getLong(0));
            statement.bindString(2, source.getString(1));
            statement.bindDouble(3, source.getDouble(2));
            statement.bindDouble(4, source.getDouble(3));
            statement.bindString(5, source.getString(4));
            statement.execute();
        } catch (JSONException e) {
            Log.i(AnkiDroidApp.TAG, "JSONException = " + e.getMessage());
        }
    }
    statement.close();
}

From source file:com.ichi2.anki.SyncClient.java

private void updateHistory(JSONArray history) {
    String sql = "INSERT OR IGNORE INTO reviewHistory (cardId, time, lastInterval, nextInterval, ease, delay, "
            + "lastFactor, nextFactor, reps, thinkingTime, yesCount, noCount) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
    SQLiteStatement statement = AnkiDatabaseManager.getDatabase(mDeck.getDeckPath()).getDatabase()
            .compileStatement(sql);/*w w  w.  j av  a2s  .  c  o  m*/
    int len = history.length();
    for (int i = 0; i < len; i++) {
        try {
            JSONArray h = history.getJSONArray(i);

            // cardId
            statement.bindLong(1, h.getLong(0));
            // time
            statement.bindDouble(2, h.getDouble(1));
            // lastInterval
            statement.bindDouble(3, h.getDouble(2));
            // nextInterval
            statement.bindDouble(4, h.getDouble(3));
            // ease
            statement.bindString(5, h.getString(4));
            // delay
            statement.bindDouble(6, h.getDouble(5));
            // lastFactor
            statement.bindDouble(7, h.getDouble(6));
            // nextFactor
            statement.bindDouble(8, h.getDouble(7));
            // reps
            statement.bindDouble(9, h.getDouble(8));
            // thinkingTime
            statement.bindDouble(10, h.getDouble(9));
            // yesCount
            statement.bindDouble(11, h.getDouble(10));
            // noCount
            statement.bindDouble(12, h.getDouble(11));

            statement.execute();
        } catch (JSONException e) {
            Log.i(AnkiDroidApp.TAG, "JSONException = " + e.getMessage());
        }
    }
    statement.close();
}

From source file:com.ichi2.anki.SyncClient.java

private void updateFacts(JSONObject factsDict) {
    try {// w w  w.j a v  a 2  s. co  m
        AnkiDb ankiDB = AnkiDatabaseManager.getDatabase(mDeck.getDeckPath());
        JSONArray facts = factsDict.getJSONArray("facts");
        int lenFacts = facts.length();

        if (lenFacts > 0) {
            JSONArray fields = factsDict.getJSONArray("fields");
            int lenFields = fields.length();

            // Grab fact ids
            // They will be used later to recalculate the count of facts and to delete them from DB
            ArrayList<String> factIds = new ArrayList<String>();
            for (int i = 0; i < lenFacts; i++) {
                factIds.add(facts.getJSONArray(i).getString(0));
            }
            String factIdsString = Utils.ids2str(factIds);

            // Recalculate fact count
            mDeck.setFactCount((int) (mDeck.getFactCount() + (lenFacts
                    - ankiDB.queryScalar("SELECT COUNT(*) FROM facts WHERE id IN " + factIdsString))));

            // Update facts
            String sqlFact = "INSERT OR REPLACE INTO facts (id, modelId, created, modified, tags, spaceUntil, lastCardId)"
                    + " VALUES(?,?,?,?,?,?,?)";
            SQLiteStatement statement = ankiDB.getDatabase().compileStatement(sqlFact);
            for (int i = 0; i < lenFacts; i++) {
                JSONArray fact = facts.getJSONArray(i);

                // id
                statement.bindLong(1, fact.getLong(0));
                // modelId
                statement.bindLong(2, fact.getLong(1));
                // created
                statement.bindDouble(3, fact.getDouble(2));
                // modified
                statement.bindDouble(4, fact.getDouble(3));
                // tags
                statement.bindString(5, fact.getString(4));
                // spaceUntil
                statement.bindDouble(6, fact.getDouble(5));
                // lastCardId
                if (!fact.isNull(6)) {
                    statement.bindLong(7, fact.getLong(6));
                } else {
                    statement.bindNull(7);
                }

                statement.execute();
            }
            statement.close();

            // Update fields (and delete first the local ones, since ids may have changed)
            ankiDB.getDatabase().execSQL("DELETE FROM fields WHERE factId IN " + factIdsString);

            String sqlFields = "INSERT INTO fields (id, factId, fieldModelId, ordinal, value) VALUES(?,?,?,?,?)";
            statement = ankiDB.getDatabase().compileStatement(sqlFields);
            for (int i = 0; i < lenFields; i++) {
                JSONArray field = fields.getJSONArray(i);

                // id
                statement.bindLong(1, field.getLong(0));
                // factId
                statement.bindLong(2, field.getLong(1));
                // fieldModelId
                statement.bindLong(3, field.getLong(2));
                // ordinal
                statement.bindString(4, field.getString(3));
                // value
                statement.bindString(5, field.getString(4));

                statement.execute();
            }
            statement.close();

            // Delete inserted facts from deleted
            ankiDB.getDatabase().execSQL("DELETE FROM factsDeleted WHERE factId IN " + factIdsString);
        }
    } catch (JSONException e) {
        Log.i(AnkiDroidApp.TAG, "JSONException = " + e.getMessage());
    }
}

From source file:com.ichi2.anki.SyncClient.java

private void updateModels(JSONArray models) {
    ArrayList<String> insertedModelsIds = new ArrayList<String>();
    AnkiDb ankiDB = AnkiDatabaseManager.getDatabase(mDeck.getDeckPath());

    String sql = "INSERT OR REPLACE INTO models"
            + " (id, deckId, created, modified, tags, name, description, features, spacing, initialSpacing, source)"
            + " VALUES(?,?,?,?,?,?,?,?,?,?,?)";
    SQLiteStatement statement = ankiDB.getDatabase().compileStatement(sql);
    int len = models.length();
    for (int i = 0; i < len; i++) {
        try {/*from  w ww  . ja  v a 2s  . c  o  m*/
            JSONObject model = models.getJSONObject(i);

            // id
            String id = model.getString("id");
            statement.bindString(1, id);
            // deckId
            statement.bindLong(2, model.getLong("deckId"));
            // created
            statement.bindDouble(3, model.getDouble("created"));
            // modified
            statement.bindDouble(4, model.getDouble("modified"));
            // tags
            statement.bindString(5, model.getString("tags"));
            // name
            statement.bindString(6, model.getString("name"));
            // description
            statement.bindString(7, model.getString("name"));
            // features
            statement.bindString(8, model.getString("features"));
            // spacing
            statement.bindDouble(9, model.getDouble("spacing"));
            // initialSpacing
            statement.bindDouble(10, model.getDouble("initialSpacing"));
            // source
            statement.bindLong(11, model.getLong("source"));

            statement.execute();

            insertedModelsIds.add(id);

            mergeFieldModels(id, model.getJSONArray("fieldModels"));
            mergeCardModels(id, model.getJSONArray("cardModels"));
        } catch (JSONException e) {
            Log.i(AnkiDroidApp.TAG, "JSONException = " + e.getMessage());
        }
    }
    statement.close();

    // Delete inserted models from modelsDeleted
    ankiDB.getDatabase()
            .execSQL("DELETE FROM modelsDeleted WHERE modelId IN " + Utils.ids2str(insertedModelsIds));
}

From source file:com.ichi2.anki.SyncClient.java

private void updateCards(JSONArray cards) {
    int len = cards.length();
    if (len > 0) {
        AnkiDb ankiDB = AnkiDatabaseManager.getDatabase(mDeck.getDeckPath());
        ArrayList<String> ids = new ArrayList<String>();
        for (int i = 0; i < len; i++) {
            try {
                ids.add(cards.getJSONArray(i).getString(0));
            } catch (JSONException e) {
                Log.i(AnkiDroidApp.TAG, "JSONException = " + e.getMessage());
            }/* w ww. ja  va2 s  .  c om*/
        }
        String idsString = Utils.ids2str(ids);

        mDeck.setCardCount((int) (mDeck.getCardCount()
                + (len - ankiDB.queryScalar("SELECT COUNT(*) FROM cards WHERE id IN " + idsString))));

        String sql = "INSERT OR REPLACE INTO cards (id, factId, cardModelId, created, modified, tags, ordinal, "
                + "priority, interval, lastInterval, due, lastDue, factor, firstAnswered, reps, successive, "
                + "averageTime, reviewTime, youngEase0, youngEase1, youngEase2, youngEase3, youngEase4, "
                + "matureEase0, matureEase1, matureEase2, matureEase3, matureEase4, yesCount, noCount, question, "
                + "answer, lastFactor, spaceUntil, type, combinedDue, relativeDelay, isDue) "
                + "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, 0, 0)";
        SQLiteStatement statement = ankiDB.getDatabase().compileStatement(sql);
        for (int i = 0; i < len; i++) {
            try {
                JSONArray card = cards.getJSONArray(i);

                // id
                statement.bindLong(1, card.getLong(0));
                // factId
                statement.bindLong(2, card.getLong(1));
                // cardModelId
                statement.bindLong(3, card.getLong(2));
                // created
                statement.bindDouble(4, card.getDouble(3));
                // modified
                statement.bindDouble(5, card.getDouble(4));
                // tags
                statement.bindString(6, card.getString(5));
                // ordinal
                statement.bindString(7, card.getString(6));
                // priority
                statement.bindString(8, card.getString(7));
                // interval
                statement.bindDouble(9, card.getDouble(8));
                // lastInterval
                statement.bindDouble(10, card.getDouble(9));
                // due
                statement.bindDouble(11, card.getDouble(10));
                // lastDue
                statement.bindDouble(12, card.getDouble(11));
                // factor
                statement.bindDouble(13, card.getDouble(12));
                // firstAnswered
                statement.bindDouble(14, card.getDouble(13));
                // reps
                statement.bindString(15, card.getString(14));
                // successive
                statement.bindString(16, card.getString(15));
                // averageTime
                statement.bindDouble(17, card.getDouble(16));
                // reviewTime
                statement.bindDouble(18, card.getDouble(17));
                // youngEase0
                statement.bindString(19, card.getString(18));
                // youngEase1
                statement.bindString(20, card.getString(19));
                // youngEase2
                statement.bindString(21, card.getString(20));
                // youngEase3
                statement.bindString(22, card.getString(21));
                // youngEase4
                statement.bindString(23, card.getString(22));
                // matureEase0
                statement.bindString(24, card.getString(23));
                // matureEase1
                statement.bindString(25, card.getString(24));
                // matureEase2
                statement.bindString(26, card.getString(25));
                // matureEase3
                statement.bindString(27, card.getString(26));
                // matureEase4
                statement.bindString(28, card.getString(27));
                // yesCount
                statement.bindString(29, card.getString(28));
                // noCount
                statement.bindString(30, card.getString(29));
                // question
                statement.bindString(31, card.getString(30));
                // answer
                statement.bindString(32, card.getString(31));
                // lastFactor
                statement.bindDouble(33, card.getDouble(32));
                // spaceUntil
                statement.bindDouble(34, card.getDouble(33));
                // type
                statement.bindString(35, card.getString(34));
                // combinedDue
                statement.bindString(36, card.getString(35));

                statement.execute();
            } catch (JSONException e) {
                Log.i(AnkiDroidApp.TAG, "JSONException = " + e.getMessage());
            }
        }
        statement.close();

        ankiDB.getDatabase().execSQL("DELETE FROM cardsDeleted WHERE cardId IN " + idsString);
    }
}