/** * 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); } }
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); } }
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(); } }
mSrc.getDb().getDatabase().execSQL("ATTACH '" + path + "' AS DST_DB"); .execSQL("INSERT INTO DST_DB.cards select * from cards where id in " + Utils.ids2str(cids)); Set<Long> nids = new HashSet<>(mSrc.getDb().queryColumn(Long.class, "select nid from cards where id in " + Utils.ids2str(cids), 0)); ArrayList<Long> uniqueNids = new ArrayList<>(nids); String strnids = Utils.ids2str(uniqueNids); mSrc.getDb().getDatabase().execSQL("INSERT INTO DST_DB.notes select * from notes where id in " + strnids); Timber.d("Copy history and revlog"); mSrc.getDb().getDatabase() .execSQL("insert into DST_DB.revlog select * from revlog where cid in " + Utils.ids2str(cids)); mSrc.getDb().getDatabase().execSQL("DETACH DST_DB"); dst.reopen(); } else { Timber.d("Detaching destination db and reopening"); mSrc.getDb().getDatabase().execSQL("DETACH DST_DB"); dst.reopen();
database.execSQL("CREATE TABLE tmp_" + EventEntity.TABLE_NAME + " (id INTEGER PRIMARY KEY NOT NULL, day_index INTEGER NOT NULL, start_time INTEGER, end_time INTEGER, room_name TEXT, slug TEXT, track_id INTEGER NOT NULL, abstract TEXT, description TEXT);"); database.execSQL("INSERT INTO tmp_" + EventEntity.TABLE_NAME + " SELECT * FROM " + EventEntity.TABLE_NAME); database.execSQL("DROP TABLE " + EventEntity.TABLE_NAME); database.execSQL("ALTER TABLE tmp_" + EventEntity.TABLE_NAME + " RENAME TO " + EventEntity.TABLE_NAME); database.execSQL("CREATE INDEX event_day_index_idx ON " + EventEntity.TABLE_NAME + " (day_index)"); database.execSQL("CREATE INDEX event_start_time_idx ON " + EventEntity.TABLE_NAME + " (start_time)"); database.execSQL("CREATE INDEX event_end_time_idx ON " + EventEntity.TABLE_NAME + " (end_time)"); database.execSQL("CREATE INDEX event_track_id_idx ON " + EventEntity.TABLE_NAME + " (track_id)"); database.execSQL("CREATE TABLE tmp_" + Link.TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, event_id INTEGER NOT NULL, url TEXT NOT NULL, description TEXT);"); database.execSQL("INSERT INTO tmp_" + Link.TABLE_NAME + " SELECT `rowid` AS id, event_id, url, description FROM " + Link.TABLE_NAME); database.execSQL("DROP TABLE " + Link.TABLE_NAME); database.execSQL("ALTER TABLE tmp_" + Link.TABLE_NAME + " RENAME TO " + Link.TABLE_NAME); database.execSQL("CREATE INDEX link_event_id_idx ON " + Link.TABLE_NAME + " (event_id)"); database.execSQL("CREATE TABLE tmp_" + Track.TABLE_NAME + " (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, type TEXT NOT NULL);"); database.execSQL("INSERT INTO tmp_" + Track.TABLE_NAME + " SELECT * FROM " + Track.TABLE_NAME); database.execSQL("DROP TABLE " + Track.TABLE_NAME); database.execSQL("ALTER TABLE tmp_" + Track.TABLE_NAME + " RENAME TO " + Track.TABLE_NAME); database.execSQL("CREATE UNIQUE INDEX track_main_idx ON " + Track.TABLE_NAME + " (name, type)"); database.execSQL("CREATE TABLE tmp_" + Day.TABLE_NAME + " (`index` INTEGER PRIMARY KEY NOT NULL, date INTEGER NOT NULL);"); database.execSQL("INSERT INTO tmp_" + Day.TABLE_NAME + " SELECT _index as `index`, date FROM " + Day.TABLE_NAME); database.execSQL("DROP TABLE " + Day.TABLE_NAME); database.execSQL("ALTER TABLE tmp_" + Day.TABLE_NAME + " RENAME TO " + Day.TABLE_NAME); database.execSQL("CREATE TABLE tmp_" + Bookmark.TABLE_NAME + " (event_id INTEGER PRIMARY KEY NOT NULL);");
try { database.execSQL("DROP INDEX index_scaleMeasurements_userId_datetime"); database.execSQL("ALTER TABLE scaleMeasurements RENAME TO scaleMeasurementsOld"); database.execSQL("ALTER TABLE scaleUsers RENAME TO scaleUsersOld"); database.execSQL("CREATE TABLE scaleMeasurements" + " (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + " userId INTEGER NOT NULL, enabled INTEGER NOT NULL," database.execSQL("CREATE TABLE scaleUsers " + "(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " + "username TEXT NOT NULL, birthday INTEGER NOT NULL, bodyHeight REAL NOT NULL, " database.execSQL("CREATE UNIQUE INDEX index_scaleMeasurements_userId_datetime" + " ON scaleMeasurements (userId, datetime)"); database.execSQL("INSERT INTO scaleMeasurements" + " SELECT id, userId, enabled, datetime, weight, fat, water, muscle," + " 0 AS visceralFat, lbw AS lbm, waist, hip, bone, 0 AS chest," database.execSQL("INSERT INTO scaleUsers" + " SELECT id, username, birthday, bodyHeight, scaleUnit, gender, initialWeight, goalWeight," + " goalDate, 0 AS measureUnit, 0 AS activityLevel FROM scaleUsersOld"); database.execSQL("DROP TABLE scaleMeasurementsOld"); database.execSQL("DROP TABLE scaleUsersOld");
try { database.execSQL("DROP INDEX index_scaleMeasurements_datetime"); database.execSQL("ALTER TABLE scaleMeasurements RENAME TO scaleMeasurementsOld"); database.execSQL("CREATE TABLE scaleMeasurements" + " (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + " userId INTEGER NOT NULL, enabled INTEGER NOT NULL," database.execSQL("CREATE UNIQUE INDEX index_scaleMeasurements_userId_datetime" + " ON scaleMeasurements (userId, datetime)"); database.execSQL("INSERT INTO scaleMeasurements" + " SELECT * FROM scaleMeasurementsOld" + " WHERE userId IN (SELECT id from scaleUsers)"); database.execSQL("DROP TABLE scaleMeasurementsOld");
try { database.execSQL("DROP INDEX index_scaleMeasurements_userId_datetime"); database.execSQL("ALTER TABLE scaleMeasurements RENAME TO scaleMeasurementsOld"); database.execSQL("CREATE TABLE scaleMeasurements" + " (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + " userId INTEGER NOT NULL, enabled INTEGER NOT NULL," database.execSQL("CREATE UNIQUE INDEX index_scaleMeasurements_userId_datetime" + " ON scaleMeasurements (userId, datetime)"); database.execSQL("INSERT INTO scaleMeasurements" + " SELECT id, userId, enabled, datetime, weight, fat, water, muscle," + " visceralFat, lbm, waist, hip, bone, chest," database.execSQL("DROP TABLE scaleMeasurementsOld");
@Override public void migrate(@NonNull SupportSQLiteDatabase db) { db.execSQL("CREATE TABLE IF NOT EXISTS `SillyEntity` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)"); } };
@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); }