本文整理汇总了Java中android.arch.persistence.db.SupportSQLiteDatabase类的典型用法代码示例。如果您正苦于以下问题:Java SupportSQLiteDatabase类的具体用法?Java SupportSQLiteDatabase怎么用?Java SupportSQLiteDatabase使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SupportSQLiteDatabase类属于android.arch.persistence.db包,在下文中一共展示了SupportSQLiteDatabase类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: rekey
import android.arch.persistence.db.SupportSQLiteDatabase; //导入依赖的package包/类
@Test
public void rekey() throws IOException {
SafeHelperFactory factory=
SafeHelperFactory.fromUser(new SpannableStringBuilder("sekrit"));
SupportSQLiteOpenHelper helper=
factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME, 1,
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, 1,
new Callback(1));
db=helper.getWritableDatabase();
assertUpdatedContent(db);
}
示例2: migrationFrom2To4_containsCorrectData
import android.arch.persistence.db.SupportSQLiteDatabase; //导入依赖的package包/类
@Test
public void migrationFrom2To4_containsCorrectData() throws IOException {
// Create the database with version 2
SupportSQLiteDatabase db = mMigrationTestHelper.createDatabase(TEST_DB_NAME, 2);
// Insert some data
insertUser(1, USER.getUserName(), db);
//Prepare for the next version
db.close();
// Re-open the database with version 4 and provide
// MIGRATION_2_3 and MIGRATION_3_4 as the migration process.
mMigrationTestHelper.runMigrationsAndValidate(TEST_DB_NAME, 4, true,
MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4);
// MigrationTestHelper automatically verifies the schema changes, but not the data validity
// Validate that the data was migrated properly.
User dbUser = getMigratedRoomDatabase().userDao().getUser();
assertEquals(dbUser.getId(), "1");
assertEquals(dbUser.getUserName(), USER.getUserName());
// The date was missing in version 2, so it should be null in version 3
assertEquals(dbUser.getDate(), null);
}
示例3: migrationFrom3To4_containsCorrectData
import android.arch.persistence.db.SupportSQLiteDatabase; //导入依赖的package包/类
@Test
public void migrationFrom3To4_containsCorrectData() throws IOException {
// Create the database with version 3
SupportSQLiteDatabase db = mMigrationTestHelper.createDatabase(TEST_DB_NAME, 3);
// db has schema version 3. Insert some data
insertUser(1, USER.getUserName(), DateConverter.toTimestamp(USER.getDate()), db);
//Prepare for the next version
db.close();
// Re-open the database with version 4 and provide
// MIGRATION_2_3 and MIGRATION_3_4 as the migration process.
mMigrationTestHelper.runMigrationsAndValidate(TEST_DB_NAME, 4, true,
MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4);
// MigrationTestHelper automatically verifies the schema changes, but not the data validity
// Validate that the data was migrated properly.
User dbUser = getMigratedRoomDatabase().userDao().getUser();
assertEquals(dbUser.getId(), "1");
assertEquals(dbUser.getUserName(), USER.getUserName());
assertEquals(dbUser.getDate(), USER.getDate());
}
示例4: startInVersion4_containsCorrectData
import android.arch.persistence.db.SupportSQLiteDatabase; //导入依赖的package包/类
@Test
public void startInVersion4_containsCorrectData() throws IOException {
// Create the database with version 4
SupportSQLiteDatabase db = mMigrationTestHelper.createDatabase(TEST_DB_NAME, 4);
// insert some data
insertUser(USER.getId(), USER.getUserName(), DateConverter.toTimestamp(USER.getDate()), db);
db.close();
// Get the latest, migrated, version of the database
UsersDatabase usersDatabase = getMigratedRoomDatabase();
// verify that the data is correct
User dbUser = getMigratedRoomDatabase().userDao().getUser();
assertEquals(dbUser.getId(), USER.getId());
assertEquals(dbUser.getUserName(), USER.getUserName());
assertEquals(dbUser.getDate(), USER.getDate());
}
示例5: startInVersion2_containsCorrectData
import android.arch.persistence.db.SupportSQLiteDatabase; //导入依赖的package包/类
@Test
public void startInVersion2_containsCorrectData() throws IOException {
// Create the database with version 2
SupportSQLiteDatabase db = mMigrationTestHelper.createDatabase(TEST_DB_NAME, 2);
// db has schema version 2. insert some data
insertUser(USER, db);
db.close();
// open the db with Room
UsersDatabase usersDatabase = getMigratedRoomDatabase();
// verify that the data is correct
User dbUser = usersDatabase.userDao().getUser();
assertEquals(dbUser.getId(), USER.getId());
assertEquals(dbUser.getUserName(), USER.getUserName());
}
示例6: migrationFrom2To3_containsCorrectData
import android.arch.persistence.db.SupportSQLiteDatabase; //导入依赖的package包/类
@Test
public void migrationFrom2To3_containsCorrectData() throws IOException {
// Create the database in version 2
SupportSQLiteDatabase db = mMigrationTestHelper.createDatabase(TEST_DB_NAME, 2);
// Insert some data
insertUser(USER.getId(), USER.getUserName(), db);
//Prepare for the next version
db.close();
// Re-open the database with version 3 and provide MIGRATION_1_2 and
// MIGRATION_2_3 as the migration process.
mMigrationTestHelper.runMigrationsAndValidate(TEST_DB_NAME, 3, true,
MIGRATION_1_2, MIGRATION_2_3);
// MigrationTestHelper automatically verifies the schema changes, but not the data validity
// Validate that the data was migrated properly.
User dbUser = getMigratedRoomDatabase().userDao().getUser();
assertEquals(dbUser.getId(), USER.getId());
assertEquals(dbUser.getUserName(), USER.getUserName());
// The date was missing in version 2, so it should be null in version 3
assertEquals(dbUser.getDate(), null);
}
示例7: startInVersion3_containsCorrectData
import android.arch.persistence.db.SupportSQLiteDatabase; //导入依赖的package包/类
@Test
public void startInVersion3_containsCorrectData() throws IOException {
// Create the database with version 3
SupportSQLiteDatabase db = mMigrationTestHelper.createDatabase(TEST_DB_NAME, 3);
// insert some data
insertUser(USER, db);
db.close();
// open the db with Room.
UsersDatabase usersDatabase = getMigratedRoomDatabase();
// verify that the data is correct
User dbUser = usersDatabase.userDao().getUser();
assertEquals(dbUser.getId(), USER.getId());
assertEquals(dbUser.getUserName(), USER.getUserName());
assertEquals(dbUser.getDate(), USER.getDate());
}
示例8: migrate
import android.arch.persistence.db.SupportSQLiteDatabase; //导入依赖的package包/类
@Override
public void migrate(SupportSQLiteDatabase database) {
// SQLite supports a limited operations for ALTER.
// Changing the type of a column is not directly supported, so this is what we need
// to do:
// Create the new table
database.execSQL(
"CREATE TABLE users_new (userid TEXT NOT NULL,"
+ "username TEXT,"
+ "last_update INTEGER,"
+ "PRIMARY KEY(userid))");
// Copy the data
database.execSQL("INSERT INTO users_new (userid, username, last_update) "
+ "SELECT userid, username, last_update "
+ "FROM users");
// Remove the old table
database.execSQL("DROP TABLE users");
// Change the table name to the correct one
database.execSQL("ALTER TABLE users_new RENAME TO users");
}
示例9: buildDatabase
import android.arch.persistence.db.SupportSQLiteDatabase; //导入依赖的package包/类
/**
* Build the database. {@link Builder#build()} only sets up the database configuration and
* creates a new instance of the database.
* The SQLite database is only created when it's accessed for the first time.
*/
private static AppDatabase buildDatabase(final Context appContext,
final AppExecutors executors) {
return Room.databaseBuilder(appContext, AppDatabase.class, DATABASE_NAME)
.addCallback(new Callback() {
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
super.onCreate(db);
executors.diskIO().execute(() -> {
// Add a delay to simulate a long-running operation
addDelay();
// Generate the data for pre-population
AppDatabase database = AppDatabase.getInstance(appContext, executors);
List<ProductEntity> products = DataGenerator.generateProducts();
List<CommentEntity> comments =
DataGenerator.generateCommentsForProducts(products);
insertData(database, products, comments);
// notify that the database was created and it's ready to be used
database.setDatabaseCreated();
});
}
}).build();
}
示例10: executeUpgradeScripts
import android.arch.persistence.db.SupportSQLiteDatabase; //导入依赖的package包/类
private void executeUpgradeScripts(SupportSQLiteDatabase db, int oldVersion, int newVersion) {
try {
if (SqliteMagic.LOGGING_ENABLED) {
LogUtil.logDebug("Executing upgrade scripts");
}
final AssetManager assets = context.getAssets();
for (int i = oldVersion; i < newVersion; i++) {
final String fileName = (i + 1) + ".sql";
if (SqliteMagic.LOGGING_ENABLED) {
LogUtil.logDebug("Executing script %s", fileName);
}
final BufferedReader bfr = new BufferedReader(new InputStreamReader(assets.open(fileName)));
String sql;
while ((sql = bfr.readLine()) != null) {
db.execSQL(sql);
}
bfr.close();
}
} catch (IOException ioe) {
LogUtil.logError("Error executing upgrade scripts");
throw new RuntimeException(ioe);
}
}
示例11: insert
import android.arch.persistence.db.SupportSQLiteDatabase; //导入依赖的package包/类
/**
* 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;
}
示例12: delete
import android.arch.persistence.db.SupportSQLiteDatabase; //导入依赖的package包/类
/**
* Delete rows from the specified {@code table} and notify any subscribed queries. This method
* will not trigger a notification if no rows were deleted.
*
* @see SupportSQLiteDatabase#delete(String, String, Object[])
*/
@WorkerThread
public int delete(@NonNull String table, @Nullable String whereClause,
@Nullable String... whereArgs) {
SupportSQLiteDatabase db = getWritableDatabase();
if (logging) {
log("DELETE\n table: %s\n whereClause: %s\n whereArgs: %s", table, whereClause,
Arrays.toString(whereArgs));
}
int rows = db.delete(table, whereClause, whereArgs);
if (logging) log("DELETE affected %s %s", rows, rows != 1 ? "rows" : "row");
if (rows > 0) {
// Only send a table trigger if rows were affected.
sendTableTrigger(Collections.singleton(table));
}
return rows;
}
示例13: update
import android.arch.persistence.db.SupportSQLiteDatabase; //导入依赖的package包/类
/**
* Update rows in the specified {@code table} and notify any subscribed queries. This method
* will not trigger a notification if no rows were updated.
*
* @see SupportSQLiteDatabase#update(String, int, ContentValues, String, Object[])
*/
@WorkerThread
public int update(@NonNull String table, @ConflictAlgorithm int conflictAlgorithm,
@NonNull ContentValues values, @Nullable String whereClause, @Nullable String... whereArgs) {
SupportSQLiteDatabase db = getWritableDatabase();
if (logging) {
log("UPDATE\n table: %s\n values: %s\n whereClause: %s\n whereArgs: %s\n conflictAlgorithm: %s",
table, values, whereClause, Arrays.toString(whereArgs),
conflictString(conflictAlgorithm));
}
int rows = db.update(table, conflictAlgorithm, values, whereClause, whereArgs);
if (logging) log("UPDATE affected %s %s", rows, rows != 1 ? "rows" : "row");
if (rows > 0) {
// Only send a table trigger if rows were affected.
sendTableTrigger(Collections.singleton(table));
}
return rows;
}
示例14: AutoContentProvider
import android.arch.persistence.db.SupportSQLiteDatabase; //导入依赖的package包/类
public AutoContentProvider(ContentHelper contentHelper, Logger logger, SupportSQLiteOpenHelperFactoryProvider supportSQLiteOpenHelperFactoryProvider) {
this.logger = logger != null ? logger : Logger.EmptyLogger.INSTANCE;
this.contentHelper = contentHelper;
this.supportSQLiteOpenHelperFactoryProvider = supportSQLiteOpenHelperFactoryProvider;
defaultCallback = new SupportSQLiteOpenHelper.Callback(contentHelper.DATABASE_VERSION) {
@Override
public void onCreate(SupportSQLiteDatabase db) {
onCreateDataBase(db);
}
@Override
public void onUpgrade(SupportSQLiteDatabase db, int oldVersion, int newVersion) {
onUpgradeDatabase(db, oldVersion, newVersion);
}
};
}
示例15: onUpgradeDatabase
import android.arch.persistence.db.SupportSQLiteDatabase; //导入依赖的package包/类
@SuppressWarnings("unused")
protected void onUpgradeDatabase(SupportSQLiteDatabase db, int oldVersion, int newVersion) {
final Cursor c = db.query("SELECT 'DROP TABLE ' || name || ';' AS cmd FROM sqlite_master WHERE type='table' AND name<>'android_metadata'");
final ArrayList<String> commands = new ArrayList<>();
if (c.moveToFirst()) {
do {
commands.add(c.getString(0));
} while (c.moveToNext());
}
c.close();
for (String command : commands) {
try {
logger.LogD(clazz, "*onUpgradeDatabase*: " + command);
db.execSQL(command);
} catch (Exception e) {
logger.LogE(clazz, e);
}
}
onCreateDataBase(db);
}