简体   繁体   English

尝试更新行时出现SQLite语法错误

[英]SQLite syntax error when trying to update row

I am trying to update a row in my database with 3 values but I keep getting a syntax error and I am not sure why, everything looks fine to me. 我正在尝试使用3个值更新数据库中的一行,但是我不断收到语法错误,并且我不确定为什么,一切对我来说都很好。

12-12 12:14:59.800: E/AndroidRuntime(21175): FATAL EXCEPTION: main
12-12 12:14:59.800: E/AndroidRuntime(21175): java.lang.IllegalStateException: Could not execute method of the activity
12-12 12:14:59.800: E/AndroidRuntime(21175):    at android.view.View$1.onClick(View.java:2695)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at android.view.View.performClick(View.java:3122)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at android.view.View$PerformClick.run(View.java:12020)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at android.os.Handler.handleCallback(Handler.java:587)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at android.os.Looper.loop(Looper.java:132)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at android.app.ActivityThread.main(ActivityThread.java:4126)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at java.lang.reflect.Method.invokeNative(Native Method)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at java.lang.reflect.Method.invoke(Method.java:491)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at dalvik.system.NativeStart.main(Native Method)
12-12 12:14:59.800: E/AndroidRuntime(21175): Caused by: java.lang.reflect.InvocationTargetException
12-12 12:14:59.800: E/AndroidRuntime(21175):    at java.lang.reflect.Method.invokeNative(Native Method)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at java.lang.reflect.Method.invoke(Method.java:491)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at android.view.View$1.onClick(View.java:2690)
12-12 12:14:59.800: E/AndroidRuntime(21175):    ... 11 more
12-12 12:14:59.800: E/AndroidRuntime(21175): Caused by: android.database.sqlite.SQLiteException: near "Smith": syntax error: , while compiling: UPDATE bowlers_table SET team_pos=?,substitute=?,team=? WHERE name=Joe Smith
12-12 12:14:59.800: E/AndroidRuntime(21175):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:146)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:367)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:253)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:83)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1829)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1780)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at com.tyczj.bowling.BowlersDB.update(BowlersDB.java:146)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at android.content.ContentProvider$Transport.update(ContentProvider.java:233)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at android.content.ContentResolver.update(ContentResolver.java:847)
12-12 12:14:59.800: E/AndroidRuntime(21175):    at com.tyczj.bowling.Teams.addToTeam(Teams.java:73)
12-12 12:14:59.800: E/AndroidRuntime(21175):    ... 14 more

here is where I update 这是我更新的地方

    public void addToTeam(View v){
    TextView tv = (TextView)findViewById(R.id.bowlersNameTV);
    EditText et = (EditText)findViewById(R.id.posET);
    CheckBox cb = (CheckBox)findViewById(R.id.checkBox1);
    int pos =  Integer.valueOf(et.getText().toString());
    int sub = 0;
    if(cb.isSelected())
        sub = 1;
    ContentValues val = new ContentValues();
    val.put(BowlersDB.TEAM,teamEdited);
    val.put(BowlersDB.POSITION,pos);
    val.put(BowlersDB.SUB,sub);
    getContentResolver().update(BowlersDB.CONTENT_URI,val,BowlersDB.NAME + "=" + tv.getText().toString(),null);
}

and this is my content provider update 这是我的内容提供商更新

@Override
public int update(Uri uri, ContentValues values, String selection,
        String[] selectionArgs) {
    int count = 0;
    int num = uriMatcher.match(uri);
    if(num == 1){
        count = db.update(BOWLERS_TABLE, values, selection, selectionArgs);
    }else if(num == 2){
        count = db.update(BOWLERS_TABLE, values, ID + " = " + uri.getPathSegments().get(1) + (!TextUtils.isEmpty(selection) ? " AND (" + 
                  selection + ')' : ""), 
                  selectionArgs);
    }else{
        throw new IllegalArgumentException(
                "Unknown URI " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return count;
}

where is my syntax wrong? 我的语法哪里出问题了? the error tells me its near the end of the last name but thats it 错误告诉我姓氏的末尾,但仅此而已

:) Caused by: android.database.sqlite.SQLiteException: near "Smith": syntax error: , while compiling: UPDATE bowlers_table SET team_pos=?,substitute=?,team=? :)由以下原因引起:android.database.sqlite.SQLiteException:在“ Smith”附近:语法错误:,而在编译时:UPDATE bowlers_table SET team_pos = ?, substitute = ?, team =? WHERE name=Joe Smith WHERE名称=乔·史密斯

you should enclose Joe Smith in quotes. 您应该将Joe Smith引号引起来。 in this line getContentResolver().update(BowlersDB.CONTENT_URI,val,BowlersDB.NAME + "=" + tv.getText().toString(),null); 在此行中的getContentResolver()。update(BowlersDB.CONTENT_URI,val,BowlersDB.NAME +“ =” + tv.getText()。toString(),null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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