I have to insert certain text in database which may contain characters with single quotes and double quotes.
For eg: "Lumix" or something like Vijay's
I have successfully escaped the double quotes like this:
if(fdata.search('"') != -1) {
fdata = fdata.replace(/(['"])/g, "\\$1");
}
But I am not understanding how to escape the single quotes. Without escaping the single quotes SQLite is not accepting the data. How to solve the issue? Thanks in advance!
I have to insert certain text in database which may contain characters with single quotes and double quotes.
For eg: "Lumix" or something like Vijay's
I have successfully escaped the double quotes like this:
if(fdata.search('"') != -1) {
fdata = fdata.replace(/(['"])/g, "\\$1");
}
But I am not understanding how to escape the single quotes. Without escaping the single quotes SQLite is not accepting the data. How to solve the issue? Thanks in advance!
Share Improve this question asked Feb 25, 2013 at 10:52 kittu88kittu88 2,4615 gold badges41 silver badges82 bronze badges 3- 1 Doesn't your database API give you bound variables or an SQLite-specific escaping function? – Quentin Commented Feb 25, 2013 at 10:54
- It is written in javascript, titanium appcelerator, not java native – kittu88 Commented Feb 25, 2013 at 10:57
- @kittu88: So? Titanium Appcelerator includes a database API, which does support bound variables (aka parameters). – Martijn Commented Feb 25, 2013 at 18:19
3 Answers
Reset to default 4Use parameters, then you don't need to escape anything:
db.execute('INSERT INTO MyTable(ID, Name) VALUES(?, ?)', 123, name);
CL.'s answer is the by far best solution, but if you want to make this work without rewriting your code you could add this function:
function removeQuotes(str){
return str.replace(/'/g, "'").replace(/"/g,""");
}
Usage:
fdata = removeQuotes(fdata);
That worked for me
Use replaceAll
method for replacing '(single quote)
with space
as below:
String name= name.replaceAll("'", "''");
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744785470a4593596.html
评论列表(0条)