问题是如何显示来自用户 ID 的数据。
我为具有名称、姓氏、标记字段的学生创建了一个数据库,我确实成功地插入和查看,但我不知道如何进行更新和删除,请帮忙。在这里,我想根据 ID 进行更新和删除我在 DAbaseclas 中进行了更新编码,但是当我要在 MainActivity 中声明时,我在这里遇到了一个错误,我附上了一张图片
我的数据库处理程序类
public class DatabaseHandler extends SQLiteOpenHelper {
private static final String DATABASE_NAME="students.db";
private static final String TABLE_NAME="student_table";
private static final String COL_1="ID";
private static final String COL_2="NAME";
private static final String COL_3="SURNAME";
private static final String COL_4="MARKS";
private static final int DATABASE_VERSION=1;
public DatabaseHandler(Context context) {
super(context,DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL( "Create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");
Log.d("oncreate","Table Was Created"); }@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST " + TABLE_NAME);
onCreate(db);
}
public boolean insertdata(String name,String surname,String marks)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues= new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,marks);
long result=db.insert(TABLE_NAME,null,contentValues);
if (result==-1)
return false;
else
return true;
}
public Cursor getalldata(){
SQLiteDatabase db=this.getReadableDatabase();
Cursor res=db.rawQuery("SELECT * FROM " +TABLE_NAME,null);
return res;
}public int Update(int id,String Name,String surname,string marks){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues= new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,marks);return db.update(TABLENAME, contentValues,"ID=?",new String[] {id});}