public void executeMany(String sql, List<Object[]> list) { mMod = true; mDatabase.beginTransaction(); try { for (Object[] o : list) { mDatabase.execSQL(sql, o); } mDatabase.setTransactionSuccessful(); } finally { mDatabase.endTransaction(); } }
/** * Closes a previously opened database connection. */ public void close() { try { mDatabase.close(); Timber.d("Database %s closed = %s", mDatabase.getPath(), !mDatabase.isOpen()); } catch (Exception e) { // The pre-framework requery API ate this exception, but the framework API exposes it. // We may want to propagate it in the future, but for now maintain the old API and log. Timber.e(e, "Failed to close database %s", this.getDatabase().getPath()); } }
@Override public void disableDatabaseWriteAheadLogging(SupportSQLiteDatabase db) { db.query("PRAGMA journal_mode = DELETE", null); }
public synchronized void close(boolean save) { if (mDb != null) { try { SupportSQLiteDatabase db = mDb.getDatabase(); if (save) { db.beginTransaction(); try { save(); db.setTransactionSuccessful(); } finally { db.endTransaction(); } } else { if (db.inTransaction()) { db.endTransaction(); } } } catch (RuntimeException e) { AnkiDroidApp.sendExceptionReport(e, "closeDB"); } if (!mServer) { CompatHelper.getCompat().disableDatabaseWriteAheadLogging(mDb.getDatabase()); } mDb.close(); mDb = null; mMedia.close(); _closeLog(); Timber.i("Collection closed"); } }
private void answerCard(Collection col, Sched sched, Card cardToAnswer, int ease, long timeTaken) { try { DB db = col.getDb(); db.getDatabase().beginTransaction(); try { if (cardToAnswer != null) { if(timeTaken != -1){ cardToAnswer.setTimerStarted(Utils.now()-timeTaken/1000); } sched.answerCard(cardToAnswer, ease); } db.getDatabase().setTransactionSuccessful(); } finally { db.getDatabase().endTransaction(); } } catch (RuntimeException e) { Timber.e(e, "answerCard - RuntimeException on answering card"); AnkiDroidApp.sendExceptionReport(e, "doInBackgroundAnswerCard"); return; } }
users.put("initialWeight", i * 25); users.put("goalWeight", i * 20); assertNotSame(-1, db.insert("scaleUsers", SQLiteDatabase.CONFLICT_ABORT, users)); assertNotSame(-1, db.insert("scaleMeasurements", SQLiteDatabase.CONFLICT_ABORT, measurement)); db.close(); Cursor cursor = db.query("SELECT * FROM scaleMeasurements ORDER BY id, userId"); assertEquals(2 * 2, cursor.getCount());
/** * WARNING: This is a convenience method that splits SQL scripts into separate queries with semicolons (;) * as the delimiter. Only use this method on internal functions where we can guarantee that the script does * not contain any non-statement-terminating semicolons. */ public void executeScript(String sql) { mMod = true; String[] queries = sql.split(";"); for(String query : queries) { mDatabase.execSQL(query); } }
int totalTasks = (mModels.all().size() * 4) + 18; // 4 things are in all-models loops, 18 things are one-offs try { mDb.getDatabase().beginTransaction(); try { save(); try { fixIntegrityProgress(progressCallback, currentTask++, totalTasks); cur = mDb.getDatabase().query("select id, flds from notes where mid = " + m.getLong("id"), null); while (cur.moveToNext()) { String flds = cur.getString(1); + " WHERE id IN " + Utils.ids2str(Utils.arrayList2array(ids))); mDb.getDatabase().setTransactionSuccessful(); throw new RuntimeException(e); } finally { mDb.getDatabase().endTransaction();
@Test public void wal() throws IOException { SafeHelperFactory factory= SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE)); SupportSQLiteOpenHelper helper= factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME, new Callback(1)); SupportSQLiteDatabase db=helper.getWritableDatabase(); assertFalse(db.isWriteAheadLoggingEnabled()); assertTrue(db.enableWriteAheadLogging()); assertTrue(db.isWriteAheadLoggingEnabled()); db.close(); factory=SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE)); helper= factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME, new Callback(1)); db=helper.getWritableDatabase(); assertTrue(db.isWriteAheadLoggingEnabled()); db.disableWriteAheadLogging(); assertFalse(db.isWriteAheadLoggingEnabled()); db.close(); }
@Test public void rekey() throws IOException { SafeHelperFactory factory= SafeHelperFactory.fromUser(new SpannableStringBuilder("sekrit")); SupportSQLiteOpenHelper helper= factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME, new Callback(1)); SupportSQLiteDatabase db=helper.getWritableDatabase(); assertOriginalContent(db); SafeHelperFactory.rekey(db, new SpannableStringBuilder(PASSPHRASE)); assertOriginalContent(db); db.execSQL("UPDATE foo SET bar=?, goo=?", new Object[] {3, "four"}); assertUpdatedContent(db); db.close(); factory=SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE)); helper=factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME, new Callback(1)); db=helper.getWritableDatabase(); assertUpdatedContent(db); }
@Test(expected = net.sqlcipher.database.SQLiteException.class) public void defaultBehavior() throws IOException { assertTrue(getDbFile().exists()); SafeHelperFactory factory= SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE)); SupportSQLiteOpenHelper helper= factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME, new Callback(1)); SupportSQLiteDatabase db=helper.getReadableDatabase(); db.close(); }
@Override public void disableDatabaseWriteAheadLogging(SupportSQLiteDatabase db) { db.disableWriteAheadLogging(); }
/** insert must always be called via DB in order to mark the db as changed */ public long insert(String table, ContentValues values) { mMod = true; return getDatabase().insert(table, SQLiteDatabase.CONFLICT_NONE, values); }
private void buryOrSuspendCard(Collection col, Sched sched, Card card, boolean bury) { try { DB db = col.getDb(); db.getDatabase().beginTransaction(); try { if (card != null) { if(bury) { // bury sched.buryCards(new long[] {card.getId()}); } else { // suspend sched.suspendCards(new long[] {card.getId()}); } } db.getDatabase().setTransactionSuccessful(); } finally { db.getDatabase().endTransaction(); } } catch (RuntimeException e) { Timber.e(e, "buryOrSuspendCard - RuntimeException on burying or suspending card"); AnkiDroidApp.sendExceptionReport(e, "doInBackgroundBurySuspendCard"); return; } }
users.put("initialWeight", i * 25); users.put("goalWeight", i * 20); assertNotSame(-1, db.insert("scaleUsers", SQLiteDatabase.CONFLICT_ABORT, users)); assertNotSame(-1, db.insert("scaleMeasurements", SQLiteDatabase.CONFLICT_ABORT, measurement)); db.close(); assertEquals(3, db.query("SELECT * FROM scaleUsers WHERE measureUnit = 0").getCount()); assertEquals(3, db.query("SELECT * FROM scaleUsers WHERE activityLevel = 0").getCount()); Cursor cursor = db.query("SELECT * FROM scaleUsers ORDER BY id"); cursor = db.query("SELECT * FROM scaleMeasurements ORDER BY id, userId"); assertEquals(2 * 2, cursor.getCount());
public void execute(String sql, Object[] object) { String s = sql.trim().toLowerCase(Locale.US); // mark modified? for (String mo : MOD_SQLS) { if (s.startsWith(mo)) { mMod = true; break; } } if (object == null) { this.getDatabase().execSQL(sql); } else { this.getDatabase().execSQL(sql, object); } }
private void dekey(Callable<?> decrypter) throws Exception { SafeHelperFactory factory= SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE)); SupportSQLiteOpenHelper helper= factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME, new Callback(1)); SupportSQLiteDatabase db=helper.getWritableDatabase(); assertOriginalContent(db); db.close(); final Context ctxt=InstrumentationRegistry.getTargetContext(); decrypter.call(); SQLiteDatabase plainDb= SQLiteDatabase.openDatabase(ctxt.getDatabasePath(DB_NAME).getAbsolutePath(), null, SQLiteDatabase.OPEN_READWRITE); assertOriginalContent(plainDb); plainDb.close(); }
@Override public void disableDatabaseWriteAheadLogging(SupportSQLiteDatabase db) { db.disableWriteAheadLogging(); }
public long queryLongScalar(String query) { Cursor cursor = null; long scalar; try { cursor = mDatabase.query(query, null); if (!cursor.moveToNext()) { return 0; } scalar = cursor.getLong(0); } finally { if (cursor != null) { cursor.close(); } } return scalar; }
private TaskData doInBackgroundAddNote(TaskData[] params) { Timber.d("doInBackgroundAddNote"); Note note = params[0].getNote(); Collection col = CollectionHelper.getInstance().getCol(mContext); try { DB db = col.getDb(); db.getDatabase().beginTransaction(); try { publishProgress(new TaskData(col.addNote(note))); db.getDatabase().setTransactionSuccessful(); } finally { db.getDatabase().endTransaction(); } } catch (RuntimeException e) { Timber.e(e, "doInBackgroundAddNote - RuntimeException on adding fact"); AnkiDroidApp.sendExceptionReport(e, "doInBackgroundAddNote"); return new TaskData(false); } return new TaskData(true); }