android - How to handle word with apostrophe? -
i'm filling arraylist cities, 1 of them ("reggio nell'emilia") have apostrophe in name , when select in app crashes. here code:
sqlitedatabase db_read = db.getreadabledatabase(); city = new arraylist<object>(); cursor result; (int =0 ; i<region.size(); i++) { result = db_read.rawquery("select distinct city merchants region='"+region.get(i)+"' order city asc",null); arraylist<string> cities = new arraylist<string>(); while (result.movetonext()) { string city = result.getstring(0); cities.add(city); } city.add(cities); }
here 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 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 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 merchants city='reggio nell'emilia' 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ android.app.activitythread.performlaunchactivity(activitythread.java:2217) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2266) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ android.app.activitythread.access$8 0 0(activitythread.java:145) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ android.app.activitythread$h.handlemessage(activitythread.java:12 06) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ android.os.handler.dispatchmessage(handler.java:1 02) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ android.os.looper.loop(looper.java:136) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ android.app.activitythread.main(activitythread.java:5141) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ java.lang.reflect.method.invokenative(native method) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ java.lang.reflect.method.invoke(method.java:515) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:795) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:611) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ 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 merchants city='reggio nell'emilia' 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ android.database.sqlite.sqliteconnection.nativepreparestatement(native method) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ android.database.sqlite.sqliteconnection.acquirepreparedstatement(sqliteconnection.java:889) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ android.database.sqlite.sqliteconnection.prepare(sqliteconnection.java:5 0 0) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ android.database.sqlite.sqlitesession.prepare(sqlitesession.java:588) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ android.database.sqlite.sqliteprogram.<init>(sqliteprogram.java:58) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ android.database.sqlite.sqlitequery.<init>(sqlitequery.java:37) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ android.database.sqlite.sqlitedirectcursordriver.query(sqlitedirectcursordriver.java:44) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ android.database.sqlite.sqlitedatabase.rawquerywithfactory(sqlitedatabase.java:1314) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ android.database.sqlite.sqlitedatabase.rawquery(sqlitedatabase.java:1253) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ com.example.myapp.listmerchant.createlistfromcity(listmerchant.java:63) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ com.example.myapp.listmerchant.oncreate(listmerchant.java:43) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ android.app.activity.performcreate(activity.java:5231) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1 087) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): @ android.app.activitythread.performlaunchactivity(activitythread.java:2171) 07-26 14:25:4 0.24 0: e/androidruntime(155 06): ... 11 more
i tried use hint:
string city = result.getstring(0); city = city.replace("'", "''");
and fixes error, name double apostrophe:
do know better solution? thank in advance.
using parameters eliminates such formatting problems:
result = db_read.rawquery("select ... region = ? ...", new string[] { region.get(i) });
Comments
Post a Comment