如何使用撇处理词吗? [英] How to handle word with apostrophe?

查看:172
本文介绍了如何使用撇处理词吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我填充一些城市一个ArrayList,但在它的名字其中之一(雷焦艾米利亚)有一个撇号,当我在应用程序中选择它崩溃。这里是code:

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

下面是LogCat中的错误:

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.

推荐答案

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

Using parameters eliminates such formatting problems:

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

这篇关于如何使用撇处理词吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆