Search in sources :

Example 1 with SupportSQLiteDatabase

use of android.arch.persistence.db.SupportSQLiteDatabase in project openScale by oliexdev.

the class DatabaseMigrationTest method migrate1To2.

@Test
public void migrate1To2() throws Exception {
    SupportSQLiteDatabase db = helper.createDatabase(TEST_DB, 1);
    ContentValues users = new ContentValues();
    for (int i = 1; i < 4; ++i) {
        users.put("id", i);
        users.put("username", String.format("test%d", i));
        users.put("bodyHeight", i * 50);
        users.put("scaleUnit", 0);
        users.put("gender", 0);
        users.put("initialWeight", i * 25);
        users.put("goalWeight", i * 20);
        assertNotSame(-1, db.insert("scaleUsers", SQLiteDatabase.CONFLICT_ABORT, users));
    }
    ContentValues measurement = new ContentValues();
    for (int i = 2; i < 5; ++i) {
        for (int j = 0; j < 2; ++j) {
            measurement.put("userId", i);
            measurement.put("enabled", j);
            measurement.put("comment", "a string");
            for (String type : new String[] { "weight", "fat", "water", "muscle", "lbw", "waist", "hip", "bone" }) {
                measurement.put(type, i * j);
            }
            assertNotSame(-1, db.insert("scaleMeasurements", SQLiteDatabase.CONFLICT_ABORT, measurement));
        }
    }
    // Prepare for the next version.
    db.close();
    // Re-open the database with version 2 and provide MIGRATION_1_2 as the migration process.
    db = helper.runMigrationsAndValidate(TEST_DB, 2, true, AppDatabase.MIGRATION_1_2);
    // MigrationTestHelper automatically verifies the schema changes.
    Cursor cursor = db.query("SELECT * FROM scaleMeasurements ORDER BY id, userId");
    assertEquals(2 * 2, cursor.getCount());
    cursor.moveToFirst();
    for (int i = 2; i < 4; ++i) {
        for (int j = 0; j < 2; ++j) {
            assertEquals(i, cursor.getInt(cursor.getColumnIndex("userId")));
            assertEquals(j, cursor.getInt(cursor.getColumnIndex("enabled")));
            assertEquals("a string", cursor.getString(cursor.getColumnIndex("comment")));
            for (String type : new String[] { "weight", "fat", "water", "muscle", "lbw", "waist", "hip", "bone" }) {
                assertEquals((float) i * j, cursor.getFloat(cursor.getColumnIndex(type)));
            }
            cursor.moveToNext();
        }
    }
    assertTrue(cursor.isAfterLast());
}
Also used : SupportSQLiteDatabase(android.arch.persistence.db.SupportSQLiteDatabase) ContentValues(android.content.ContentValues) Cursor(android.database.Cursor) Test(org.junit.Test)

Example 2 with SupportSQLiteDatabase

use of android.arch.persistence.db.SupportSQLiteDatabase in project Android-Debug-Database by amitshekhariitbhu.

the class RequestHandler method getDBListResponse.

private String getDBListResponse() {
    mDatabaseFiles = DatabaseFileProvider.getDatabaseFiles(mContext);
    if (mCustomDatabaseFiles != null) {
        mDatabaseFiles.putAll(mCustomDatabaseFiles);
    }
    Response response = new Response();
    if (mDatabaseFiles != null) {
        for (HashMap.Entry<String, Pair<File, String>> entry : mDatabaseFiles.entrySet()) {
            String[] dbEntry = { entry.getKey(), !entry.getValue().second.equals("") ? "true" : "false", "true" };
            response.rows.add(dbEntry);
        }
    }
    if (mRoomInMemoryDatabases != null) {
        for (HashMap.Entry<String, SupportSQLiteDatabase> entry : mRoomInMemoryDatabases.entrySet()) {
            String[] dbEntry = { entry.getKey(), "false", "false" };
            response.rows.add(dbEntry);
        }
    }
    response.rows.add(new String[] { Constants.APP_SHARED_PREFERENCES, "false", "false" });
    response.isSuccessful = true;
    return mGson.toJson(response);
}
Also used : Response(com.amitshekhar.model.Response) UpdateRowResponse(com.amitshekhar.model.UpdateRowResponse) TableDataResponse(com.amitshekhar.model.TableDataResponse) SupportSQLiteDatabase(android.arch.persistence.db.SupportSQLiteDatabase) HashMap(java.util.HashMap) Pair(android.util.Pair)

Example 3 with SupportSQLiteDatabase

use of android.arch.persistence.db.SupportSQLiteDatabase in project Android-Debug-Database by amitshekhariitbhu.

the class Utils method setInMemoryRoomDatabases.

public static void setInMemoryRoomDatabases(SupportSQLiteDatabase... database) {
    if (BuildConfig.DEBUG) {
        try {
            Class<?> debugDB = Class.forName("com.amitshekhar.DebugDB");
            Class[] argTypes = new Class[] { HashMap.class };
            HashMap<String, SupportSQLiteDatabase> inMemoryDatabases = new HashMap<>();
            // set your inMemory databases
            inMemoryDatabases.put("InMemoryOne.db", database[0]);
            Method setRoomInMemoryDatabase = debugDB.getMethod("setInMemoryRoomDatabases", argTypes);
            setRoomInMemoryDatabase.invoke(null, inMemoryDatabases);
        } catch (Exception ignore) {
        }
    }
}
Also used : SupportSQLiteDatabase(android.arch.persistence.db.SupportSQLiteDatabase) HashMap(java.util.HashMap) Method(java.lang.reflect.Method)

Example 4 with SupportSQLiteDatabase

use of android.arch.persistence.db.SupportSQLiteDatabase in project Android-Debug-Database by amitshekhariitbhu.

the class Utils method setInMemoryRoomDatabases.

public static void setInMemoryRoomDatabases(SupportSQLiteDatabase... database) {
    if (BuildConfig.DEBUG) {
        try {
            Class<?> debugDB = Class.forName("com.amitshekhar.DebugDB");
            Class[] argTypes = new Class[] { HashMap.class };
            HashMap<String, SupportSQLiteDatabase> inMemoryDatabases = new HashMap<>();
            // set your inMemory databases
            inMemoryDatabases.put("InMemoryOne.db", database[0]);
            Method setRoomInMemoryDatabase = debugDB.getMethod("setInMemoryRoomDatabases", argTypes);
            setRoomInMemoryDatabase.invoke(null, inMemoryDatabases);
        } catch (Exception ignore) {
        }
    }
}
Also used : SupportSQLiteDatabase(android.arch.persistence.db.SupportSQLiteDatabase) HashMap(java.util.HashMap) Method(java.lang.reflect.Method)

Example 5 with SupportSQLiteDatabase

use of android.arch.persistence.db.SupportSQLiteDatabase in project sqlbrite by square.

the class BriteDatabase method insert.

/**
 * Insert a row into the specified {@code table} and notify any subscribed queries.
 *
 * @see SupportSQLiteDatabase#insert(String, int, ContentValues)
 */
@WorkerThread
public long insert(@NonNull String table, @ConflictAlgorithm int conflictAlgorithm, @NonNull ContentValues values) {
    SupportSQLiteDatabase db = getWritableDatabase();
    if (logging) {
        log("INSERT\n  table: %s\n  values: %s\n  conflictAlgorithm: %s", table, values, conflictString(conflictAlgorithm));
    }
    long rowId = db.insert(table, conflictAlgorithm, values);
    if (logging)
        log("INSERT id: %s", rowId);
    if (rowId != -1) {
        // Only send a table trigger if the insert was successful.
        sendTableTrigger(Collections.singleton(table));
    }
    return rowId;
}
Also used : SupportSQLiteDatabase(android.arch.persistence.db.SupportSQLiteDatabase) WorkerThread(android.support.annotation.WorkerThread)

Aggregations

SupportSQLiteDatabase (android.arch.persistence.db.SupportSQLiteDatabase)8 WorkerThread (android.support.annotation.WorkerThread)3 HashMap (java.util.HashMap)3 Method (java.lang.reflect.Method)2 Test (org.junit.Test)2 ContentValues (android.content.ContentValues)1 Cursor (android.database.Cursor)1 Pair (android.util.Pair)1 Response (com.amitshekhar.model.Response)1 TableDataResponse (com.amitshekhar.model.TableDataResponse)1 UpdateRowResponse (com.amitshekhar.model.UpdateRowResponse)1