Java Code Examples for android.database.sqlite.SQLiteStatement#bindNull()

The following examples show how to use android.database.sqlite.SQLiteStatement#bindNull() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private void bindArgsToStatement(SQLiteStatement myStatement, ReadableArray sqlArgs) {
    if (sqlArgs == null)
        return;

    for (int i = 0; i < sqlArgs.size(); i++) {
        ReadableType type = sqlArgs.getType(i);
        if (type == ReadableType.Number) {
            double tmp = sqlArgs.getDouble(i);
            if (tmp == (long) tmp) {
                myStatement.bindLong(i + 1, (long) tmp);
            } else {
                myStatement.bindDouble(i + 1, tmp);
            }
        } else if (sqlArgs.isNull(i)) {
            myStatement.bindNull(i + 1);
        } else {
            myStatement.bindString(i + 1, sqlArgs.getString(i));
        }
    }
}
 
Example 2
private void bindArgsToStatement(SQLiteStatement myStatement, ReadableArray sqlArgs) {
    for (int i = 0; i < sqlArgs.size(); i++) {
        ReadableType type = sqlArgs.getType(i);
        if (type == ReadableType.Number){
            double tmp = sqlArgs.getDouble(i);
            if (tmp == (long) tmp) {
                myStatement.bindLong(i + 1, (long) tmp);
            } else {
                myStatement.bindDouble(i + 1, tmp);
            }
        } else if (sqlArgs.isNull(i)) {
            myStatement.bindNull(i + 1);
        } else {
            myStatement.bindString(i + 1, SQLitePluginConverter.getString(sqlArgs,i,""));
        }
    }
}
 
Example 3
Source Project: wallpaperboard   File: Database.java    License: Apache License 2.0 5 votes vote down vote up
public void updateWallpapers(@NonNull List<Wallpaper> wallpapers) {
    if (!openDatabase()) {
        LogUtil.e("Database error: updateWallpapers() failed to open database");
        return;
    }

    String query = "UPDATE " +TABLE_WALLPAPERS+ " SET " +KEY_FAVORITE+ " = ?, " +KEY_SIZE+ " = ?, "
            +KEY_MIME_TYPE+ " = ?, " +KEY_WIDTH+ " = ?," +KEY_HEIGHT+ " = ?, " +KEY_COLOR+ " = ? "
            +"WHERE " +KEY_URL+ " = ?";
    SQLiteStatement statement = mDatabase.get().mSQLiteDatabase.compileStatement(query);
    mDatabase.get().mSQLiteDatabase.beginTransaction();

    for (Wallpaper wallpaper : wallpapers) {
        statement.clearBindings();

        statement.bindLong(1, wallpaper.isFavorite() ? 1 : 0);
        statement.bindLong(2, wallpaper.getSize());

        String mimeType = wallpaper.getMimeType();
        if (mimeType != null) {
            statement.bindString(3, mimeType);
        } else {
            statement.bindNull(3);
        }

        ImageSize dimension = wallpaper.getDimensions();
        int width = dimension == null ? 0 : dimension.getWidth();
        int height = dimension == null ? 0 : dimension.getHeight();
        statement.bindLong(4, width);
        statement.bindLong(5, height);

        statement.bindLong(6, wallpaper.getColor());
        statement.bindString(7, wallpaper.getUrl());
        statement.execute();
    }

    mDatabase.get().mSQLiteDatabase.setTransactionSuccessful();
    mDatabase.get().mSQLiteDatabase.endTransaction();
}
 
Example 4
Source Project: AvI   File: SQLiteAndroidDatabase.java    License: MIT License 5 votes vote down vote up
private void bindArgsToStatement(SQLiteStatement myStatement, JSONArray sqlArgs) throws JSONException {
    for (int i = 0; i < sqlArgs.length(); i++) {
        if (sqlArgs.get(i) instanceof Float || sqlArgs.get(i) instanceof Double) {
            myStatement.bindDouble(i + 1, sqlArgs.getDouble(i));
        } else if (sqlArgs.get(i) instanceof Number) {
            myStatement.bindLong(i + 1, sqlArgs.getLong(i));
        } else if (sqlArgs.isNull(i)) {
            myStatement.bindNull(i + 1);
        } else {
            myStatement.bindString(i + 1, sqlArgs.getString(i));
        }
    }
}
 
Example 5
Source Project: beacons-android   File: Storage.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Binds either a string or NULL to a SQLite statement.
 * Reason: trying to bind a null string would normally crash the app.
 * @param statement    SQLite statement
 * @param value        A string, or null
 */
public static void bindStringOrNull(@NonNull SQLiteStatement statement, int index, String value) {
    if (null == value) {
        statement.bindNull(index);
    }
    else {
        statement.bindString(index, value);
    }
}
 
Example 6
private static void addEncryptedStingToStatement(MasterCipher masterCipher, SQLiteStatement statement, int index, String value) {
  if (value == null || value.equals("null")) {
    statement.bindNull(index);
  } else {
    statement.bindString(index, masterCipher.encryptBody(value));
  }
}
 
Example 7
private static void addEncryptedStringToStatement(Context context, SQLiteStatement statement,
                                                  Cursor cursor, MasterSecret masterSecret,
                                                  int index, String key)
{
  int columnIndex = cursor.getColumnIndexOrThrow(key);

  if (cursor.isNull(columnIndex)) {
    statement.bindNull(index);
  } else {
    statement.bindString(index, encrypt(masterSecret, cursor.getString(columnIndex)));
  }
}
 
Example 8
private static void addStringToStatement(SQLiteStatement statement, Cursor cursor,
                                         int index, String key)
{
  int columnIndex = cursor.getColumnIndexOrThrow(key);

  if (cursor.isNull(columnIndex)) {
    statement.bindNull(index);
  } else {
    statement.bindString(index, cursor.getString(columnIndex));
  }
}
 
Example 9
private static void addIntToStatement(SQLiteStatement statement, Cursor cursor,
                                      int index, String key)
{
  int columnIndex = cursor.getColumnIndexOrThrow(key);

  if (cursor.isNull(columnIndex)) {
    statement.bindNull(index);
  } else {
    statement.bindLong(index, cursor.getLong(columnIndex));
  }
}
 
Example 10
@Override
public void execute(String sql, Object... params) throws IOException {
    try {
        SQLiteStatement s = db.compileStatement(sql);
        for (int i = 0; i < params.length; i++) {
            Object p = params[i];
            if(p == null){
                s.bindNull(i + 1);
            }else{
                if(p instanceof String){
                    s.bindString(i + 1, (String)p);                    
                }else if(p instanceof byte[]){
                    s.bindBlob(i + 1, (byte [])p);
                }else if(p instanceof Double){
                    s.bindDouble(i + 1, ((Double)p).doubleValue());
                } else if(p instanceof Long){
                    s.bindLong(i + 1, ((Long)p).longValue());
                } else if(p instanceof Integer){
                    s.bindLong(i + 1, ((Integer)p).intValue());
                } else {
                    if(p != null) {
                        s.bindString(i + 1, p.toString());
                    }
                }
            }
        }
        s.execute();
        s.close();
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException(e.getMessage());
    }
    
}
 
Example 11
public <T> SQLiteStatement bindValue(SQLiteStatement statement, Integer index, T value) {

        if (value != null) {
            statement.bindString(index, value.toString());
        } else {
            statement.bindNull(index);
        }
        return statement;
    }
 
Example 12
Source Project: candybar   File: Database.java    License: Apache License 2.0 4 votes vote down vote up
public void addWallpapers(List<?> list) {
    if (!openDatabase()) {
        LogUtil.e("Database error: addWallpapers() failed to open database");
        return;
    }

    String query = "INSERT OR IGNORE INTO " + TABLE_WALLPAPERS + " (" + KEY_NAME + "," + KEY_AUTHOR + "," + KEY_URL + ","
            + KEY_THUMB_URL + "," + KEY_ADDED_ON + ") VALUES (?,?,?,?,?);";
    SQLiteStatement statement = mDatabase.get().mSQLiteDatabase.compileStatement(query);
    mDatabase.get().mSQLiteDatabase.beginTransaction();

    for (int i = 0; i < list.size(); i++) {
        statement.clearBindings();

        Wallpaper wallpaper;
        if (list.get(i) instanceof Wallpaper) {
            wallpaper = (Wallpaper) list.get(i);
        } else {
            wallpaper = JsonHelper.getWallpaper(list.get(i));
        }

        if (wallpaper != null) {
            if (wallpaper.getURL() != null) {
                String name = wallpaper.getName();
                if (name == null) name = "";

                statement.bindString(1, name);

                if (wallpaper.getAuthor() != null) {
                    statement.bindString(2, wallpaper.getAuthor());
                } else {
                    statement.bindNull(2);
                }

                statement.bindString(3, wallpaper.getURL());
                statement.bindString(4, wallpaper.getThumbUrl());
                statement.bindString(5, TimeHelper.getLongDateTime());
                statement.execute();
            }
        }
    }
    mDatabase.get().mSQLiteDatabase.setTransactionSuccessful();
    mDatabase.get().mSQLiteDatabase.endTransaction();
}
 
Example 13
Source Project: wallpaperboard   File: Database.java    License: Apache License 2.0 4 votes vote down vote up
public void add(@NonNull List<?> categoryLIst, @NonNull List<?> wallpaperList) {
    if (!openDatabase()) {
        LogUtil.e("Database error: add() failed to open database");
        return;
    }

    Iterator categoryIterator = categoryLIst.iterator();
    Iterator wallpaperIterator = wallpaperList.iterator();
    int size = categoryLIst.size() > wallpaperList.size() ? categoryLIst.size() : wallpaperList.size();

    String categoryQuery = "INSERT OR IGNORE INTO " +TABLE_CATEGORIES+ " (" +KEY_NAME+ ") VALUES (?);";
    String wallpaperQuery = "INSERT OR IGNORE INTO " +TABLE_WALLPAPERS+ " (" +KEY_NAME+ "," +KEY_AUTHOR+ "," +KEY_URL+ ","
            +KEY_THUMB_URL+ "," +KEY_CATEGORY+ "," +KEY_ADDED_ON+ ") VALUES (?,?,?,?,?,?);";

    SQLiteStatement categoryStatements = mDatabase.get().mSQLiteDatabase.compileStatement(categoryQuery);
    SQLiteStatement wallpaperStatements = mDatabase.get().mSQLiteDatabase.compileStatement(wallpaperQuery);
    mDatabase.get().mSQLiteDatabase.beginTransaction();

    int i = 0;
    do {
        categoryStatements.clearBindings();
        wallpaperStatements.clearBindings();

        if (categoryIterator.hasNext()) {
            Category category;
            if (categoryIterator.next() instanceof Category) {
                category = (Category) categoryLIst.get(i);
            } else {
                category = JsonHelper.getCategory(categoryLIst.get(i));
            }

            if (category != null) {
                categoryStatements.bindString(1, category.getName());
                categoryStatements.execute();
            }
        }

        if (wallpaperIterator.hasNext()) {
            Wallpaper wallpaper;
            if (wallpaperIterator.next() instanceof Wallpaper) {
                wallpaper = (Wallpaper) wallpaperList.get(i);
            } else {
                wallpaper = JsonHelper.getWallpaper(wallpaperList.get(i));
            }

            if (wallpaper != null) {
                if (wallpaper.getUrl() != null) {
                    String name = wallpaper.getName();
                    if (name == null) name = "";

                    wallpaperStatements.bindString(1, name);

                    if (wallpaper.getAuthor() != null) {
                        wallpaperStatements.bindString(2, wallpaper.getAuthor());
                    } else {
                        wallpaperStatements.bindNull(2);
                    }

                    wallpaperStatements.bindString(3, wallpaper.getUrl());
                    wallpaperStatements.bindString(4, wallpaper.getThumbUrl());
                    wallpaperStatements.bindString(5, wallpaper.getCategory());
                    wallpaperStatements.bindString(6, TimeHelper.getLongDateTime());
                    wallpaperStatements.execute();
                }
            }
        }
        i++;
    } while (i < size);

    mDatabase.get().mSQLiteDatabase.setTransactionSuccessful();
    mDatabase.get().mSQLiteDatabase.endTransaction();
}
 
Example 14
Source Project: wallpaperboard   File: Database.java    License: Apache License 2.0 4 votes vote down vote up
public void addWallpapers(@NonNull List<?> list) {
    if (!openDatabase()) {
        LogUtil.e("Database error: addWallpapers() failed to open database");
        return;
    }

    String query = "INSERT OR IGNORE INTO " +TABLE_WALLPAPERS+ " (" +KEY_NAME+ "," +KEY_AUTHOR+ "," +KEY_URL+ ","
            +KEY_THUMB_URL+ "," +KEY_CATEGORY+ "," +KEY_ADDED_ON+ ") VALUES (?,?,?,?,?,?);";
    SQLiteStatement statement = mDatabase.get().mSQLiteDatabase.compileStatement(query);
    mDatabase.get().mSQLiteDatabase.beginTransaction();

    for (int i = 0; i < list.size(); i++) {
        statement.clearBindings();

        Wallpaper wallpaper;
        if (list.get(i) instanceof Wallpaper) {
            wallpaper = (Wallpaper) list.get(i);
        } else {
            wallpaper = JsonHelper.getWallpaper(list.get(i));
        }

        if (wallpaper != null) {
            if (wallpaper.getUrl() != null) {
                String name = wallpaper.getName();
                if (name == null) name = "";

                statement.bindString(1, name);

                if (wallpaper.getAuthor() != null) {
                    statement.bindString(2, wallpaper.getAuthor());
                } else {
                    statement.bindNull(2);
                }

                statement.bindString(3, wallpaper.getUrl());
                statement.bindString(4, wallpaper.getThumbUrl());
                statement.bindString(5, wallpaper.getCategory());
                statement.bindString(6, TimeHelper.getLongDateTime());
                statement.execute();
            }
        }
    }
    mDatabase.get().mSQLiteDatabase.setTransactionSuccessful();
    mDatabase.get().mSQLiteDatabase.endTransaction();
}
 
Example 15
Source Project: candybar-library   File: Database.java    License: Apache License 2.0 4 votes vote down vote up
public void addWallpapers(List<?> list) {
    if (!openDatabase()) {
        LogUtil.e("Database error: addWallpapers() failed to open database");
        return;
    }

    String query = "INSERT OR IGNORE INTO " +TABLE_WALLPAPERS+ " (" +KEY_NAME+ "," +KEY_AUTHOR+ "," +KEY_URL+ ","
            +KEY_THUMB_URL+ "," +KEY_ADDED_ON+ ") VALUES (?,?,?,?,?);";
    SQLiteStatement statement = mDatabase.get().mSQLiteDatabase.compileStatement(query);
    mDatabase.get().mSQLiteDatabase.beginTransaction();

    for (int i = 0; i < list.size(); i++) {
        statement.clearBindings();

        Wallpaper wallpaper;
        if (list.get(i) instanceof Wallpaper) {
            wallpaper = (Wallpaper) list.get(i);
        } else {
            wallpaper = JsonHelper.getWallpaper(list.get(i));
        }

        if (wallpaper != null) {
            if (wallpaper.getURL() != null) {
                String name = wallpaper.getName();
                if (name == null) name = "";

                statement.bindString(1, name);

                if (wallpaper.getAuthor() != null) {
                    statement.bindString(2, wallpaper.getAuthor());
                } else {
                    statement.bindNull(2);
                }

                statement.bindString(3, wallpaper.getURL());
                statement.bindString(4, wallpaper.getThumbUrl());
                statement.bindString(5, TimeHelper.getLongDateTime());
                statement.execute();
            }
        }
    }
    mDatabase.get().mSQLiteDatabase.setTransactionSuccessful();
    mDatabase.get().mSQLiteDatabase.endTransaction();
}
 
Example 16
private static void addStringToStatement(SQLiteStatement statement, int index, String value) {
  if (value == null || value.equals("null")) statement.bindNull(index);
  else                                       statement.bindString(index, value);
}
 
Example 17
private static void addNullToStatement(SQLiteStatement statement, int index) {
  statement.bindNull(index);
}
 
Example 18
/**
 * Add multiple leq records
 * @param leqBatches List of Leq to add
 */
public void addLeqBatches(List<LeqBatch> leqBatches) {
    SQLiteDatabase database = storage.getWritableDatabase();
    try {
        database.beginTransaction();
        SQLiteStatement leqStatement = database.compileStatement(
                "INSERT INTO " + Storage.Leq.TABLE_NAME + "(" +
                        Storage.Leq.COLUMN_RECORD_ID + "," +
                        Storage.Leq.COLUMN_LEQ_UTC + "," +
                        Storage.Leq.COLUMN_LATITUDE  + "," +
                        Storage.Leq.COLUMN_LONGITUDE  + "," +
                        Storage.Leq.COLUMN_ALTITUDE  + "," +
                        Storage.Leq.COLUMN_ACCURACY  + "," +
                        Storage.Leq.COLUMN_LOCATION_UTC + "," +
                        Storage.Leq.COLUMN_SPEED + "," +
                        Storage.Leq.COLUMN_BEARING +
                        ") VALUES (?, ?,?,?,?,?,?,?,?)");
        SQLiteStatement leqValueStatement = database.compileStatement("INSERT INTO " +
                Storage.LeqValue.TABLE_NAME + " VALUES (?,?,?)");
        for(LeqBatch leqBatch : leqBatches) {
            Storage.Leq leq = leqBatch.getLeq();
            leqStatement.clearBindings();
            leqStatement.bindLong(1, leq.getRecordId());
            leqStatement.bindLong(2, leq.getLeqUtc());
            leqStatement.bindDouble(3, leq.getLatitude());
            leqStatement.bindDouble(4, leq.getLongitude());
            if(leq.getAltitude() != null) {
                leqStatement.bindDouble(5, leq.getAltitude());
            } else {
                leqStatement.bindNull(5);
            }
            leqStatement.bindDouble(6, leq.getAccuracy());
            leqStatement.bindDouble(7, leq.getLocationUTC());
            if(leq.getSpeed() != null) {
                leqStatement.bindDouble(8, leq.getSpeed());
            } else {
                leqStatement.bindNull(8);
            }
            if(leq.getBearing() != null) {
                leqStatement.bindDouble(9, leq.getBearing());
            } else {
                leqStatement.bindNull(9);
            }
            long leqId = leqStatement.executeInsert();
            for(Storage.LeqValue leqValue : leqBatch.getLeqValues()) {
                leqValueStatement.clearBindings();
                leqValueStatement.bindLong(1, leqId);
                leqValueStatement.bindLong(2, leqValue.getFrequency());
                leqValueStatement.bindDouble(3, leqValue.getSpl());
                leqValueStatement.execute();
            }
        }
        database.setTransactionSuccessful();
        database.endTransaction();
    } finally {
        database.close();
    }
}
 
Example 19
public void updateRecordUserInput(int recordId, String description, Short pleasantness,
                                  String[] tags, Uri photo_uri, String noisePartyTag) {

    SQLiteDatabase database = storage.getWritableDatabase();
    try {
        database.beginTransaction();
        SQLiteStatement recordStatement = database.compileStatement(
                "UPDATE " + Storage.Record.TABLE_NAME +
                        " SET "+ Storage.Record.COLUMN_DESCRIPTION + " = ?, " +
                        Storage.Record.COLUMN_PLEASANTNESS + " = ?, " +
                        Storage.Record.COLUMN_PHOTO_URI + " = ?, " +
                        Storage.Record.COLUMN_NOISEPARTY_TAG + " = ?" +
                        " WHERE " + Storage.Record.COLUMN_ID + " = ?");
        recordStatement.clearBindings();
        recordStatement.bindString(1, description);
        if(pleasantness != null) {
            recordStatement.bindLong(2, pleasantness);
        } else {
            recordStatement.bindNull(2);
        }
        recordStatement.bindString(3, photo_uri != null ? photo_uri.toString() : "");
        recordStatement.bindString(4, noisePartyTag);
        recordStatement.bindLong(5, recordId);
        recordStatement.executeUpdateDelete();
        database.delete(Storage.RecordTag.TABLE_NAME,
                Storage.RecordTag.COLUMN_RECORD_ID + " = ?" +
                        "", new String[] {String.valueOf(recordId)});
        SQLiteStatement tagStatement = database.compileStatement(
                "INSERT INTO " + Storage.RecordTag.TABLE_NAME +
                        "(" + Storage.RecordTag.COLUMN_RECORD_ID + ", " +
                        Storage.RecordTag.COLUMN_TAG_SYSTEM_NAME + ") " +
                        " VALUES (?, ?)");
        for(String sysTag : tags) {
            tagStatement.clearBindings();
            tagStatement.bindLong(1, recordId);
            tagStatement.bindString(2, sysTag);
            tagStatement.executeInsert();
        }
        database.setTransactionSuccessful();
        database.endTransaction();
    } finally {
        database.close();
    }
}
 
Example 20
private void bindArgs(SQLiteStatement stmt, Object[] args, FieldType[] argFieldTypes) throws SQLException {
	if (args == null) {
		return;
	}
	for (int i = 0; i < args.length; i++) {
		Object arg = args[i];
		if (arg == null) {
			stmt.bindNull(i + 1);
		} else {
			SqlType sqlType = argFieldTypes[i].getSqlType();
			switch (sqlType) {
				case STRING:
				case LONG_STRING:
				case CHAR:
					stmt.bindString(i + 1, arg.toString());
					break;
				case BOOLEAN:
				case BYTE:
				case SHORT:
				case INTEGER:
				case LONG:
					stmt.bindLong(i + 1, ((Number) arg).longValue());
					break;
				case FLOAT:
				case DOUBLE:
					stmt.bindDouble(i + 1, ((Number) arg).doubleValue());
					break;
				case BYTE_ARRAY:
				case SERIALIZABLE:
					stmt.bindBlob(i + 1, (byte[]) arg);
					break;
				case DATE:
					// this is mapped to a STRING under Android
				case BLOB:
					// this is only for derby serializable
				case BIG_DECIMAL:
					// this should be handled as a STRING
					throw new SQLException("Invalid Android type: " + sqlType);
				case UNKNOWN:
				default:
					throw new SQLException("Unknown sql argument type: " + sqlType);
			}
		}
	}
}