-
StackOverflow 文件
-
Android 教程
-
SQLite
-
使用 SQLiteOpenHelper 類
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "Example.db";
private static final int DATABASE_VERSION = 3;
// For all Primary Keys _id should be used as column name
public static final String COLUMN_ID = "_id";
// Definition of table and column names of Products table
public static final String TABLE_PRODUCTS = "Products";
public static final String COLUMN_NAME = "Name";
public static final String COLUMN_DESCRIPTION = "Description";
public static final String COLUMN_VALUE = "Value";
// Definition of table and column names of Transactions table
public static final String TABLE_TRANSACTIONS = "Transactions";
public static final String COLUMN_PRODUCT_ID = "ProductId";
public static final String COLUMN_AMOUNT = "Amount";
// Create Statement for Products Table
private static final String CREATE_TABLE_PRODUCT = "CREATE TABLE " + TABLE_PRODUCTS + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY, " +
COLUMN_DESCRIPTION + " TEXT, " +
COLUMN_NAME + " TEXT, " +
COLUMN_VALUE + " REAL" +
");";
// Create Statement for Transactions Table
private static final String CREATE_TABLE_TRANSACTION = "CREATE TABLE " + TABLE_TRANSACTIONS + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY," +
COLUMN_PRODUCT_ID + " INTEGER," +
COLUMN_AMOUNT + " INTEGER," +
" FOREIGN KEY (" + COLUMN_PRODUCT_ID + ") REFERENCES " + TABLE_PRODUCTS + "(" + COLUMN_ID + ")" +
");";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// onCreate should always create your most up to date database
// This method is called when the app is newly installed
db.execSQL(CREATE_TABLE_PRODUCT);
db.execSQL(CREATE_TABLE_TRANSACTION);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// onUpgrade is responsible for upgrading the database when you make
// changes to the schema. For each version the specific changes you made
// in that version have to be applied.
for (int version = oldVersion + 1; version <= newVersion; version++) {
switch (version) {
case 2:
db.execSQL("ALTER TABLE " + TABLE_PRODUCTS + " ADD COLUMN " + COLUMN_DESCRIPTION + " TEXT;");
break;
case 3:
db.execSQL(CREATE_TABLE_TRANSACTION);
break;
}
}
}
}