2

问题是如何显示来自用户 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});}
4

1 回答 1

1

在 MainActivity.java 中:

 private void deleteData() {
        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer deleteAccount = databaseHelperAdapter.deleteAccount(tvEmail.getText().toString());
                if (deleteAccount > 0) {
                    Toast.makeText(Welcome.this, "Data Deleted ", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(Welcome.this, "Data Not Deleted ", Toast.LENGTH_LONG).show();
                }
            }
        });
    }


    public void UpdateData() {
        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String gender = genderspinner.getSelectedItem().toString();
                String image = imUserPhoto.toString();

                boolean isUpdate = databaseHelperAdapter.updateData(tvEmail.getText().toString(),
                        etFname.getText().toString(),
                        etLname.getText().toString(),
                        etPassword.getText().toString(), gender, image);
                if (isUpdate == true) {
                    Toast.makeText(Welcome.this, "Data Updated ", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(Welcome.this, "Data Not Updated ", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

在 Database.java 中:

  public boolean updateData(String name, String fname, String lname, String password, String gender, String image) {
        SQLiteDatabase sqLiteDatabase = helper.getWritableDatabase();

        ContentValues contentValues = new ContentValues();
        contentValues.put(DatabaseHelper.EMAIL, email);
        contentValues.put(DatabaseHelper.FNAME, fname);
        contentValues.put(DatabaseHelper.LNAME, lname);
        contentValues.put(DatabaseHelper.PASSWORD, password);
        contentValues.put(DatabaseHelper.GENDER, gender);
        contentValues.put(DatabaseHelper.IMAGE, image);
        sqLiteDatabase.update(DatabaseHelper.TABLE_NAME, contentValues, DatabaseHelper.EMAIL + " =? ", new String[]{email});
        return true;

    }

    public int deleteAccount(String email) {
        SQLiteDatabase sqLiteDatabase = helper.getWritableDatabase();
        return sqLiteDatabase.delete(DatabaseHelper.TABLE_NAME, DatabaseHelper.EMAIL + " =?", new String[]{email});

    }

从 Id 获取数据:

 public Cursor getData(int id){
      SQLiteDatabase db = this.getReadableDatabase();
      Cursor res =  db.rawQuery( "select * from contacts where id="+id+"", null );
      return res;
   }
于 2015-09-25T09:13:56.970 回答