如何更新 SQLite android 中现有列的值? (无法在我的项目中执行)
How to update the value of the existing columns in SQLite android ? (not able to do in my project)
我正在尝试更新 table 的现有专栏,但我做不到....
没有错误,只是没有得到更新。
我的代码在下面
通过传递值调用函数 a 是我要更改的 _id,i 是我要插入的值。
boolean isUpdate = mDbHelper.updatedata(String.valueOf(a),String.valueOf(i));
我用来更改值的函数
public boolean updatedata(String id,String books){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Lib_student.COLUMN_STUDENT_BOOKS,books);
Cursor cursor = db.rawQuery("select * from Library_Student where books=?",new String[]{books});
long r = db.update("Library_Student",contentValues,"books=?",new String[]{books});
if (r == -1){
return false;
}else {
return true;
}
}
这是我需要编辑的 table..
String SQL_CREATE_LIBRARY_TABLE_STUDENT = "CREATE TABLE "+ Lib_student.TABLE_NAME + " ("
+Lib_student._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+Lib_student.COLUMN_STUDENT_NAME+ " TEXT NOT NULL, "
+Lib_student.COLUMN_STUDENT_EMAIL+ " TEXT, "
+Lib_student.COLUMN_STUDENT_AGE+ " INTEGER , "
+Lib_student.COLUMN_STUDENT_GENDER+ " TEXT ,"
+Lib_student.COLUMN_STUDENT_NUMBER+ " INTEGER ,"
+Lib_student.COLUMN_STUDENT_ADDRESS+ " TEXT ,"
+Lib_student.COLUMN_STUDENT_BOOKS + " INTEGER );";
首先,不需要 select 您要更新的行,因此删除此行:
Cursor cursor = db.rawQuery("select * from Library_Student where books=?",new String[]{books});
此外,您必须将 id
而不是 books
的值作为参数传递给方法 update()
:
public boolean updatedata(String id, String books) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Lib_student.COLUMN_STUDENT_BOOKS, books);
return db.update("Library_Student", contentValues, Lib_student._ID + " = ?", new String[]{id}) > 0;
}
方法 update()
returns 更新行的数量(它永远不会 returns -1
),所以你必须将该数字与 0
和 return true
如果它大于 0
或 false
如果它是 0
.
我正在尝试更新 table 的现有专栏,但我做不到.... 没有错误,只是没有得到更新。 我的代码在下面
通过传递值调用函数 a 是我要更改的 _id,i 是我要插入的值。
boolean isUpdate = mDbHelper.updatedata(String.valueOf(a),String.valueOf(i));
我用来更改值的函数
public boolean updatedata(String id,String books){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Lib_student.COLUMN_STUDENT_BOOKS,books);
Cursor cursor = db.rawQuery("select * from Library_Student where books=?",new String[]{books});
long r = db.update("Library_Student",contentValues,"books=?",new String[]{books});
if (r == -1){
return false;
}else {
return true;
}
}
这是我需要编辑的 table..
String SQL_CREATE_LIBRARY_TABLE_STUDENT = "CREATE TABLE "+ Lib_student.TABLE_NAME + " ("
+Lib_student._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+Lib_student.COLUMN_STUDENT_NAME+ " TEXT NOT NULL, "
+Lib_student.COLUMN_STUDENT_EMAIL+ " TEXT, "
+Lib_student.COLUMN_STUDENT_AGE+ " INTEGER , "
+Lib_student.COLUMN_STUDENT_GENDER+ " TEXT ,"
+Lib_student.COLUMN_STUDENT_NUMBER+ " INTEGER ,"
+Lib_student.COLUMN_STUDENT_ADDRESS+ " TEXT ,"
+Lib_student.COLUMN_STUDENT_BOOKS + " INTEGER );";
首先,不需要 select 您要更新的行,因此删除此行:
Cursor cursor = db.rawQuery("select * from Library_Student where books=?",new String[]{books});
此外,您必须将 id
而不是 books
的值作为参数传递给方法 update()
:
public boolean updatedata(String id, String books) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Lib_student.COLUMN_STUDENT_BOOKS, books);
return db.update("Library_Student", contentValues, Lib_student._ID + " = ?", new String[]{id}) > 0;
}
方法 update()
returns 更新行的数量(它永远不会 returns -1
),所以你必须将该数字与 0
和 return true
如果它大于 0
或 false
如果它是 0
.