Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite exception- unrecognized token when trying to update table

Tags:

android

sqlite

I am trying to update my SQLite database through this method in my SQLiteHelper class and I am getting the error:

android.database.sqlite.SQLiteException: unrecognized token: "55c7e253afcf48" (code 1): , while compiling: UPDATE login SET user_group=? WHERE uid=55c7e253afcf48.85187730
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

The uid is correct other than the extra ".85187730" at the end... I'm not sure what those numbers mean. Here is my update method:

//updating sqlite database with the group name
    public void updateUserGroup(String groupName) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_GROUP, groupName);
        HashMap<String, String> user = getUserDetails();
        String userID = user.get("uid");

        System.out.println("User id is: " + userID);

        db.update(TABLE_LOGIN, values, SQLiteHandler.KEY_UID + "=" + "userID", null);




    }

    /**
     * Getting user data from database
     * */
    public HashMap<String, String> getUserDetails() {
        HashMap<String, String> user = new HashMap<String, String>();
        String selectQuery = "SELECT  * FROM " + TABLE_LOGIN;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // Move to first row
        cursor.moveToFirst();
        if (cursor.getCount() > 0) {
            user.put("name", cursor.getString(1));
            user.put("email", cursor.getString(2));
            user.put("user_group", cursor.getString(3));
            user.put("uid", cursor.getString(4));
            user.put("created_at", cursor.getString(5));
        }
        cursor.close();
        //db.close();
        // return user
        Log.d(TAG, "Fetching user from Sqlite: " + user.toString());

        return user;
    }

Any help is appreciated.

like image 852
clue11 Avatar asked Dec 19 '22 01:12

clue11


1 Answers

You've missed quotation marks around your uid.

You should pass every string as below to avoid errors.

UPDATE login SET user_group=? WHERE uid="55c7e253afcf48.85187730"

and in your case query will be like

HashMap<String, String> user = getUserDetails();
String userID = user.get("uid");
db.update(TABLE_LOGIN, values, SQLiteHandler.KEY_UID + "=\"" + userID + "\"", null);
like image 85
SilentKiller Avatar answered Jan 12 '23 00:01

SilentKiller