2

How do we insert strings that have commas (,) in them into SQLite database using Android ?

If there are no commas within the values to be inserted, then insertion happens fine. Say for example see the Android code:-

db.execSQL("insert into Student ( id, name, mark ) values (1, 'Geevarghese', 100)");

Inserting into the db works well.

If I modify my question to add the address to the database, like the following,

db.execSQL("insert into Student ( id, name, mark, address ) values (1, 'Geevarghese', 100, 'No.20, Cochin, Kerala')");

This query doesn't insert data into the database

How to properly insert data that includes comma into SQLite db from Android ?

1
  • use scape sequence \ for each special character that you want as string to be inserted in db Commented Sep 20, 2014 at 6:15

2 Answers 2

3

You can use ContentValues as follow:

ContentValues initialValues = new ContentValues();

initialValues.put("id", 1);
initialValues.put("name", "Geevarghese");
initialValues.put("mark", 100);
initialValues.put("address", "No.20, Cochin, Kerala");

db.insert("Student", null, initialValues);

Ref:http://developer.android.com/reference/android/content/ContentValues.html

Hope It helps :)

Sign up to request clarification or add additional context in comments.

Comments

0
String data = "No.20, Cochin, Kerala";
data = data.replaceAll("," , "\,");


db.execSQL("insert into Student ( id, name, mark, address ) values (1, 'Geevarghese', 100, '"+data+"')");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.