Single quotes in sqlite on android
I am getting the value from webservice. Some of my values come with single quotes. Like this sample's sqlite is not supporting single quotes. So I replace them with double quotes. While I then retrieve the value from db, I am not able to parse the value. I am getting parser exception not wellformed. Can anybody tell me how to do this?
this is my code replacing single quote to double quote
stockservicevalue=stockservicevalue.replaceAll("'", "\"");
this is my retrieving value from db
String sql = "SELECT * FROM MyQuest;";
The log:
03-28 19:02:36.309: WARN/System.err(344): org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 50437: not well-formed (invalid token)
03-28 19:02:36.321: WARN/System.err(344): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:492)
03-28 19:02:36.330: WARN/System.err(344): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:477)
03-28 19:02:36.340: WARN/System.err(344): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:317)
03-28 19:02:36.380: WARN/System.err(344): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:273)
03-28 19:02:36.389: WARN/System.err(344): at com.inquest.SaxPortfolioDocument.getEventsFromAnXML(SaxPortfolioDocument.java:13)
03-28 19:02:36.411: WARN/System.err(344): at com.inquest.PortfolioActivity$PortFolioTask.doInBackground(PortfolioActivity.java:171)
03-28 19:02:36.420: WARN/System.err(344): at com.inquest.PortfolioActivity$PortFolioTask.doInBackground(PortfolioActivity.java:1)
03-28 19:02:36.430: WARN/System.err(344): at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-28 19:02:36.430: WARN/System.err(344): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-28 19:02:36.440: WARN/System.err(344): at java.util.co开发者_StackOverflow社区ncurrent.FutureTask.run(FutureTask.java:137)
03-28 19:02:36.479: WARN/System.err(344): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-28 19:02:36.500: WARN/System.err(344): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-28 19:02:36.510: WARN/System.err(344): at java.lang.Thread.run(Thread.java:1096)
03-28 19:02:36.840: WARN/InputManagerService(58): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@46dfec78
Thanks
stockservicevalue=stockservicevalue.replaceAll("'", "\''");
We can make use android DatabaseUtils class.
stockservicevalue= DatabaseUtils.sqlEscapeString(stockservicevalue);
I think the correct format would be stockservicevalue=stockservicevalue.replaceAll("\'", "\"");
How to insert single quote with SQLite Java. It works for me.
String str = "'Hello";
str = str.replace("'","\\'");
精彩评论