Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape? data that is being inserted into sqlite database.

Tags:

sqlite

cordova

I'm making an apple app using phone gap (which uses sqlite database).

All my inserts work fine except for when I try to insert a weblink. This errors because there are " characters in the weblink.

Using the following:

var content = 'hello, this is my <a href="www.google.com">link</a>'
tx.executeSql('UPDATE PAGES SET content="'+content+'" WHERE id="1"');

Brings back the following error

error code 1
error: "near "http": syntax error"

If I remove the website address, I don't get an error. I have tried: content = escape(content);

but that hasn't worked.

like image 468
Billie Avatar asked Sep 30 '11 09:09

Billie


2 Answers

Use parameter binding. This is the right and the safest way to do what you're trying to do.

tx.executeSql('UPDATE PAGES SET content=? WHERE id=1', [content]);
like image 173
hamstergene Avatar answered Oct 06 '22 03:10

hamstergene


To escape quotes in SQLite you'll have to repeat the quotes.

So to insert a 7" screen you'll have to put a 7"" screen in the INSERT statement.

like image 44
Dylan Avatar answered Oct 06 '22 02:10

Dylan