我正在尝试向 SQLite 表添加一些记录,但 LogCat 告诉我该表不存在。并且 DDMS 显示,是的,该表没有/尚未创建。
然而我确实在 SQLiteOpenHelper 类中创建了表:
public class SQLiteHandlerDeliveryItem extends SQLiteOpenHelper {
. . .
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String CREATE_DELIVERYITEMS_TABLE = "CREATE TABLE " +
TABLE_DELIVERYITEMS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_INVOICENUM + " TEXT,"
+ COLUMN_UPCPLU + " TEXT," + COLUMN_VENDORITEMID + " TEXT,"
+ COLUMN_PACKSIZE + " INTEGER," + COLUMN_DESCRIPTION + " TEXT,"
//+ COLUMN_COST + " REAL," + COLUMN_MARGIN + " REAL," + COLUMN_LISTPRICE + " REAL,"
+ COLUMN_COST + " REAL DEFAULT 0," + COLUMN_MARGIN + " REAL DEFAULT 0," +
COLUMN_LISTPRICE + " REAL DEFAULT 0,"
+ COLUMN_DEPTNUM + " INTEGER," + COLUMN_SUBDEPT + " TEXT," + COLUMN_QTY + " TEXT"
+ ")";
sqLiteDatabase.execSQL(CREATE_DELIVERYITEMS_TABLE);
}
我调用添加记录的类的方法:
SQLiteHandlerDeliveryItem sqliteHandler = new SQLiteHandlerDeliveryItem(SQLiteActivity.this, null);
sqliteHandler.addDeliveryItem(delItem);
这应该调用 SQLiteHandlerDeliveryItem 的构造函数(当 sqliteHandler 被实例化时),但它没有!我在 onCreate() 方法中有一个断点,果然 - 它从未到达过。
为什么?以及如何强制调用构造函数,以便创建表?
奇怪的 [est] 事情是我还在另一个(工作)SQLiteOpenHelper 类中放置了一个断点,而且它也没有达到......什么?!?它至少工作了一次,因为该表确实存在/是从该代码创建的。
所以很明显我的挥杆有一个洞;我有什么误解或做错了什么?
更新
我过早地将答案标记为答案。
至于:
“1. 您必须在某个时候调用 getWritableDatabase() 或 getReadableDatabase()。”
我确实在创建或读取记录的每个方法中调用 getWritableDatabase(),如下所示:
public long addDeliveryItem(DeliveryItem delItem) {
long IdAdded = 0;
ContentValues values = new ContentValues();
values.put(COLUMN_INVOICENUM, delItem.get_invoiceNumber());
. . .
values.put(COLUMN_QTY, delItem.get_quantity());
SQLiteDatabase db = this.getWritableDatabase(); <= Rot Cheer
if (db != null) {
IdAdded = db.insert(TABLE_DELIVERYITEMS, null, values);
}
. . .
...关于:
“2. 在 SQLiteHandlerDeliveryItem 的构造函数中,您必须调用 super(...)。”
我确实在扩展 SQLiteOpenHelper 的类中调用了 super:
public SQLiteHandlerDeliveryItem(Context context, SQLiteDatabase.CursorFactory factory)
{
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
所以......我仍然不知道问题/解决方案是什么......
更新 2
因此,当我尝试在 LogCat 中将记录插入此表时,我看到的是:
E/SQLiteLog:(1)没有这样的表:deliveryitems
然而,我尝试添加记录的代码实例化了扩展 SQLiteOpenHelper 的相应/适当类,如下所示:
else if ("Delivery Items".equals(tableName)) {
try {
JSONArray jsonArr = new JSONArray(result);
for (int i = 0; i < jsonArr.length(); i++) {
JSONObject jsonObj = jsonArr.getJSONObject(i);
String invNum = jsonObj.getString("invoiceNumber");
. . .
// Prepare for writing to db
DeliveryItem delItem = new DeliveryItem();
delItem.set_invoiceNumber(invNum);
. . .
SQLiteHandlerDeliveryItem sqliteHandler = new
SQLiteHandlerDeliveryItem(SQLiteActivity.this, null);
sqliteHandler.addDeliveryItem(delItem);
}
}
...并且该类具有创建表的代码:
public class SQLiteHandlerDeliveryItem extends SQLiteOpenHelper {
. . .
public SQLiteHandlerDeliveryItem(Context context, SQLiteDatabase.CursorFactory factory)
{
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String CREATE_DELIVERYITEMS_TABLE = "CREATE TABLE " +
TABLE_DELIVERYITEMS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_INVOICENUM + " TEXT,"
+ COLUMN_UPCPLU + " TEXT," + COLUMN_VENDORITEMID + " TEXT,"
+ COLUMN_PACKSIZE + " INTEGER," + COLUMN_DESCRIPTION + " TEXT,"
+ COLUMN_COST + " INTEGER," + COLUMN_MARGIN + " INTEGER," + COLUMN_LISTPRICE + " INTEGER,"
+ COLUMN_DEPTNUM + " INTEGER," + COLUMN_SUBDEPT + " TEXT," + COLUMN_QTY + " TEXT"
+ ")";
sqLiteDatabase.execSQL(CREATE_DELIVERYITEMS_TABLE);
}
那么......我做错了什么,或者没有做对?
更新 3
确实没有创建表。LogCat 中的 err msg 表明情况确实如此(它是“没有这样的表:delivertitems”)。
我的构造函数如下所示:
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_DELIVERYITEMS_TABLE = "CREATE TABLE " +
TABLE_DELIVERYITEMS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_INVOICENUM + " TEXT,"
+ COLUMN_UPCPLU + " TEXT," + COLUMN_VENDORITEMID + " TEXT,"
+ COLUMN_PACKSIZE + " INTEGER," + COLUMN_DESCRIPTION + " TEXT,"
+ COLUMN_COST + " INTEGER," + COLUMN_MARGIN + " INTEGER," + COLUMN_LISTPRICE + " INTEGER,"
+ COLUMN_DEPTNUM + " INTEGER," + COLUMN_SUBDEPT + " TEXT," + COLUMN_QTY + " TEXT"
+ ")";
db.execSQL(CREATE_DELIVERYITEMS_TABLE);
}
确实,该代码不会被输入。那么我必须跳过什么箍才能调用构造函数?
我认为当我实例化类时会发生这种情况:
SQLiteHandlerDeliveryItem sqliteHandler = new
SQLiteHandlerDeliveryItem(SQLiteActivity.this, null);
即使调用它,构造函数如何知道 arg (SQLiteDatabase db) 是什么——它从哪里得到这个值?
我确实有一个数据库,只有一张表。它只是拒绝添加第二个表。
正如有人推荐的那样,我正在添加一个单独的类,该类为我要添加到数据库的每个表扩展 SQLiteOpenHelper。
当我到达那条线时:
SQLiteHandlerDeliveryItem sqliteHandler = new
SQLiteHandlerDeliveryItem(SQLiteActivity.this, null);
...并混搭 F7 进入它,我到达类构造函数:
public SQLiteHandlerDeliveryItem(Context context, SQLiteDatabase.CursorFactory factory)
{
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
...但不是 onCreate 事件。
所以“难怪”我没有deliveryitems 表,因为创建该表的代码永远不会到达;但是为什么没有达到 - 我必须做些什么才能达到它?