2013年1月13日

Android使用SQLite/SQLiteDatabase/SQLiteOpenHelper 建立資料庫


建立一個SQLiteOpenHelper的class類別


import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MySQLiteHelper extends SQLiteOpenHelper  implements DatatableString{

public MySQLiteHelper(Context context) {
super(context, DatatableString.MYDATABASE, null,DatatableString.MYDATABASE_VERSION);
Log.d("MySQLiteHelper","public MySQLiteHelper(Context context)");
}

@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
Log.d("MySQLiteHelper", "public void onCreate(SQLiteDatabase arg0)");
String DATABASE_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "+DatatableString.MYTABLENAME+" ("
+ "F_ID INTEGER PRIMARY KEY,"
+ "F_A CHAR(10),"
+ "F_DATETIME DATE,"
+ "F_ITEM_A VARCHAR(100),"
+ "F_ITEM_B VARCHAR(100),"
+ "F_COST INTEGER,"
+ "F_COMMEND VARCHAR(1000),"
+ "F_PICTURE_PATH CHAR(1000),"
+ "F_BACK_A CHAR(100),"
+ "F_BACK_B CHAR(100)"
+ ");";
// 建立moneyTable資料表
arg0.execSQL(DATABASE_CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}
}

interface DatatableString
{
public static final String MYDATABASE="mydatatable.db";// 資料庫名稱
public static final int MYDATABASE_VERSION = 1; // 資料庫版本
public static final String MYTABLENAME="mytableone";
}

沒有留言:

張貼留言