I want to know the difference between inserting data using ContentValues and inserting data using Raw SQL in SQLlite(Android), Is there a advantage using content values?
-
https://developer.android.com/reference/android/content/ContentValues.html – IntelliJ Amiya Apr 17 '17 at 06:12
1 Answers
To perform insert, read, delete, update operation there are two different ways:
- Write parameterized queries (Recommended)
- Write raw queries
Parameterized Queries: These are those queries which are performed using inbuilt functions to insert, read, delete or update data. These operation related functions are provided in SQLiteDatabase
class.
Raw Queries: These are simple sql queries similar to other databases like MySql, Sql Server etc, In this case user will have to write query as text and passed the query string in rawQuery(String sql,String [] selectionArgs)
or execSQL(String sql,Object [] bindArgs)
method to perform operations.
Important Note: Android documentation don’t recommend to use raw queries to perform insert, read, update, delete operations, always use SQLiteDatabase
class’s insert, query, update, delete functions.
Following is an example of raw query to insert data:
public void insertItem(Item item) {
String query = "INSERT INTO " + ItemTable.NAME + " VALUES (0,?,?)";
SQLiteDatabase db = getWritableDatabase();
db.execSQL(query, new String[]{item.name, item.description});
db.close();
}
While using raw queries we never come to know the result of operation, however with parameterized queries function a value is returned for success or failure of operation.
Insert: To perform insert operation using parameterized query
we have to call insert function available in SQLiteDatabase class. insert()
function has three parameters like public long insert(String tableName,String nullColumnHack,ContentValues values)
where tableName
is name of table in which data to be inserted.
Here is simple example:
//Item is a class representing any item with id, name and description.
public void addItem(Item item) {
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name",item.name);
// name - column
contentValues.put("description",item.description);
// description is column in items table, item.description has value for description
db.insert("Items", null, contentValues);//Items is table name
db.close();
}
For more Information see this Link

- 23,683
- 6
- 47
- 62
-
This answer is wrong: `rawQuery` and `execSQL` can be parameterized; this is what `selectionArgs`/`bindArgs` are for. – CL. Apr 17 '17 at 07:49
-
i never said that `raw queries` can not be parameterized! also add the function definition `rawQuery(String sql,String [] selectionArgs)` , `execSQL(String sql,Object [] bindArgs)` ...in answer.. – rafsanahmad007 Apr 17 '17 at 07:52
-
"Parameterized queries" are queries with parameters, so this name would fit the other functions better (`insert` does not even use parameters *explicitly*). You should use a better description for those functions. – CL. Apr 17 '17 at 07:59