Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i insert double quote ( " ) into SQLite Database Varchar field?

Tags:

android

sqlite

I'm trying to insert into a table with this field TITLE varchar(200) NOT NULL the next sentence:

INSERT OR IGNORE INTO EXAMPLETABLE VALUES ("1-Preludi \"Obrint Pas 2011\"", ... 

The problem is that SQLite missunderstand the \" escape caracter , and i don't know how to put it to resolve the problem (Maybe a character code?) in my Android program.

like image 984
A.Quiroga Avatar asked Oct 04 '11 11:10

A.Quiroga


3 Answers

To insert values into a database you should always use SQLiteStatement (HowTo) as they escape your values and prevent your app from being targeted with SQL-Injections.

Also, if you're inserting multiple values (say in a loop), executing a precompiled statement with different bound values is much faster than a normal insert-statement.

like image 43
Lukas Knuth Avatar answered Oct 21 '22 13:10

Lukas Knuth


Just working with sqlite and direct sql insert commands from the command line, I find that double quoting escapes itself. With your example

INSERT OR IGNORE INTO EXAMPLETABLE VALUES ("1-Preludi ""Obrint Pas 2011""", ...
like image 134
Nicholas Adams Avatar answered Oct 21 '22 12:10

Nicholas Adams


Try something similar:

ContentValues values = new ContentValues();
values.put("YOURCOLUMNNAME", "1-Preludi \"Obrint Pas 2011\"");
// ... other stuff you want to insert ...
SQLiteDatabase db = ... ; // Usually obtained using a SQLiteOpenHelper
long id = db.insertWithOnConflict("EXAMPLETABLE", BaseColumns._ID, values, SQLiteDatabase.CONFLICT_IGNORE);
like image 33
Jens Avatar answered Oct 21 '22 14:10

Jens