Java Code Examples for android.database.sqlite.SQLiteOpenHelper#onUpgrade()

The following examples show how to use android.database.sqlite.SQLiteOpenHelper#onUpgrade() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DatabaseTest.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * 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();
}
 
Example 2
Source File: DatabaseTest.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * 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();
}
 
Example 3
Source File: DatabaseTest.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * 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();
}
 
Example 4
Source File: DatabaseTest.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * 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();
}
 
Example 5
Source File: DatabaseTest.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * 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();
}