android.database.sqlite.SQLiteOpenHelper#onUpgrade ( )源码实例Demo

下面列出了android.database.sqlite.SQLiteOpenHelper#onUpgrade ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: android-dev-challenge   文件: DatabaseTest.java
/**
 * Tests that onUpgrade works by inserting 2 rows then calling onUpgrade and verifies that the
 * database has been successfully dropped and recreated by checking that the database is there
 * but empty
 * @throws Exception in case the constructor hasn't been implemented yet
 */
@Test
public void upgrade_database_test() throws Exception{

    /* Insert 2 rows before we upgrade to check that we dropped the database correctly */

    /* Use reflection to try to run the correct constructor whenever implemented */
    SQLiteOpenHelper dbHelper =
            (SQLiteOpenHelper) mDbHelperClass.getConstructor(Context.class).newInstance(mContext);

    /* Use WaitlistDbHelper to get access to a writable database */
    SQLiteDatabase database = dbHelper.getWritableDatabase();

    ContentValues testValues = new ContentValues();
    testValues.put(WaitlistContract.WaitlistEntry.COLUMN_GUEST_NAME, "test name");
    testValues.put(WaitlistContract.WaitlistEntry.COLUMN_PARTY_SIZE, 99);

    /* Insert ContentValues into database and get first row ID back */
    long firstRowId = database.insert(
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            null,
            testValues);

    /* Insert ContentValues into database and get another row ID back */
    long secondRowId = database.insert(
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            null,
            testValues);

    dbHelper.onUpgrade(database, 0, 1);
    database = dbHelper.getReadableDatabase();

    /* This Cursor will contain the names of each table in our database */
    Cursor tableNameCursor = database.rawQuery(
            "SELECT name FROM sqlite_master WHERE type='table' AND name='" +
                    WaitlistContract.WaitlistEntry.TABLE_NAME + "'",
            null);

    assertTrue(tableNameCursor.getCount() == 1);

    /*
     * Query the database and receive a Cursor. A Cursor is the primary way to interact with
     * a database in Android.
     */
    Cursor wCursor = database.query(
            /* Name of table on which to perform the query */
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null,
            /* Values for "where" clause */
            null,
            /* Columns to group by */
            null,
            /* Columns to filter by row groups */
            null,
            /* Sort order to return in Cursor */
            null);

    /* Cursor.moveToFirst will return false if there are no records returned from your query */

    assertFalse("Database doesn't seem to have been dropped successfully when upgrading",
            wCursor.moveToFirst());

    tableNameCursor.close();
    database.close();
}
 
源代码2 项目: android-dev-challenge   文件: DatabaseTest.java
/**
 * Tests that onUpgrade works by inserting 2 rows then calling onUpgrade and verifies that the
 * database has been successfully dropped and recreated by checking that the database is there
 * but empty
 * @throws Exception in case the constructor hasn't been implemented yet
 */
@Test
public void upgrade_database_test() throws Exception{

    /* Insert 2 rows before we upgrade to check that we dropped the database correctly */

    /* Use reflection to try to run the correct constructor whenever implemented */
    SQLiteOpenHelper dbHelper =
            (SQLiteOpenHelper) mDbHelperClass.getConstructor(Context.class).newInstance(mContext);

    /* Use WaitlistDbHelper to get access to a writable database */
    SQLiteDatabase database = dbHelper.getWritableDatabase();

    ContentValues testValues = new ContentValues();
    testValues.put(WaitlistContract.WaitlistEntry.COLUMN_GUEST_NAME, "test name");
    testValues.put(WaitlistContract.WaitlistEntry.COLUMN_PARTY_SIZE, 99);

    /* Insert ContentValues into database and get first row ID back */
    long firstRowId = database.insert(
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            null,
            testValues);

    /* Insert ContentValues into database and get another row ID back */
    long secondRowId = database.insert(
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            null,
            testValues);

    dbHelper.onUpgrade(database, 0, 1);
    database = dbHelper.getReadableDatabase();

    /* This Cursor will contain the names of each table in our database */
    Cursor tableNameCursor = database.rawQuery(
            "SELECT name FROM sqlite_master WHERE type='table' AND name='" +
                    WaitlistContract.WaitlistEntry.TABLE_NAME + "'",
            null);

    assertTrue(tableNameCursor.getCount() == 1);

    /*
     * Query the database and receive a Cursor. A Cursor is the primary way to interact with
     * a database in Android.
     */
    Cursor wCursor = database.query(
            /* Name of table on which to perform the query */
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null,
            /* Values for "where" clause */
            null,
            /* Columns to group by */
            null,
            /* Columns to filter by row groups */
            null,
            /* Sort order to return in Cursor */
            null);

    /* Cursor.moveToFirst will return false if there are no records returned from your query */

    assertFalse("Database doesn't seem to have been dropped successfully when upgrading",
            wCursor.moveToFirst());

    tableNameCursor.close();
    database.close();
}
 
源代码3 项目: android-dev-challenge   文件: DatabaseTest.java
/**
 * Tests that onUpgrade works by inserting 2 rows then calling onUpgrade and verifies that the
 * database has been successfully dropped and recreated by checking that the database is there
 * but empty
 * @throws Exception in case the constructor hasn't been implemented yet
 */
@Test
public void upgrade_database_test() throws Exception{

    /* Insert 2 rows before we upgrade to check that we dropped the database correctly */

    /* Use reflection to try to run the correct constructor whenever implemented */
    SQLiteOpenHelper dbHelper =
            (SQLiteOpenHelper) mDbHelperClass.getConstructor(Context.class).newInstance(mContext);

    /* Use WaitlistDbHelper to get access to a writable database */
    SQLiteDatabase database = dbHelper.getWritableDatabase();

    ContentValues testValues = new ContentValues();
    testValues.put(WaitlistContract.WaitlistEntry.COLUMN_GUEST_NAME, "test name");
    testValues.put(WaitlistContract.WaitlistEntry.COLUMN_PARTY_SIZE, 99);

    /* Insert ContentValues into database and get first row ID back */
    long firstRowId = database.insert(
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            null,
            testValues);

    /* Insert ContentValues into database and get another row ID back */
    long secondRowId = database.insert(
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            null,
            testValues);

    dbHelper.onUpgrade(database, 0, 1);
    database = dbHelper.getReadableDatabase();

    /* This Cursor will contain the names of each table in our database */
    Cursor tableNameCursor = database.rawQuery(
            "SELECT name FROM sqlite_master WHERE type='table' AND name='" +
                    WaitlistContract.WaitlistEntry.TABLE_NAME + "'",
            null);

    assertTrue(tableNameCursor.getCount() == 1);

    /*
     * Query the database and receive a Cursor. A Cursor is the primary way to interact with
     * a database in Android.
     */
    Cursor wCursor = database.query(
            /* Name of table on which to perform the query */
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null,
            /* Values for "where" clause */
            null,
            /* Columns to group by */
            null,
            /* Columns to filter by row groups */
            null,
            /* Sort order to return in Cursor */
            null);

    /* Cursor.moveToFirst will return false if there are no records returned from your query */

    assertFalse("Database doesn't seem to have been dropped successfully when upgrading",
            wCursor.moveToFirst());

    tableNameCursor.close();
    database.close();
}
 
源代码4 项目: android-dev-challenge   文件: DatabaseTest.java
/**
 * Tests that onUpgrade works by inserting 2 rows then calling onUpgrade and verifies that the
 * database has been successfully dropped and recreated by checking that the database is there
 * but empty
 * @throws Exception in case the constructor hasn't been implemented yet
 */
@Test
public void upgrade_database_test() throws Exception{

    /* Insert 2 rows before we upgrade to check that we dropped the database correctly */

    /* Use reflection to try to run the correct constructor whenever implemented */
    SQLiteOpenHelper dbHelper =
            (SQLiteOpenHelper) mDbHelperClass.getConstructor(Context.class).newInstance(mContext);

    /* Use WaitlistDbHelper to get access to a writable database */
    SQLiteDatabase database = dbHelper.getWritableDatabase();

    ContentValues testValues = new ContentValues();
    testValues.put(WaitlistContract.WaitlistEntry.COLUMN_GUEST_NAME, "test name");
    testValues.put(WaitlistContract.WaitlistEntry.COLUMN_PARTY_SIZE, 99);

    /* Insert ContentValues into database and get first row ID back */
    long firstRowId = database.insert(
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            null,
            testValues);

    /* Insert ContentValues into database and get another row ID back */
    long secondRowId = database.insert(
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            null,
            testValues);

    dbHelper.onUpgrade(database, 0, 1);
    database = dbHelper.getReadableDatabase();

    /* This Cursor will contain the names of each table in our database */
    Cursor tableNameCursor = database.rawQuery(
            "SELECT name FROM sqlite_master WHERE type='table' AND name='" +
                    WaitlistContract.WaitlistEntry.TABLE_NAME + "'",
            null);

    assertTrue(tableNameCursor.getCount() == 1);

    /*
     * Query the database and receive a Cursor. A Cursor is the primary way to interact with
     * a database in Android.
     */
    Cursor wCursor = database.query(
            /* Name of table on which to perform the query */
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null,
            /* Values for "where" clause */
            null,
            /* Columns to group by */
            null,
            /* Columns to filter by row groups */
            null,
            /* Sort order to return in Cursor */
            null);

    /* Cursor.moveToFirst will return false if there are no records returned from your query */

    assertFalse("Database doesn't seem to have been dropped successfully when upgrading",
            wCursor.moveToFirst());

    tableNameCursor.close();
    database.close();
}
 
源代码5 项目: android-dev-challenge   文件: DatabaseTest.java
/**
 * Tests that onUpgrade works by inserting 2 rows then calling onUpgrade and verifies that the
 * database has been successfully dropped and recreated by checking that the database is there
 * but empty
 * @throws Exception in case the constructor hasn't been implemented yet
 */
@Test
public void upgrade_database_test() throws Exception{

    /* Insert 2 rows before we upgrade to check that we dropped the database correctly */

    /* Use reflection to try to run the correct constructor whenever implemented */
    SQLiteOpenHelper dbHelper =
            (SQLiteOpenHelper) mDbHelperClass.getConstructor(Context.class).newInstance(mContext);

    /* Use WaitlistDbHelper to get access to a writable database */
    SQLiteDatabase database = dbHelper.getWritableDatabase();

    ContentValues testValues = new ContentValues();
    testValues.put(WaitlistContract.WaitlistEntry.COLUMN_GUEST_NAME, "test name");
    testValues.put(WaitlistContract.WaitlistEntry.COLUMN_PARTY_SIZE, 99);

    /* Insert ContentValues into database and get first row ID back */
    long firstRowId = database.insert(
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            null,
            testValues);

    /* Insert ContentValues into database and get another row ID back */
    long secondRowId = database.insert(
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            null,
            testValues);

    dbHelper.onUpgrade(database, 0, 1);
    database = dbHelper.getReadableDatabase();

    /* This Cursor will contain the names of each table in our database */
    Cursor tableNameCursor = database.rawQuery(
            "SELECT name FROM sqlite_master WHERE type='table' AND name='" +
                    WaitlistContract.WaitlistEntry.TABLE_NAME + "'",
            null);

    assertTrue(tableNameCursor.getCount() == 1);

    /*
     * Query the database and receive a Cursor. A Cursor is the primary way to interact with
     * a database in Android.
     */
    Cursor wCursor = database.query(
            /* Name of table on which to perform the query */
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null,
            /* Values for "where" clause */
            null,
            /* Columns to group by */
            null,
            /* Columns to filter by row groups */
            null,
            /* Sort order to return in Cursor */
            null);

    /* Cursor.moveToFirst will return false if there are no records returned from your query */

    assertFalse("Database doesn't seem to have been dropped successfully when upgrading",
            wCursor.moveToFirst());

    tableNameCursor.close();
    database.close();
}