[英]Android sqlite update not done in table
Following is my code for update record done. 以下是我完成更新记录的代码。
try {
String str_edit = edit_note.getText().toString();
Toast.makeText(getApplicationContext(), str_edit,
Toast.LENGTH_LONG).show();
Toast.LENGTH_LONG).show();
String rawQuery="UPDATE " + GlobalVariable.getstr_tbl_name()
+ " SET note = '" + str_edit + "' where name = '"
+ str + "' ";
db.execSQL(rawQuery);
} catch (Exception e) {
System.out.println(e.getMessage());
}
Code for Display: 显示代码:
try {
Cursor note_cursor = db.query(GlobalVariable.getstr_tbl_name(),
new String[] { "note" + " as note" }, "name" + "=?",
new String[] { GlobalVariable.getstr_food_name() }, null,
null, null);
if (note_cursor.moveToFirst()) {
int notecol = note_cursor.getColumnIndex("note");
do {
String str = note_cursor.getString(notecol);
Toast.makeText(getApplicationContext(), str,
Toast.LENGTH_LONG).show();
edit_note.setText(str);
} while (note_cursor.moveToNext());
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
I am getting all variable value ie global variables and all but update does not reflects on table. 我正在获取所有变量值,即全局变量,除更新外的所有变量都未反映在表上。 What wrong i am done?
我做错了什么?
每当我们尝试更新数据库时,只需清理并卸载您的应用程序,然后再次安装,如果您发现正确,则在不卸载时可能会发生一些更改,请告诉其他明智的做法,我们将在下一个
Should int notecol = note_cursor.getColumnIndex("name");
应该为
int notecol = note_cursor.getColumnIndex("name");
instead be int notecol = note_cursor.getColumnIndex("note");
而是
int notecol = note_cursor.getColumnIndex("note");
since in the first block of code you are updating note.. 因为在第一段代码中您正在更新注释。
The problem is that you try to update your DB using a rawQuery
method instead of a execSql
. 问题是,你尝试使用更新您的数据库
rawQuery
方法,而不是一个execSql
。 RawQuery
is intended to retrieve data from your database, while execSql
lets you RawQuery
旨在从数据库中检索数据,而execSql
允许您
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
执行一个不是SELECT的SQL语句或任何其他返回数据的SQL语句。
You can also consider to use public int update (String table, ContentValues values, String whereClause, String[] whereArgs)
method. 您也可以考虑使用
public int update (String table, ContentValues values, String whereClause, String[] whereArgs)
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.