Opret en Singleton Database-klasse:
public class Database extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "yourDatabaseName";
//Declare a String for each Table name
public static final String TABLE_NAME1 = "tableName1";
public static final String TABLE_NAME2 = "tableName2";
//Declare a SQL string for create each table
private static final String CREATE_TABLE1 =
"CREATE TABLE if not exists " + TABLE_NAME1 ..............";
private static final String CREATE_TABLE2 =
"CREATE TABLE if not exists " + TABLE_NAME2 .................";
private static Database Singleton = null;
private Context context;
private static SQLiteDatabase Db;
private Database(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
public static synchronized Database getInstance(Context c){
if(Singleton == null) {
Singleton = new Database(c.getApplicationContext());
Db = Singleton.getWritableDatabase();
}
return Singleton;
}
public SQLiteDatabase getDatabase() {
return Db;
}
public synchronized void close() {
if (Singleton != null && Db.isOpen()) Db.close();
}
@Override
protected void finalize() throws Throwable {
try{
close();
}
finally{
super.finalize();
}
@Override
public void onCreate(SQLiteDatabase db) {
//Create Tables
db.execSQL(CREATE_TABLE1);
db.execSQL(CREATE_TABLE2);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
Opret basisklasse for tabeller. Denne klasse har almindelige metoder til at håndtere tabeller
abstract class DatabaseTable {
private SQLiteDatabase Db;
private String TableName;
public DatabaseTable(SQLiteDatabase db,String tableName) {
this.TableName = tableName;
this.Db = db;
}
public Cursor fetchAll() {
Cursor cursor = Db.query(TableName, null, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
public Cursor fetchAllOrderBy(String OrderField) {
Cursor cursor = Db.query(TableName, null, null, null, null, null, OrderField);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
public Cursor fetchById(long id) {
Cursor cursor = Db.query(TableName, null, "_id = " + id, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
public boolean deleteById(long id) {
return Db.delete(TableName,"_id = " + id, null) > 0;
}
public boolean deleteAll() {
return Db.delete(TableName,null, null) > 0;
}
public int getNumberRows() {
return (int) DatabaseUtils.queryNumEntries(Db, TableName);
}
}
Nu skal du definere klasser for hver tabel, du ønsker at håndtere:
public class TbTable1 extends DatabaseTable{
//Declare string for each field name
public static final String FIELD_ID = "_id";
public static final String FIELD_FIELDNAME1 = "fieldName1";
public static final String FIELD_FIELDNAME2 = "fieldName2";
..........
..........
private SQLiteDatabase Db;
//Get the table name from Database class
private static final String TableName = Database.TABLE_NAME1;
public TbTable1(SQLiteDatabase db) {
super(db,TableName);
this.Db = db;
}
// Below define all the methods you need to access this table
public long insertRecord(String value1, String value2) {
ContentValues initialValues = new ContentValues();
initialValues.put(FIELD_FIELDNAME2, value1);
initialValues.put(FIELD_FIELDNAME2, value2);
return Db.insert(TableName, null, initialValues);
}
..............
..............
}
Når du vil gøre noget med en tabel:
SQLiteDatabase Db = Database.getInstance(context).getDatabase();
TbTable1 table1 = new TbTable1(Db);
table1.insertRecord("Value1","Value2");
Cursor cursor = table1.fetchAll();
// do something with data
cursor.close();
Håber dette hjælper!