@Override public void execSQL(String sql, Object[] bindArgs) throws SQLException { delegate.execSQL(sql, bindArgs);
@Override public void execSQL(String sql) throws SQLException { delegate.execSQL(sql);
@Override public void onCreate(SQLiteDatabase db) { db.execSQL( "CREATE TABLE " + APODContract.TABLE_NAME + " (" + APODContract.Columns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + APODContract.Columns.TITLE + " TEXT, " + APODContract.Columns.DESCRIPTION_IMAGE_URL + " TEXT, " + APODContract.Columns.DESCRIPTION_TEXT + " TEXT, " + APODContract.Columns.LARGE_IMAGE_URL + " TEXT " + ")");
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // If you need to add a column if (newVersion > oldVersion) { db.execSQL("ALTER TABLE foo ADD COLUMN new_column INTEGER DEFAULT 0");
@Override public void onCreate(final SQLiteDatabase db) { db.execSQL("CREATE TABLE orgs (id INTEGER PRIMARY KEY);"); db.execSQL("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, avatarurl TEXT);"); db.execSQL("CREATE TABLE repos (id INTEGER PRIMARY KEY, repoId INTEGER, orgId INTEGER, " + "name TEXT, ownerId INTEGER, private INTEGER, fork INTEGER, description TEXT, " + "forks INTEGER, watchers INTEGER, language TEXT, hasIssues INTEGER, " + "mirrorUrl TEXT, permissions_admin INTEGER, permissions_pull INTEGER, " + "permissions_push INTEGER);");
@Override public void onOpen(SQLiteDatabase db) { super.onOpen(db); if (!db.isReadOnly()) { // Enable foreign key constraints db.execSQL("PRAGMA foreign_keys=ON;");
@Transactional void successfulTransaction(SQLiteDatabase db) { db.execSQL("Some SQL");
@Transactional void successfulTransaction(SQLiteDatabase db) { db.execSQL("Some SQL");
private <T> T executeRawQuery(
SQLiteDatabase database,
String query,
ExecuteResultHandler<T> handler) {
database.execSQL(query);
return handler.handleRawQuery();
private void setupTable(SQLiteDatabase db, String table) {
db.execSQL("CREATE TABLE " + table + " (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"testVal INTEGER DEFAULT 0" +
");");
private void insertData(SQLiteDatabase db, String table, int[] values) {
@Override public void enableFeatures(@SQLiteOpenOptions int openOptions, SQLiteDatabase db) { if ((openOptions & ENABLE_WRITE_AHEAD_LOGGING) != 0) { db.enableWriteAheadLogging(); if ((openOptions & ENABLE_FOREIGN_KEY_CONSTRAINTS) != 0) { db.execSQL("PRAGMA foreign_keys = ON");
@Test public void testSQLWithLocalizedOrUnicodeCollatorShouldBeSortedAsNoCase() throws Exception { database.execSQL("insert into routine(name) values ('الصحافة اليدوية')"); database.execSQL("insert into routine(name) values ('Hand press 1')"); database.execSQL("insert into routine(name) values ('hand press 2')"); database.execSQL("insert into routine(name) values ('Hand press 3')"); List<String> expected = Arrays.asList("Hand press 1", "hand press 2", "Hand press 3", "الصحافة اليدوية" ); String sqlLocalized = "SELECT `name` FROM `routine` ORDER BY `name` collate localized"; String sqlUnicode = "SELECT `name` FROM `routine` ORDER BY `name` collate unicode"; assertThat(simpleQueryForList(database, sqlLocalized)).isEqualTo(expected); assertThat(simpleQueryForList(database, sqlUnicode)).isEqualTo(expected);
@Test public void shouldThrowWhenForeignKeysConstraintIsViolated() { database.execSQL("CREATE TABLE master (master_value INTEGER)"); database.execSQL("CREATE TABLE slave (master_value INTEGER REFERENCES master(master_value))"); database.execSQL("PRAGMA foreign_keys=ON"); try { database.execSQL("INSERT INTO slave(master_value) VALUES (1)"); fail("Foreign key constraint is violated but exception is not thrown"); } catch (SQLiteException e) { assertThat(e.getCause()).hasMessageThat().contains("foreign");
@Test public void testGetClob() throws Exception { String sql = "UPDATE table_name set clob_value=? where id=1234"; String s = "Don't CLOBber my data, please. Thank you."; database.execSQL(sql, new Object[]{s}); assertThat(cursor.moveToFirst()).isTrue(); String actual = cursor.getString(6); assertThat(s).isEqualTo(actual);
@Test public void shouldStoreGreatBigHonkingIntegersCorrectly() throws Exception { database.execSQL("INSERT INTO table_name(big_int) VALUES(1234567890123456789);"); Cursor cursor = database.query("table_name", new String[]{"big_int"}, null, null, null, null, null); assertThat(cursor.moveToFirst()).isTrue(); assertEquals(1234567890123456789L, cursor.getLong(0));
@Test public void testGetNumbersFromBlobField() throws Exception { database.execSQL("update table_name set name = '1.2'"); assertThat(cursor.moveToFirst()).isTrue(); assertThat(cursor.getInt(1)).isEqualTo(1); assertThat(cursor.getDouble(1)).isEqualTo(1.2d); assertThat(cursor.getFloat(1)).isEqualTo(1.2f);
@Test public void testGetNullNumberValues() throws Exception { String sql = "UPDATE table_name set long_value=NULL, float_value=NULL, double_value=NULL"; database.execSQL(sql); assertThat(cursor.moveToFirst()).isTrue(); assertThat(cursor.getType(2)).isEqualTo(Cursor.FIELD_TYPE_NULL); assertThat(cursor.getLong(2)).isEqualTo(0); assertThat(cursor.getType(3)).isEqualTo(Cursor.FIELD_TYPE_NULL); assertThat(cursor.getFloat(3)).isEqualTo(0f); assertThat(cursor.getType(4)).isEqualTo(Cursor.FIELD_TYPE_NULL); assertThat(cursor.getDouble(4)).isEqualTo(0d);