suja
suja

Reputation: 1288

Insert into SQLite

I am trying to insert text messages from an inbox to a SQLite Database, but due to some special characters/symbols it doesn't insert properly.

I went through some question in Stackoverflow but nothing seems useful.

I got this code, but it is not working.

data.execSQL("INSERT INTO recor(text) VALUES('"+DatabaseUtils.sqlEscapeString(messag)+"')");

My database has only one field in it and am trying to insert details of messages along with it. I am putting the details (type, time, number, message) to a single string message and recor is my table name.

This is what I get as toast when I use a try catch loop.

Error is near:

"FROM":syntax error:INSERT INTO recor(text) VALUES("FROM 15555215556 Message:-MSG")

Upvotes: 2

Views: 1511

Answers (2)

Ian Newson
Ian Newson

Reputation: 7949

Your final SQL string seems to include two sets of quotes around the string you are inserting, so I assume the DatabaseUtils.sqlEscapeString method adds its own quotes around the string.

Therefore, your code should be:

data.execSQL("INSERT INTO recor(text) VALUES("+DatabaseUtils.sqlEscapeString(messag)+")");

Upvotes: 0

azgolfer
azgolfer

Reputation: 15137

Uses the DatabaseAdapter's insert method instead. e.g.

ContentValues values = new ContentValues();
values.put(COLUMN_NAME, value);
dbAdapter.insert(TABLE_NAME, null, values);

it looks like your column name is 'text'? this must be wrong, as text is a keyword in sqlite.

Upvotes: 2

Related Questions