您可以使用 SQLiteDatabse update() 将 table 中的列设置为等于另一列吗?
Can you set a column equal to another column in a table using SQLiteDatabse update()?
我需要使一列等于 table 中的另一列。我无法使用 SQLiteDatabase.
的更新方法弄明白
我知道 SQL 语句是:
UPDATE coolTable SET columnA = columnB;
我是否将它放在我传递函数的 ContentValues 中?或选择字符串?
execSQL()
错误检查:
SQLiteDatabase db = getReadableDatabase();
try {
long count = DatabaseUtils.queryNumEntries(db, "pweb");
db.execSQL("update pweb set h_id = _id;");
long rows = DatabaseUtils.longForQuery(db, "SELECT changes()", null);
if(count != rows ) Log.e("wrong count","update failed");
}
catch(SQLException e){
Log.e("SQLException","update failed");
}
db.close();
but I was wondering if it is possible to use the database's update()
function instead of execSQL()
您可以使用 sqlite3
查看和操作您的数据,并测试 sql
命令。
创建 table:
CREATE TABLE pweb (_id INTEGER PRIMARY KEY,h_id INTEGER ,sent INTEGER);
查看原始行:
sqlite> select * from pweb;
1|10|1
2|20|1
3|30|0
4|40|0
执行列更新:
sqlite> update pweb set h_id = _id;
查看所有行的变化:
sqlite> select * from pweb;
1|1|1
2|2|1
3|3|0
4|4|0
更复杂的东西?
sqlite> update pweb set h_id = _id + 1;
结果:
sqlite> select * from pweb;
1|2|1
2|3|1
3|4|0
4|5|0
您只能使用 update()
来设置文字值(作为绑定参数),不能使用 sqlite SQL syntax 支持的任何其他类型的表达式。
使用 execSQL()
执行带有列名表达式的原始 UPDATE
查询。
我需要使一列等于 table 中的另一列。我无法使用 SQLiteDatabase.
的更新方法弄明白我知道 SQL 语句是:
UPDATE coolTable SET columnA = columnB;
我是否将它放在我传递函数的 ContentValues 中?或选择字符串?
execSQL()
错误检查:
SQLiteDatabase db = getReadableDatabase();
try {
long count = DatabaseUtils.queryNumEntries(db, "pweb");
db.execSQL("update pweb set h_id = _id;");
long rows = DatabaseUtils.longForQuery(db, "SELECT changes()", null);
if(count != rows ) Log.e("wrong count","update failed");
}
catch(SQLException e){
Log.e("SQLException","update failed");
}
db.close();
but I was wondering if it is possible to use the database's update() function instead of execSQL()
您可以使用 sqlite3
查看和操作您的数据,并测试 sql
命令。
创建 table:
CREATE TABLE pweb (_id INTEGER PRIMARY KEY,h_id INTEGER ,sent INTEGER);
查看原始行:
sqlite> select * from pweb;
1|10|1
2|20|1
3|30|0
4|40|0
执行列更新:
sqlite> update pweb set h_id = _id;
查看所有行的变化:
sqlite> select * from pweb;
1|1|1
2|2|1
3|3|0
4|4|0
更复杂的东西?
sqlite> update pweb set h_id = _id + 1;
结果:
sqlite> select * from pweb;
1|2|1
2|3|1
3|4|0
4|5|0
您只能使用 update()
来设置文字值(作为绑定参数),不能使用 sqlite SQL syntax 支持的任何其他类型的表达式。
使用 execSQL()
执行带有列名表达式的原始 UPDATE
查询。