public void btn1OnClick(View v) {
dbHelper = new MyDBHelper(this);
db = dbHelper.getWritableDatabase();
TextView output = (TextView) findViewById(R.id.txv1);
output.setText("資料庫是否開啟:" + db.isOpen() + "
資料版本:" + db.getVersion());
}
}
public class MyDBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "MyBooks";
private static final int DATABASE_VERSION = 1;
public MyDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE titles (_id"
+ "integer primary key autoincrement, "
+ "title text no null, price real no null)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS titles");
onCreate(db);
}
}