使用以下查询创建名称为shippingMaster的表。尝试将json的分析值添加到sqlite表中时出现Sqlite异常
private static final String CREATE_SHIPMENTMASTER_TABLE = "CREATE TABLE " + TABLE_SHIPMENTMASTER + "("+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_STATUS + " TEXT" + ")";
onCreate方法中,我执行上述查询。
将值添加到数据库中,进出口使用下面的方法,
public void addShipmentMaster (ShipmentMasterDao shipmentMasterDao){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID, shipmentMasterDao.getId());
values.put(KEY_STATUS,shipmentMasterDao.getStatus());
db.insert(TABLE_SHIPMENTMASTER, null, values);
db.close();
}
更新的代码: 我解析一个JSON添加这些值以分贝如下,
JSONObject value = new JSONObject(notificationResponse.getString("value"));
JSONObject shipmentMaster = value.getJSONObject("shipment_master");
Iterator<String> shipmentMasterIterator = shipmentMaster.keys();
String status = null;
String key = null;
final int numberLenth = shipmentMaster.length();
while (shipmentMasterIterator.hasNext()) {
key = shipmentMasterIterator.next();
status = shipmentMaster.optString(key);
db.addShipmentMaster(new ShipmentMasterDao(Integer.valueOf(key), status));
}
即时解析的Json如下,
{
"status": 1,
"value": {
"shipment_master": {
"1": "Order Placed",
"2": "In Production",
"3": "Quality Check In progress",
"4": "Goods received for shipment",
"5": "Stuffing in progress",
"6": "Cargo Shipped"
}
}
}
它如下抛出一个错误信息,
E/SQLiteLog: (1) no such table: shipmentMaster E/SQLiteDatabase: Error inserting status=Cargo Shipped id=0 android.database.sqlite.SQLiteException: no such table: shipmentMaster (code 1): , while compiling: INSERT INTO shipmentMaster(status,id) VALUES (?,?)
很遗憾,您的表没有创建。所以请交叉检查你的'SQL CREATE Table命令'并按照@Jas的说法做 –
表未被创建并检查数据库版本也 – Chinmay