public OrderDataBaseUtil(Context ctx, String tbname, String cols[], String cons[]) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
// super(context, name, factory, version);
/**
* context to use to open or create the database
* name of the database file, or null for an in-memory database
* factory to use for creating cursor objects, or null for the default
* version number of the database (starting at 1); if the database is older, onUpgrade(SQLiteDatabase, int, int) will be used to upgrade the database
*/
tableName = tbname;
columns = cols;
constraints = cons;
Log.i("OrderDataBaseUtil constructor","OrderDataBaseUtil");
//由于这里,onCreate()数据库第一次创建执行,只执行一次,以后不再执行
//所以先判断table存在与否
//不存在,调用onCreate
SQLiteDatabase sdb = openWriteDB();
onCreate(sdb);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i("OrderDataBaseUtil::onCreate","OrderDataBaseUtil");
StringBuilder sql = new StringBuilder();
// if not exists,不需要知道表是否存在,在创建表的时候加上if not exists
sql.append("CREATE TABLE if not exists ").append(tableName); //sql.append("CREATE TABLE ").append(tableName);
sql.append("(").append(DEFAULT_ID).append(",");
for(int i = 0; i < columns.length; i++){
if(i < columns.length -1)
sql.append(columns[i]).append(" ").append(constraints[i]).append(",");
else
sql.append(columns[i]).append(" ").append(constraints[i]);
}
sql.append(")");
Log.i("CREATE_TB==>>", sql.toString());
db.execSQL(sql.toString());
}
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
// super(context, name, factory, version);
/**
* context to use to open or create the database
* name of the database file, or null for an in-memory database
* factory to use for creating cursor objects, or null for the default
* version number of the database (starting at 1); if the database is older, onUpgrade(SQLiteDatabase, int, int) will be used to upgrade the database
*/
tableName = tbname;
columns = cols;
constraints = cons;
Log.i("OrderDataBaseUtil constructor","OrderDataBaseUtil");
//由于这里,onCreate()数据库第一次创建执行,只执行一次,以后不再执行
//所以先判断table存在与否
//不存在,调用onCreate
SQLiteDatabase sdb = openWriteDB();
onCreate(sdb);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i("OrderDataBaseUtil::onCreate","OrderDataBaseUtil");
StringBuilder sql = new StringBuilder();
// if not exists,不需要知道表是否存在,在创建表的时候加上if not exists
sql.append("CREATE TABLE if not exists ").append(tableName); //sql.append("CREATE TABLE ").append(tableName);
sql.append("(").append(DEFAULT_ID).append(",");
for(int i = 0; i < columns.length; i++){
if(i < columns.length -1)
sql.append(columns[i]).append(" ").append(constraints[i]).append(",");
else
sql.append(columns[i]).append(" ").append(constraints[i]);
}
sql.append(")");
Log.i("CREATE_TB==>>", sql.toString());
db.execSQL(sql.toString());
}