在 SQLite Android 中使用 ContentValues 和 Raw SQL 插入数据有什么区别?
What is the difference between inserting data using ContentValues and Raw SQL in SQLite Android?
我想知道在 SQLlite(Android) 中使用 ContentValues 插入数据和使用 Raw SQL 插入数据的区别,使用内容值有优势吗?
执行插入、读取、删除、更新操作有两种不同的方式:
- 编写参数化查询(推荐)
- 编写原始查询
参数化查询:这些查询使用内置函数执行以插入、读取、删除或更新数据。 SQLiteDatabase
class.
中提供了这些操作相关的函数
原始查询:这些是简单的sql查询,类似于其他数据库,如MySql、Sql服务器等,在本例中为用户将不得不将查询写成文本,并在 rawQuery(String sql,String [] selectionArgs)
或 execSQL(String sql,Object [] bindArgs)
方法中传递查询字符串以执行操作。
重要说明:Android文档不建议使用原始查询来执行插入、读取、更新、删除操作,始终使用SQLiteDatabase
class 的插入、查询、更新、删除功能。
以下是插入数据的原始查询示例:
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();
}
使用原始查询时,我们永远不会知道操作的结果,但是使用参数化查询函数时,会返回一个值来表示操作成功或失败。
Insert:要使用 parameterized query
执行插入操作,我们必须调用 SQLiteDatabase class 中可用的插入函数。 insert()
函数有三个参数,如 public long insert(String tableName,String nullColumnHack,ContentValues values)
其中 tableName
是要插入数据的 table 的名称。
这是一个简单的例子:
//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();
}
有关详细信息,请参阅此 Link
我想知道在 SQLlite(Android) 中使用 ContentValues 插入数据和使用 Raw SQL 插入数据的区别,使用内容值有优势吗?
执行插入、读取、删除、更新操作有两种不同的方式:
- 编写参数化查询(推荐)
- 编写原始查询
参数化查询:这些查询使用内置函数执行以插入、读取、删除或更新数据。 SQLiteDatabase
class.
原始查询:这些是简单的sql查询,类似于其他数据库,如MySql、Sql服务器等,在本例中为用户将不得不将查询写成文本,并在 rawQuery(String sql,String [] selectionArgs)
或 execSQL(String sql,Object [] bindArgs)
方法中传递查询字符串以执行操作。
重要说明:Android文档不建议使用原始查询来执行插入、读取、更新、删除操作,始终使用SQLiteDatabase
class 的插入、查询、更新、删除功能。
以下是插入数据的原始查询示例:
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();
}
使用原始查询时,我们永远不会知道操作的结果,但是使用参数化查询函数时,会返回一个值来表示操作成功或失败。
Insert:要使用 parameterized query
执行插入操作,我们必须调用 SQLiteDatabase class 中可用的插入函数。 insert()
函数有三个参数,如 public long insert(String tableName,String nullColumnHack,ContentValues values)
其中 tableName
是要插入数据的 table 的名称。
这是一个简单的例子:
//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();
}
有关详细信息,请参阅此 Link