Code example for SQLiteDatabase

Methods: beginTransactionendTransactionexecSQLsetTransactionSuccessful

0
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    } 
 
    @Override 
    public void onCreate (SQLiteDatabase db) {
        db.execSQL("CREATE TABLE cards ("
        + "_id        INTEGER PRIMARY KEY, " 
        + "type       TEXT NOT NULL, " 
        + "serial     TEXT NOT NULL, " 
        + "data       BLOB NOT NULL, " 
        + "scanned_at LONG" 
        + ");"); 
    } 
 
    @Override 
    public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion == 1 && newVersion == 2) {
            db.beginTransaction();
            try { 
                db.execSQL("ALTER TABLE cards RENAME TO cards_old");
                onCreate(db);
                db.execSQL("INSERT INTO cards (type, serial, data) SELECT type, serial, data from cards_old");
                db.execSQL("DROP TABLE cards_old");
                db.setTransactionSuccessful();
            } finally { 
                db.endTransaction();
            } 
            return; 
        } 
        throw new UnsupportedOperationException("Not yet implemented");
    }