2015-09-25 78 views
1

問題是如何顯示來自用戶標識的數據。如何更新和刪除SQLiteOPENHELPER中MainActivity中的表格數據

我創建了一個數據庫的學生與字段名稱,姓氏,標記和我沒有插入和查看Sucesfully,但我不知道如何做更新和刪除請幫助。在這裏,我想基於ID我做了一個更新coading在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});} 
+0

你'update'方法需要4個參數和你逝去的只有3個,約'id'你是不是通過什麼在數據庫處理程序'update'方法 –

+0

? – Karthick

+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}); 

    } 

從標識獲取數據:

public Cursor getData(int id){ 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor res = db.rawQuery("select * from contacts where id="+id+"", null); 
     return res; 
    } 
+0

謝謝,但是當給id和點擊顯示細節想加載我們如何做到這一點(即)「顯示」按鈕單擊事件 – Karthick

+0

你需要從用戶id獲取數據,然後吐司該數據。 –

+0

如果我上面的答案是正確的,並且滿足你想要的,那麼..接受它。 –