异常无效限制条款-Android

我正在尝试将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对象信息看起来像这样

{
    "COMMAND":"ADD_NEW",
    "ParaMS":{
        "deviceid":"1234",
        "custID":"41701",
        "description":"Ddd",        
        "colTranType":"ABS",        
        }
}

这是我的小桌子

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

解决方法:

这些是query()方法的参数:

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和limit参数没有意义.要在整个表中找到最大的ID,这些参数必须为null.

无论如何,有一个helper function使得从数据库中读取单个数字变得更容易,而不必使用游标进行处理:

long lastID = DatabaseUtils.longForQuery(_Db, "SELECT max(ID) FROM "+TABLE_NAME, null);

并且,如果您已将ID列声明为INTEGER PRIMARY KEY,则该列将自动递增,并且您不必手动设置.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...