Example usage for android.database.sqlite SQLiteStatement bindString

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

Introduction

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

Prototype

public void bindString(int index, String value) 

Source Link

Document

Bind a String value to this statement.

Usage

From source file:Main.java

/**
 * Binds the string or null value to an statement.
 *
 * @param statement The statement.//from w w w. ja  v a2 s.  c  o  m
 * @param index     The index to bind.
 * @param value     The value to bind.
 */
public static void bindStringOrNull(SQLiteStatement statement, int index, @Nullable String value) {
    if (value != null) {
        statement.bindString(index, value);
    } else {
        statement.bindNull(index);
    }
}

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  . j  a  v a  2 s . 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:org.fitchfamily.android.wifi_backend.database.Database.java

private static void bind(SQLiteStatement statement, SimpleLocation location, int index) {
    statement.bindString(index, String.valueOf(location == null ? 0.f : location.latitude()));
    statement.bindString(index + 1, String.valueOf(location == null ? 0.f : location.longitude()));
}

From source file:org.fitchfamily.android.wifi_backend.database.Database.java

private static SQLiteStatement bind(SQLiteStatement statement, AccessPoint accessPoint, int start) {
    if (!TextUtils.isEmpty(accessPoint.ssid())) {
        statement.bindString(start, accessPoint.ssid());
    }/* w  w w . j  ava  2 s.  c om*/
    statement.bindString(start + 1, String.valueOf(accessPoint.estimateLocation().latitude()));
    statement.bindString(start + 2, String.valueOf(accessPoint.estimateLocation().longitude()));
    statement.bindString(start + 3, String.valueOf(accessPoint.estimateLocation().radius()));
    statement.bindString(start + 4, String.valueOf(accessPoint.moveGuard()));
    statement.bindLong(start + 5, changedValue(accessPoint));
    bind(statement, accessPoint.sample(0), start + 6);
    bind(statement, accessPoint.sample(1), start + 8);
    bind(statement, accessPoint.sample(2), start + 10);
    return statement;
}

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);/*w  ww  .ja  va2s  .  co  m*/
    stmt.bindDouble(3, targetPrice);
    stmt.bindString(4, reason);
    stmt.execute();
    stmt.close();

    db.close();

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

From source file:com.mobshep.mobileshepherd.Insecure_Data_Storage1.java

private void InsertData(String user, String pass) throws IOException {

    try {//  w  ww .  ja v a2s.  c  o  m
        String path = DB_PATH + DB_NAME;
        Users = this.openOrCreateDatabase(path, MODE_PRIVATE, null);

        //get the base64 functionality
        annoyingObfuscationUtil util = new annoyingObfuscationUtil();

        SQLiteStatement stmt = Users.compileStatement("INSERT INTO Users (name, password) VALUES (?,?);");
        stmt.bindString(1, user);
        stmt.bindString(2, util.Obfuscation1(pass));
        stmt.execute();

        Snackbar insert = Snackbar.make(findViewById(android.R.id.content), "Data Inserted!",
                Snackbar.LENGTH_LONG);
        insert.show();

        EditText username = (EditText) findViewById(R.id.etName);
        EditText password = (EditText) findViewById(R.id.etPass);

        username.setText("");
        password.setText("");

    } catch (Exception e) {
        Log.e("DB ERROR", "Error Inserting into Database");

        Snackbar error = Snackbar.make(findViewById(android.R.id.content), "Could not Insert Data.",
                Snackbar.LENGTH_LONG);
        error.show();
    }
}

From source file:com.mobshep.mobileshepherd.Insecure_Data_Storage.java

private void InsertData(String user, String pass) throws IOException {

    try {/*from w ww .  ja  v  a2  s  . co  m*/
        String path = DB_PATH + DB_NAME;
        Members = this.openOrCreateDatabase(path, MODE_PRIVATE, null);

        SQLiteStatement stmt = Members.compileStatement("INSERT INTO Members (name, password) VALUES (?,?);");
        stmt.bindString(1, user);
        stmt.bindString(2, pass);
        stmt.execute();

        Snackbar insert = Snackbar.make(findViewById(android.R.id.content), "Data Inserted!",
                Snackbar.LENGTH_LONG);
        insert.show();

        EditText username = (EditText) findViewById(R.id.etName);
        EditText password = (EditText) findViewById(R.id.etPass);

        username.setText("");
        password.setText("");

    } catch (Exception e) {
        Log.e("DB ERROR", "Error Inserting into Database");

        Snackbar error = Snackbar.make(findViewById(android.R.id.content), "Could not Insert Data.",
                Snackbar.LENGTH_LONG);
        error.show();
    }
}

From source file:com.aquatest.dbinterface.tools.DatabaseUpdater.java

/**
 * Populates the "?" parameters in the prepared statement with values from
 * the included JSON object. It is assumed that the prepared statement was
 * created based on the same set of field names (in the same order) as have
 * been passed to this method./*  w w  w.j a va2s.  com*/
 * 
 * @param preparedStatement
 *            previously prepared SQL statement, which includes "?"
 *            parameter placeholders
 * @param type
 *            Operation type. Can be one of <code>TYPE_ADDED</code>,
 *            <code>TYPE_UPDATED</code> or <code>TYPE_DELETED</code>
 * @param fieldNames
 *            array of field names relating to this query
 * @param dataRow
 *            JSON object containing data relevant to this prepared
 *            statement
 * @throws JSONException
 *             if the JSON array or data row contain invalid JSON
 */
// method declared static for Android optimisation
private static void bindQueryParameters(SQLiteStatement preparedStatement, int type, JSONArray fieldNames,
        JSONObject dataRow) throws JSONException {
    // initialise the query by clearing out any previous parameters
    preparedStatement.clearBindings();

    // index used to bind parameters to the prepared statement
    int paramIndex = 1;

    // DELETE statements only need the id field bound, other require all
    // fields
    if (type == TYPE_DELETED) {
        // assume the id field exists for a DELETE statement
        String value = dataRow.getString("deleted_id");
        preparedStatement.bindString(1, value);
    } else {
        // do this to optimise the Android code
        int fieldCount = fieldNames.length();

        // add items to prepared statement by iterating over the array
        for (int i = 0; i < fieldCount; i++) {
            String fieldName = fieldNames.getString(i);

            // exclude some columns from the update
            if (includeFieldInUpdates(fieldName)) {
                // get data from JSON and add as parameter
                preparedStatement.bindString(paramIndex++, dataRow.getString(fieldName));
            }
        }
    }
}

From source file:com.dm.wallpaper.board.databases.Database.java

public void addCategories(List<WallpaperJson> categories) {
    String query = "INSERT INTO " + TABLE_CATEGORIES + " (" + KEY_NAME + "," + KEY_THUMB_URL
            + ") VALUES (?,?);";
    SQLiteDatabase db = this.getWritableDatabase();
    SQLiteStatement statement = db.compileStatement(query);
    db.beginTransaction();/*from ww w  .  j  a  va2  s  .c o m*/

    for (int i = 0; i < categories.size(); i++) {
        statement.clearBindings();
        statement.bindString(1, categories.get(i).name);
        statement.bindString(2, categories.get(i).thumbUrl == null ? "" : categories.get(i).thumbUrl);
        statement.execute();
    }
    db.setTransactionSuccessful();
    db.endTransaction();
    db.close();
}

From source file:com.dm.wallpaper.board.databases.Database.java

public void addWallpapers(@NonNull List<Wallpaper> wallpapers) {
    String query = "INSERT INTO " + TABLE_WALLPAPERS + " (" + KEY_NAME + "," + KEY_AUTHOR + "," + KEY_URL + ","
            + KEY_THUMB_URL + "," + KEY_CATEGORY + "," + KEY_ADDED_ON + ") VALUES (?,?,?,?,?,?);";
    SQLiteDatabase db = this.getWritableDatabase();
    SQLiteStatement statement = db.compileStatement(query);
    db.beginTransaction();/*from w  w w  .  j ava  2s. c  o m*/

    for (int i = 0; i < wallpapers.size(); i++) {
        statement.clearBindings();
        statement.bindString(1, wallpapers.get(i).getName());
        statement.bindString(2, wallpapers.get(i).getAuthor());
        statement.bindString(3, wallpapers.get(i).getUrl());
        statement.bindString(4, wallpapers.get(i).getThumbUrl());
        statement.bindString(5, wallpapers.get(i).getCategory());
        statement.bindString(6, TimeHelper.getLongDateTime());
        statement.execute();
    }
    db.setTransactionSuccessful();
    db.endTransaction();
    db.close();
}