0

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.

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

1 Answer 1

1

:) Caused by: android.database.sqlite.SQLiteException: near "Smith": syntax error: , while compiling: UPDATE bowlers_table SET team_pos=?,substitute=?,team=? WHERE name=Joe Smith

you should enclose Joe Smith in quotes. in this line getContentResolver().update(BowlersDB.CONTENT_URI,val,BowlersDB.NAME + "=" + tv.getText().toString(),null);

Sign up to request clarification or add additional context in comments.

3 Comments

but the name is from a textview so I cant, the name is selected and show information about the person then if i want to change something on there I use the name as the WHERE
i would rather let sqlite do the values escaping. but in your case the easiest will be just adding quotes getContentResolver().update(BowlersDB.CONTENT_URI,val,BowlersDB.NAME + "\" + tv.getText().toString()+"\"",null); though, that won't help when you have quote sign in the name, so you will have to escape it anyway
just wondering, why you don't want to make something like this? getContentResolver().update(BowlersDB.CONTENT_URI,val,BowlersDB.NAME + "= ?",new String[] { tv.getText().toString() });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.