Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.database.sqlite.SQLiteException: unrecognized token:

I try to update database table and this is my code:

public void updatefiletable(String filename, String v1, String v2){
  AndroidOpenDbHelper androidOpenDbHelperObj = new AndroidOpenDbHelper(this);
  SQLiteDatabase sqliteDatabase = androidOpenDbHelperObj.getWritableDatabase();

  ContentValues values = new ContentValues();
  values.put(AndroidOpenDbHelper.COLLUMN_NAME_FILE_CLOUD, v1);
  values.put(AndroidOpenDbHelper.COLLUMN_NAME_FILE_DATE_UPLOADING, v2);
  sqliteDatabase.update(AndroidOpenDbHelper.TABLE_FILE, values, AndroidOpenDbHelper.COLUMN_NAME_FILE_NAME+"="+filename, null);

  sqliteDatabase.close();    
}

when I call my method with file_name values egal priv_priv_secondfile_2012-06-15.pdf I get that in the logcat:

android.database.sqlite.SQLiteException: unrecognized token: "15.pdf": ,
while compiling: UPDATE file_table SET file_cloud_column=?, 
file_date_upload_column=? 
WHERE file_name_column=priv_priv_secondfile_2012-06-15.pdf

how can I fix it?

like image 227
user1381760 Avatar asked Dec 03 '22 03:12

user1381760


1 Answers

You need to escape the filename parameter. The punctuation in the filename is confusing SQLite. You could do it by surrounding the filename in 'single quotes' in the string you pass in to SQLite, but it's cleaner and safer to pass it as a separate argument, like this:

sqliteDatabase.update(AndroidOpenDbHelper.TABLE_FILE, values,
        AndroidOpenDbHelper.COLUMN_NAME_FILE_NAME+"=?", new String[] {filename});
like image 178
Graham Borland Avatar answered Dec 27 '22 16:12

Graham Borland