尝试更新行时SQLite语法错误

时间:2011-12-12 17:35:02

标签: android sqlite

我正在尝试使用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

这是我更新的地方

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

这是我的内容提供商更新

@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;
}

我的语法在哪里错了?错误告诉我它接近姓氏的末尾,但就是这样

1 个答案:

答案 0 :(得分:1)

:) 引起:android.database.sqlite.SQLiteException:靠近“Smith”:语法错误:,同时编译:UPDATE bowlers_table SET team_pos = ?, substitute = ?, team =?姓名= Joe Smith

你应该用引号括起Joe Smith。   在这一行     getContentResolver()。update(BowlersDB.CONTENT_URI,val,BowlersDB.NAME +“=”+ tv.getText()。toString(),null);