private StoragePersistOperations(Map<String, String> workingStorage, SQLiteDatabase database) { this.workingStorage = workingStorage; insertStatement = database.compileStatement( "INSERT INTO preferences_storage (primkey, value) VALUES (?, ?)"); deleteStatement = database.compileStatement( "DELETE FROM preferences_storage WHERE primkey = ?"); }
public SQLiteStatement getMarkAsCancelledStatement() { if (markAsCancelledStatement == null) { String sql = "UPDATE " + tableName + " SET " + DbOpenHelper.CANCELLED_COLUMN.columnName + " = 1 " + " WHERE " + primaryKeyColumnName + " = ? "; markAsCancelledStatement = db.compileStatement(sql); } return markAsCancelledStatement; }
public SQLiteStatement getCountStatement() { if (countStatement == null) { countStatement = db.compileStatement("SELECT COUNT(*) FROM " + tableName + " WHERE " + DbOpenHelper.RUNNING_SESSION_ID_COLUMN.columnName + " != ?"); } return countStatement; }
public SQLiteStatement getDeleteStatement() { if (deleteStatement == null) { deleteStatement = db.compileStatement("DELETE FROM " + tableName + " WHERE " + primaryKeyColumnName + " = ?"); } return deleteStatement; }
public SQLiteStatement getDeleteJobTagsStatement() { if (deleteJobTagsStatement == null) { deleteJobTagsStatement = db.compileStatement("DELETE FROM " + tagsTableName + " WHERE " + DbOpenHelper.TAGS_JOB_ID_COLUMN.columnName + "= ?"); } return deleteJobTagsStatement; }
public SQLiteStatement getOnJobFetchedForRunningStatement() { if (onJobFetchedForRunningStatement == null) { String sql = "UPDATE " + tableName + " SET " + DbOpenHelper.RUN_COUNT_COLUMN.columnName + " = ? , " + DbOpenHelper.RUNNING_SESSION_ID_COLUMN.columnName + " = ? " + " WHERE " + primaryKeyColumnName + " = ? "; onJobFetchedForRunningStatement = db.compileStatement(sql); } return onJobFetchedForRunningStatement; }
@Override public DatabaseStatement compileStatement(String sql) { return new StandardDatabaseStatement(delegate.compileStatement(sql)); }
@TargetApi(DatabaseConstants.MIN_API_LEVEL) private <T> T executeUpdateDelete( SQLiteDatabase database, String query, ExecuteResultHandler<T> handler) { SQLiteStatement statement = database.compileStatement(query); int count = statement.executeUpdateDelete(); return handler.handleUpdateDelete(count); }
private <T> T executeInsert( SQLiteDatabase database, String query, ExecuteResultHandler<T> handler) { SQLiteStatement statement = database.compileStatement(query); long count = statement.executeInsert(); return handler.handleInsert(count); }
SQLiteDatabase db = dbHelper.getWritableDatabase(); SQLiteStatement stmt = db.compileStatement("SELECT * FROM Country WHERE code = ?"); stmt.bindString(1, "US"); stmt.execute();
final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final SQLiteStatement statement = db.compileStatement(INSERT_QUERY); db.beginTransaction(); try { for(MyBean bean : list){ statement.clearBindings(); statement.bindString(1, bean.getName()); // rest of bindings statement.execute(); //or executeInsert() if id is needed } db.setTransactionSuccessful(); } finally { db.endTransaction(); }
public void insertUser(){ SQLiteDatabase db = dbHelper.getWritableDatabase(); String delSql = "DELETE FROM ACCOUNTS"; SQLiteStatement delStmt = db.compileStatement(delSql); delStmt.execute(); String sql = "INSERT INTO ACCOUNTS (account_id,account_name,account_image) VALUES(?,?,?)"; SQLiteStatement insertStmt = db.compileStatement(sql); insertStmt.clearBindings(); insertStmt.bindString(1, Integer.toString(this.accId)); insertStmt.bindString(2,this.accName); insertStmt.bindBlob(3, this.accImage); insertStmt.executeInsert(); db.close(); }
@Test(expected = SQLiteDoneException.class) public void simpleQueryForLongThrowsSQLiteDoneExceptionTest() throws Exception { //throw SQLiteDOneException if no rows returned. SQLiteStatement stmt = database.compileStatement("SELECT * FROM `countme` where `name`= 'cessationoftime'"); stmt.simpleQueryForLong(); }
@Before public void setUp() throws Exception { final File databasePath = ApplicationProvider.getApplicationContext().getDatabasePath("path"); databasePath.getParentFile().mkdirs(); database = SQLiteDatabase.openOrCreateDatabase(databasePath.getPath(), null); SQLiteStatement createStatement = database.compileStatement("CREATE TABLE `routine` (`id` INTEGER PRIMARY KEY AUTOINCREMENT , `name` VARCHAR , `lastUsed` INTEGER DEFAULT 0 , UNIQUE (`name`)) ;"); createStatement.execute(); SQLiteStatement createStatement2 = database.compileStatement("CREATE TABLE `countme` (`id` INTEGER PRIMARY KEY AUTOINCREMENT , `name` VARCHAR , `lastUsed` INTEGER DEFAULT 0 , UNIQUE (`name`)) ;"); createStatement2.execute(); }
@Test(expected = SQLiteDoneException.class) public void simpleQueryForStringThrowsSQLiteDoneExceptionTest() throws Exception { //throw SQLiteDOneException if no rows returned. SQLiteStatement stmt = database.compileStatement("SELECT * FROM `countme` where `name`= 'cessationoftime'"); assertThat(stmt.simpleQueryForString()).isEqualTo("0"); }
@Test public void testExecuteUpdateDelete() throws Exception { SQLiteStatement insertStatement = database.compileStatement("INSERT INTO `routine` (`name`) VALUES (?)"); insertStatement.bindString(1, "Hand Press"); long pkeyOne = insertStatement.executeInsert(); assertThat(pkeyOne).isEqualTo(1); SQLiteStatement updateStatement = database.compileStatement("UPDATE `routine` SET `name`=? WHERE `id`=?"); updateStatement.bindString(1, "Head Press"); updateStatement.bindLong(2, pkeyOne); assertThat(updateStatement.executeUpdateDelete()).isEqualTo(1); Cursor dataCursor = database.rawQuery("SELECT `name` FROM `routine`", null); assertThat(dataCursor.moveToNext()).isTrue(); assertThat(dataCursor.getString(0)).isEqualTo("Head Press"); }
@Test public void testExecuteInsertShouldCloseGeneratedKeysResultSet() throws Exception { // NOTE: // As a side-effect we will get "database locked" exception // on rollback if generatedKeys wasn't closed // // Don't know how suitable to use Mockito here, but // it will be a little bit simpler to test ShadowSQLiteStatement // if actualDBStatement will be mocked database.beginTransaction(); try { SQLiteStatement insertStatement = database.compileStatement("INSERT INTO `routine` " + "(`name` ,`lastUsed`) VALUES ('test',0)"); try { insertStatement.executeInsert(); } finally { insertStatement.close(); } } finally { database.endTransaction(); } }
@Before public void setUp() throws Exception { database = createDatabase("database.db"); SQLiteStatement createStatement = database.compileStatement( "CREATE TABLE `routine` (`id` INTEGER PRIMARY KEY AUTOINCREMENT , `name` VARCHAR , `lastUsed` INTEGER DEFAULT 0 , UNIQUE (`name`)) ;"); createStatement.execute(); conn = getSQLiteConnection(database); }
@Test public void testCloseShouldCloseUnderlyingPreparedStatement() throws Exception { SQLiteStatement insertStatement = database.compileStatement("INSERT INTO `routine` (`name`) VALUES (?)"); insertStatement.bindString(1, "Hand Press"); insertStatement.close(); try { insertStatement.executeInsert(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class); } } }
@Test public void simpleQueryTest() throws Exception { SQLiteStatement stmt = database.compileStatement("SELECT count(*) FROM `countme`"); assertThat(stmt.simpleQueryForLong()).isEqualTo(0L); assertThat(stmt.simpleQueryForString()).isEqualTo("0"); SQLiteStatement insertStatement = database.compileStatement("INSERT INTO `countme` (`name` ,`lastUsed` ) VALUES (?,?)"); insertStatement.bindString(1, "Leg Press"); insertStatement.bindLong(2, 0); insertStatement.executeInsert(); assertThat(stmt.simpleQueryForLong()).isEqualTo(1L); assertThat(stmt.simpleQueryForString()).isEqualTo("1"); insertStatement.bindString(1, "Bench Press"); insertStatement.bindLong(2, 1); insertStatement.executeInsert(); assertThat(stmt.simpleQueryForLong()).isEqualTo(2L); assertThat(stmt.simpleQueryForString()).isEqualTo("2"); }