添加更新删除和搜索

时间:2018-10-15 08:37:06

标签: android

谁能帮我发送代码以在Android Studio中添加添加更新删除 我已经完成更新,但无法正常工作

 public boolean updateInfor(String username, String dob, String password, String gender){
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues con = new ContentValues();
    con.put(UserProfile.Users.DOB, dob);
    con.put(UserProfile.Users.PASSWORD, password);
    con.put(UserProfile.Users.GENDER, gender);

    String where = UserProfile.Users.USERNAME + " = ?";
    String[] wheree = {username};

    long r = db.update(UserProfile.Users.TABLE_NAME, con, where, wheree);

    if(r == 1)
        return false;
    else
        return true;
}

2 个答案:

答案 0 :(得分:0)

  public void deleteInfo(String Username){
    SQLiteDatabase db = this.getWritableDatabase();
    String where = UserProfile.Users.USERNAME + "=?";
    String[] whereeee = {Username};

    db.delete(UserProfile.Users.TABLE_NAME,where,whereeee);
}

    public Cursor readAllInfor(String username){
    SQLiteDatabase db = this.getWritableDatabase();

    String[] col = {UserProfile.Users.USERNAME, UserProfile.Users.DOB, UserProfile.Users.PASSWORD, UserProfile.Users.GENDER};

    String where = UserProfile.Users.USERNAME + " = ?";
    String[] wheree = {username};

    Cursor cursor = db.query(UserProfile.Users.TABLE_NAME,
            col,
            where,
            wheree,
            null,
            null,
            null);

    return cursor;
}


public boolean updateInfor(String username, String dob, String password, String gender){
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues con = new ContentValues();
    con.put(UserProfile.Users.DOB, dob);
    con.put(UserProfile.Users.PASSWORD, password);
    con.put(UserProfile.Users.GENDER, gender);

    String where = UserProfile.Users.USERNAME + " = ?";
    String[] wheree = {username};

    long r = db.update(UserProfile.Users.TABLE_NAME, con, where, wheree);

    if(r == 1)
        return false;
    else
        return true;
}

public Cursor readAllInfor(){
    SQLiteDatabase db = this.getReadableDatabase();

    String[] col = {UserProfile.Users.USERNAME, UserProfile.Users.DOB, UserProfile.Users.PASSWORD, UserProfile.Users.GENDER};

    Cursor cursor = db.query(UserProfile.Users.TABLE_NAME,col,null,null, null, null, null);

    return cursor;
}


public boolean addInfo(String username, String dob, String password, String gender){
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues con = new ContentValues();
    con.put(UserProfile.Users.USERNAME, username);
    con.put(UserProfile.Users.DOB, dob);
    con.put(UserProfile.Users.PASSWORD, password);
    con.put(UserProfile.Users.GENDER, gender);

    long r = db.insert(UserProfile.Users.TABLE_NAME, null, con);

    if(r == 1)
        return false;
    else
        return true;
}

答案 1 :(得分:0)

公共无效editButton(视图){

    DBHelper db = new DBHelper(this);

    un = (EditText) findViewById(R.id.editText9);
    dob = (EditText) findViewById(R.id.editText10);
    pw = (EditText) findViewById(R.id.editText11);

    int res = gender.getCheckedRadioButtonId();
    String ress = String.valueOf(res);

    boolean x = db.updateInfor(un.getText().toString(), dob.getText().toString(), pw.getText().toString(), ress);

    if(x == false)
        Toast.makeText(this, "Unsuccessfull", Toast.LENGTH_SHORT);
    else
        Toast.makeText(this, "Successfull", Toast.LENGTH_SHORT);
}

public void delete(View view){
    DBHelper db = new DBHelper(this);

    un = (EditText) findViewById(R.id.editText9);

    db.deleteInfo(un.getText().toString());
}

public void ReadAll(View view){
    DBHelper db = new DBHelper(this);

    Cursor cursor = db.readAllInfor();

    List <String> l1 = new  ArrayList<>() ;

    while(cursor.moveToNext()){
        l1.add(cursor.getString(0));
        l1.add(cursor.getString(1));
        l1.add(cursor.getString(2));
        l1.add(cursor.getString(3));
    }

    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    CharSequence[] ch = l1.toArray(new CharSequence[l1.size()]);

    alert.setItems(ch, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });

    alert.show();
}

公共无效ReadOne(查看视图){         DBHelper db =新的DBHelper(this);

    un = (EditText) findViewById(R.id.editText9);
    dob = (EditText) findViewById(R.id.editText10);
    pw = (EditText) findViewById(R.id.editText11);
    Cursor cursor = db.readAllInfor(un.getText().toString());

    while(cursor.moveToNext()){
        dob.setText(cursor.getString(1));
        pw.setText(cursor.getString(2));
    }
}