2011-10-19 153 views
0

我試圖插入一些數據在我的Android SQLite新創建的數據庫與這段代碼,但它不工作。Android SQLite插入數據

以下是錯誤:

10-19 19:05:39.793: INFO/Database(413): sqlite returned: error code = 1, msg = near ",": syntax error 

我知道這些代碼需要一個乾淨的,但後來我會做... ^^

下面是代碼:

SQLiteDatabase db = this.sqlHelper.getWritableDatabase(); 
ContentValues dv = new ContentValues(); 
dv.put("`createdTime`", h.createdTime); 
dv.put("`type`", h.type); 
dv.put("`what`", h.what); 
dv.put("`where`", h.where); 
dv.put("`readableWhat`", h.readableWhat); 
dv.put("`readableWhere`", h.readableWhere); 
dv.put("`accuracy`", h.accuracy); 
dv.put("`isMnemo`", h.isMnemo); 
dv.put("`isPlaceCode`", h.isPlaceCode); 
dv.put("`isCoords`", h.isCoords); 
dv.put("`isNear`", h.isNear); 
dv.put("`isEverywhere`", h.isEverywhere); 

Cursor c = db.query("HISTORY", 
     new String[]{ 
       "`type`", 
       "`what`", 
       "`where`", 
       "`readableWhat`", 
       "`readableWhere`", 
       "`accuracy`", 
       "`isMnemo`", 
       "`isPlaceCode`", 
       "`isCoords`", 
       "`isNear`", 
       "`isEverywhere`", 
     }, 
     "`type`='" + h.type + "'" + "," + 
     "`what`='" + h.what + "'" + "," + 
     "`where`='" + h.where + "'" + "," + 
     "`readableWhat`='" + h.readableWhat + "'" + "," + 
     "`readableWhere`='" + h.readableWhere + "'" + "," + 
     "`accuracy`='" + h.accuracy +"'" + "," + 
     "`isMnemo`='" + h.isMnemo + "'" + "," + 
     "`isPlaceCode`='" + h.isPlaceCode + "'" + "," + 
     "`isCoords`='" + h.isCoords + "'" + "," + 
     "`isNear`='" + h.isNear + "'" + "," + 
     "`isEverywhere`='" + h.isEverywhere + "'" 
     , 
     null, null, null, null); 
if (!c.moveToFirst()) 
{ 
    PJUtils.log("INSERT " + h.readableWhat + " - " + h.readableWhere); 
    db.insertOrThrow("HISTORY", "", dv); 
} 
else 
{ 
    PJUtils.log("UPDATE " + h.readableWhat + " - " + h.readableWhere); 
    db.update("CONFIG", 
     dv, 
     "`type`='" + h.type + "'" + "," + 
     "`what`='" + h.what + "'" + "," + 
     "`where`='" + h.where + "'" + "," + 
     "`readableWhat`='" + h.readableWhat + "'" + "," + 
     "`readableWhere`='" + h.readableWhere + "'" + "," + 
     "`accuracy`='" + h.accuracy +"'" + "," + 
     "`isMnemo`='" + h.isMnemo + "'" + "," + 
     "`isPlaceCode`='" + h.isPlaceCode + "'" + "," + 
     "`isCoords`='" + h.isCoords + "'" + "," + 
     "`isNear`='" + h.isNear + "'" + "," + 
     "`isEverywhere`='" + h.isEverywhere + "'" 
     , 
     null); 
} 
c.close(); 
+0

你爲什麼把撇號到您的字符串;我的意思是「'type'=」而不是「type =」? – slkorolev

+0

如何做到這一點?我的意思是你用'type =?'的方式。你在談論'selectionArgs'參數嗎? – ChristopheCVB

回答

2

而不是

+ "," + 

使用

+ " and " + 
+0

Thx很多!愚蠢的東西... – ChristopheCVB

0

嘗試此查詢:

  String[] columns = new String[]{ 
       "type", 
       "what", 
       "where", 
       "readableWhat", 
       "readableWhere", 
       "accuracy", 
       "isMnemo", 
       "isPlaceCode", 
       "isCoords", 
       "isNear", 
       "isEverywhere" }; 
     String selection = "type=? AND what=? AND where=? AND readableWhat=? AND readableWhere=? AND accuracy=? AND isMnemo=? AND isPlaceCode=? AND isCoords=? AND isNear=? AND isEverywhere=?"; 
     String[] selectionArgs = new String[]{ 
       h.type, 
       h.what, 
       h.where, 
       h.readableWhat, 
       h.readableWhere, 
       h.accuracy, 
       h.isMnemo, 
       h.isPlaceCode, 
       h.isCoords, 
       h.isNear, 
       h.isEverywhere 
     }; 
     Cursor c = db.query("HISTORY", columns, selection, selectionArgs, null, null, null);