admin管理员组文章数量:1404352
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("'", "''");
版权声明:本文标题:javascript - Difficulty in escaping single quote character while SQLite database insertion - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744785470a2624969.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论