How to handle word with apostrophe?

a 夏天 提交于 2019-12-12 02:26:37

问题


I'm filling an arraylist with some cities, but one of them ("Reggio nell'Emilia") have an apostrophe in its name and when i select it in the app it crashes. Here is the code:

SQLiteDatabase db_read = db.getReadableDatabase();
city = new ArrayList<Object>();
Cursor result;
for (int i =0 ; i<region.size(); i++) {
    result = db_read.rawQuery("select distinct city from merchants where region='"+region.get(i)+"' order by city ASC",null);
    ArrayList<String> cities = new ArrayList<String>();
    while (result.moveToNext()) {
        String city = result.getString(0);
        cities.add(city);
    }
    city.add(cities);
}

Here is the error in LogCat:

07-26 14:25:4    0.11    0: I/Timeline(155    06): Timeline: Activity_launch_request id:com.example.myapp time:1    0873122
07-26 14:25:4    0.225: E/SQLiteLog(155    06): (1) near "Emilia": syntax error
07-26 14:25:4    0.225: D/AndroidRuntime(155    06): Shutting down VM
07-26 14:25:4    0.225: W/dalvikvm(155    06): threadid=1: thread exiting with uncaught exception (group=    0x41f17ce    0)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06): FATAL EXCEPTION: main
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06): Process: com.example.myapp, PID: 155    06
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.ListMerchant}: android.database.sqlite.SQLiteException: near "Emilia": syntax error (code 1): , while compiling: Select _id,title,phone,comune from merchants where city='Reggio nell'Emilia'
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2217)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2266)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at android.app.ActivityThread.access$8    0    0(ActivityThread.java:145)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:12    06)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at android.os.Handler.dispatchMessage(Handler.java:1    02)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at android.os.Looper.loop(Looper.java:136)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at android.app.ActivityThread.main(ActivityThread.java:5141)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at java.lang.reflect.Method.invokeNative(Native Method)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at java.lang.reflect.Method.invoke(Method.java:515)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at dalvik.system.NativeStart.main(Native Method)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06): Caused by: android.database.sqlite.SQLiteException: near "Emilia": syntax error (code 1): , while compiling: Select _id,title,phone,comune from merchants where city='Reggio nell'Emilia'
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:5    0    0)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at com.example.myapp.ListMerchant.createListFromCity(ListMerchant.java:63)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at com.example.myapp.ListMerchant.onCreate(ListMerchant.java:43)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at android.app.Activity.performCreate(Activity.java:5231)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1    087)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2171)
07-26 14:25:4    0.24    0: E/AndroidRuntime(155    06):    ... 11 more

I tried to use this hint:

String city = result.getString(0);
city = city.replace("'", "''");

and it fixes the error, but i get name with double apostrophe:

Do you know a better solution? Thank you in advance.


回答1:


Using parameters eliminates such formatting problems:

result = db_read.rawQuery("select ... where region = ? ...",
                          new String[] { region.get(i) });



回答2:


You should make use of SqliteQueryBuilder to make a safe query:

SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
// Set the table
queryBuilder.setTables('Table_Name');

SQLiteDatabase db = database.getWritableDatabase();
Cursor cursor = queryBuilder.query(db, projection, selection,
    selectionArgs, null, null, sortOrder);

Good luck.

Edited:

Also there is a util method in 'DatabaseUtils` class in andorid the help you to scape unwanted characters:

value = DatabaseUtils.sqlEscapeString(value);



回答3:


I finally solved the problem using the hint (that one described in my question) in another part of the code (it was used as extra in another activity).



来源:https://stackoverflow.com/questions/25365651/how-to-handle-word-with-apostrophe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!