如何用撇号处理单词?

时间:2014-08-18 14:24:11

标签: android string sqlite double apostrophe

我在某些城市填写了一个arraylist,但其中一个(" Reggio nell' Emilia")在其名称中有一个撇号,当我在应用程序中选择它时崩溃。这是代码:

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

以下是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

我尝试使用此提示:

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

它修正了错误,但是我得到了双撇号的名字:

enter image description here

您知道更好的解决方案吗?提前谢谢。

3 个答案:

答案 0 :(得分:2)

使用参数消除了这种格式化问题:

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

答案 1 :(得分:0)

您应该使用SqliteQueryBuilder进行安全查询:

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);
祝你好运。

<强>编辑:

在'DatabaseUtils`类中还有一个util方法可以帮助你排除不需要的字符:

value = DatabaseUtils.sqlEscapeString(value);

答案 2 :(得分:0)

我终于在代码的另一部分使用提示(在我的问题中描述的那个)解决了问题(它在另一个活动中被用作额外的)。