我正在创建一个具有Sqlite数据库方法的类对象,但是当我调用该类时,我的构造函数被调用但是调用不在onCreate方法中。
我的电话再次返回给来电课程......请告诉我我在接下来的电话中接听电话
我使用:
调用KotDBHelper类QueryListBean queryListBean=new QueryListBean();
queryListBean.setQueryList(queryList);
db=new KotDBHelper(context,queryListBean);
db.getWritableDatabase();
my code for sqlite helper class
public class KotDBHelper extends SQLiteOpenHelper {
private static final int DB_VERSION = 3;
private static final String DB_NAME = "eKOT";
ArrayList<String> queryList;
KotDBHelper objKotDBHelper;
BeanInterface bean;
/*private static final String TABLE_CATEGORIES="category_master";
private static final String TABLE_KOT="kot_master";
private static final String TABLE_ITEM="item_master";
private static final String TABLE_KOT_DETAIL="kot_detail";
private static final String TABLE_WAITER_MASTER="waiter_master";
private static final String TABLE_TABLE_MASTER="table_master";
private static final String TABLE_CONFIGURATION_MASTER="configuration_master";
private static final String TABLE_USER_MASTER="user_master";*/
public KotDBHelper(Context context,BeanInterface bean){
super(context, DB_NAME, null, DB_VERSION);
this.bean=bean;
}
@Override
public void onCreate(SQLiteDatabase db) {
try{
String address;
BluetoothAdapter adpt=BluetoothAdapter.getDefaultAdapter();
address=adpt.getAddress();
// casting bean here
QueryListBean objBean=(QueryListBean)bean;
objBean.getQueryList();
AsyncClass objAsyncClass=new AsyncClass(objKotDBHelper,null,address);
objAsyncClass.execute("");
for(int i=0;i<queryList.size();i++){
db.execSQL(queryList.get(i));
}
/*db.execSQL(CREATE_TABLE_CATEGORIES);
db.execSQL(CREATE_TABLE_ITEM);
db.execSQL(CREATE_WAITER_MASTER);
db.execSQL(CREATE_TABL_KOT);
db.execSQL(CREATE_TABLE_KOT_DETAIL);
db.execSQL(CREATE_TABLE_MASTER);
db.execSQL(CREATE_CONFIGURATION_MASTER);
db.execSQL(CREATE_USER_MASTER);
db.execSQL("insert into table_master(table_no,curDate,type) values(1,'','Room')");
db.execSQL("insert into table_master(table_no,curDate,type) values(10,'','Table')");
db.execSQL("insert into user_master(username,password) values('admin','admin')");*/
}catch(Exception e){
e.printStackTrace();
}
}
答案 0 :(得分:0)
这是使用sqlite帮助程序类在android中创建数据库的示例代码:
public class SQLite_Helper extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "mydb.db";
// Contacts table name
private static final String USER_TABLE = "User";
public static final String TABLE_ACCOUNT = "Account";
static String CREATE_TABLE_TRANSACTION = "CREATE TABLE " + TABLE_TRANSACTION + "("
+ TRAN_ID + " INTEGER PRIMARY KEY NOT NULL," + PAYEE + " TEXT," + CATEGORY + " TEXT," + AMOUNT + " NUMERIC,"
+ DUEDATE + " TEXT," + PAYFROM + " TEXT," + NOTES + " TEXT," + IS_REPEAT + " TEXT," + TYPE + " TEXT," + " FOREIGN KEY ("+TRAN_ID+") REFERENCES "+USER_TABLE+" ("+USER_ID+"));";
String CREATE_USER_TABLE = "CREATE TABLE " + USER_TABLE + "("
+ USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + FIRST_NAME + " TEXT not null unique," + LAST_NAME + " TEXT not null unique," + EMAIL + " TEXT not null unique,"
+ PASSWORD + " TEXT not null unique" + ")";
static String CREATE_TABLE_ACCOUNT = "CREATE TABLE " + TABLE_ACCOUNT + "("
+ ACCT_ID + " INTEGER PRIMARY KEY NOT NULL," + ACCT_NAME + " TEXT,"
+ ACCT_HEAD_NAME + " TEXT," + OPEN_BAL + " TEXT,"+ ACCOUNT_EMAIL + " TEXT," + " FOREIGN KEY ("+ACCT_ID+") REFERENCES "+USER_TABLE+" ("+USER_ID+"));";
public SQLite_Helper(Context context) {
super(context, "login.db", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_TABLE_ACCOUNT);
db.execSQL(CREATE_USER_TABLE);
db.execSQL(CREATE_TABLE_TRANSACTION);
}
// code or methods something here...
}
答案 1 :(得分:0)
数据库助手onCreate()
在
使用getWritableDatabase()
或getReadableDatabase()
和
数据库文件不存在。
一旦onCreate()
成功返回而不抛出,则认为数据库已创建。
了解更多信息:When is SQLiteOpenHelper onCreate() / onUpgrade() run?
在onCreate()
中执行除数据库设置之外的其他操作是不正确的。只需创建表并使用一些默认值(如果有)填充它们。从onCreate()
删除所有其他代码。
通过捕获异常来隐藏问题是不正确的。如果在设置数据库时出现问题,onCreate()
应该将问题(异常)传播到框架。删除try-catch。