javascript - Difficulty in escaping single quote character while SQLite database insertion - Stack Overflow

I have to insert certain text in database which may contain characters with single quotes and double qu

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
Add a ment  | 

3 Answers 3

Reset to default 4

Use 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信