我正在尝试将json对象放在sqlite数据库中
private void addList(String info){
try {
JSONObject reader=new JSONObject(info);
String COMMAND = reader.getString("COMMAND");
String PARAMS = reader.getString("PARAMS");
int id = vpictures.InsertList(COMMAND,info);
} catch (Exception e){
if (DEBUG_FLAG)Log.d("Log1 ", "adding Exception :"+ e.getMessage());
}
return;
}
json对象info
看起来像这样
{
"COMMAND":"ADD_NEW",
"PARAMS":{
"deviceID":"1234",
"custID":"41701",
"description":"Ddd",
"colTranType":"ABS",
}
}
这是我的sqlite表
private static final String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER, DEVICEID TEXT, LTIME INTEGER, LATITUDE REAL,"+
"LONGITUDE REAL, THEPICTURE BLOB, SENT INTEGER, NOTES TEXT, COMMAND TEXT, PARAMS TEXT);";
我正在尝试插入COMMAND
和PARAMS
。
我的sqlite代码看起来像这样
public int InsertList(String COMMAND, String info){
try {
JSONObject reader = new JSONObject(info);
String param_str = reader.getString("info");
if (_Db == null)
_Db = getWritableDatabase();
if (_LastId == -1)
{
Cursor c = _Db.query(TABLE_NAME, new String[] {"max(ID)"}, null, null, null, null, COMMAND, param_str);
if (c != null && c.moveToFirst()) {
_LastId = c.getInt(0);
c.close();
}
else
_LastId = 0;
c.close();
}
try {
ContentValues cv = new ContentValues();
cv.put("ID",++_LastId);
cv.put("COMMAND",String.valueOf(COMMAND));
cv.put("PARAMS",PARAMS);
_Db.insert(TABLE_NAME, "", cv);
} catch (Exception e){
Log.d("Log2","Error:"+e.getMessage());
}
} catch (JSONException e1) {
Log.d("Log2","Error:"+e1.getMessage());
}
return _LastId;
}
基本上是我从addList
函数
adding Exception : Exception invalid LIMIT clauses
如何考虑将json对象插入sqlite
答案 0 :(得分:3)
这些是query()方法的参数:
using System.Net;
string requestUri = "http:\\example.com";
using (var webClient = new WebClient()) {
webClient.Headers[HttpRequestHeader.Accept] = "application/json";
string json = webClient.DownloadString(requestUri);
}
Cursor c = _Db.query(
TABLE_NAME, // table
new String[] {"max(ID)"}, // columns
null, // selection
null, // selectionArgs
null, // groupBy
null, // having
COMMAND, // orderBy
param_str); // limit
和orderBy
参数没有意义。要查找整个表中的最大ID,这些参数必须为limit
。
无论如何,有一个helper function使得从数据库中读取单个数字变得更加容易,而不必使用游标:
null
如果您已将long lastID = DatabaseUtils.longForQuery(_Db, "SELECT max(ID) FROM "+TABLE_NAME, null);
列声明为ID
,则会自动增量,您无需手动设置。