kittu88
kittu88

Reputation: 2461

Difficulty in escaping single quote character while SQLite database insertion

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!

Upvotes: 2

Views: 5461

Answers (3)

RobertH
RobertH

Reputation: 365

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

Upvotes: 1

CL.
CL.

Reputation: 180070

Use parameters, then you don't need to escape anything:

db.execute('INSERT INTO MyTable(ID, Name) VALUES(?, ?)', 123, name);

Upvotes: 4

Avadhani Y
Avadhani Y

Reputation: 7636

Use replaceAll method for replacing '(single quote) with space as below:

     String name= name.replaceAll("'", "''");

Upvotes: 0

Related Questions