Exception invalid restrictions – Android

I'm trying to put JSON objects in the SQLite database

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 object information looks like this

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

This is my little table

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);";

I'm trying to insert command and params. My SQLite code looks like this

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;

 }

Basically, I get the exception from the addlist function

adding Exception : Exception invalid LIMIT clauses

How to consider inserting JSON objects into SQLite

resolvent:

These are the parameters of the query () method:

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

The orderby and limit parameters are meaningless. To find the largest ID in the entire table, these parameters must be null

In any case, there is a helper function that makes it easier to read a single number from the database without using a cursor:

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

Also, if you have declared the ID column as integer primary key, the column will be incremented automatically and you do not have to set it manually

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>