使用以下查詢創建名稱爲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