android.database.sqlite.SQLiteException:无法识别的令牌:

时间:2022-01-23 23:02:49

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:

当我用file_name值调用我的方法egal priv_priv_secondfile_2012-06-15.pdf我在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?

我该怎么解决?

1 个解决方案

#1


17  

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:

您需要转义filename参数。文件名中的标点符号使SQLite感到困惑。您可以通过将传递给SQLite的字符串中的“单引号”包围文件名来实现,但将它作为单独的参数传递更简洁,更安全,如下所示:

sqliteDatabase.update(AndroidOpenDbHelper.TABLE_FILE, values,
        AndroidOpenDbHelper.COLUMN_NAME_FILE_NAME+"=?", new String[] {filename});

#1


17  

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:

您需要转义filename参数。文件名中的标点符号使SQLite感到困惑。您可以通过将传递给SQLite的字符串中的“单引号”包围文件名来实现,但将它作为单独的参数传递更简洁,更安全,如下所示:

sqliteDatabase.update(AndroidOpenDbHelper.TABLE_FILE, values,
        AndroidOpenDbHelper.COLUMN_NAME_FILE_NAME+"=?", new String[] {filename});