2016-03-02 56 views
0

使用以下查询创建名称为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 (?,?)

+1

很遗憾,您的表没有创建。所以请交叉检查你的'SQL CREATE Table命令'并按照@Jas的说法做 –

+0

表未被创建并检查数据库版本也 – Chinmay

回答

2

日志清楚的问题说。 no such table: shipmentMaster。检查您的命令创建表,如果没有问题,请尝试从设备上卸载应用程序并重新安装。

替换您创建语句是:

private static final String CREATE_SHIPMENTMASTER_TABLE = "CREATE TABLE " + TABLE_SHIPMENTMASTER 
          + "(" 
          + KEY_ID + " INTEGER PRIMARY KEY," 
          + KEY_STATUS + " TEXT");"; 
+0

谢谢!它说的东西像'android.database.sqlite.SQLiteConstraintException:PRIMARY KEY必须是唯一的(代码19)' –

+1

你不应该在多个colums中添加相同的密钥ID – Jas

+0

我会发布我如何做这个...我猜问题在于我已更新它的循环 –

相关问题