Geevarghese
Geevarghese

Reputation: 105

Inserting string to SQLite using Android

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 ?

Upvotes: 2

Views: 3249

Answers (2)

Vivek Mishra
Vivek Mishra

Reputation: 352

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 :)

Upvotes: 3

Digvesh Patel
Digvesh Patel

Reputation: 6533

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


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

Upvotes: 0

Related Questions