简体   繁体   中英

No such column found error while updating sqlite database

I'm using following code to update my SQLite database.

fun updateTable(user: Users):Int{
        val db=this.writableDatabase
        val values=ContentValues().apply {
            put(USER_ID,user.userId)
            put(USER_NAME,user.name)
            put(USER_NUMBER,user.number)
            put(USER_STATUS,user.status)
            put(USER_IMAGE,user.image)
            put(ANY_MESSAGE_MADE,user.anyMessageMade)
        }
        val newRowId = db?.update(TABLE_NAME,values, USER_ID+ "="+user.userId,null)!!
        db.close()
        return newRowId
    }

But I got this error in my log and programn being crased

E/SQLiteLog: (1) no such column: vV91XjGEgzIpV3235aYS
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.russvkm.chathut, PID: 4421
    android.database.sqlite.SQLiteException: no such column: vV91XjGEgzIpV3235aYS (code 1): , while compiling: UPDATE contact_entry SET name=?,user_status=?,anyMessageMade=?,userId=?,user_image=?,user_number=? WHERE userId=vV91XjGEgzIpV3235aYS
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:890)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:501)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1651)
        at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1599)
        at com.russvkm.chathut.utils.CreateContact.updateTable(CreateContact.kt:78)
        at com.russvkm.chathut.fragment.ChatFragment.onClick(ChatFragment.kt:89)
        at android.view.View.performClick(View.java:6294)
        at android.view.View$PerformClick.run(View.java:24770)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

user id and table entry are available what I'm trying to update.

I'm trying to update table with primary key which is userId

The value of of the variable user.userId should be inside single quotes when you pass it to the WHERE clause of the update() method.
So the code should be:

db?.update(TABLE_NAME,values, USER_ID + " = '" + user.userId + "'", null)!!

but the correct and safe way to do this is with a ? placeholder in the WHERE clause and passing the value of the parameter in the 4th argument of update() :

db?.update(TABLE_NAME, values, USER_ID + " = ?", arrayOf(user.userId))!!

But in your code there is also this line:

put(USER_ID,user.userId)

Why do you want to update the column userId with the same value that it already has?

Also the method update() returns the number of the updated rows and not any new rowid .
So what is the point of this line:

val newRowId = db?.update(TABLE_NAME,values, USER_ID+ "="+user.userId,null)!! 

This makes me think that maybe you want to insert a new row in the table and not update an existing row.
If this is the case then you should use the method insert() :

val newRowId = db?.insert(TABLE_NAME, null, values)!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM