如何在SQLITEOPENHELPER中的MainActivity中更新和删除表中的数据

时间:2015-09-25 09:01:23

标签: android sqlite sql-update android-sqlite main-activity

问题是如何显示来自用户ID的数据。

我为学生创建了一个数据库,其中包含字段名称,姓氏,标记,我确实插入并查看了Sucesfully但我不知道如何更新和删除请帮助。在这里我想根据ID做更新和删除我在DAbaseclas做了更新coaded但是当我要在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});}

1 个答案:

答案 0 :(得分: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;
   }