简体   繁体   English

如何从android studio的sqlite数据库中删除用户?

[英]How to delete a user from sqlite database in android studio?

Im trying to figure out how I can delete one user at a time by typing in the edittext the users username and clicking deleting which I then want all the users information (username, usernum, password, birthdate, phone, address) to be deleted from the database.我试图弄清楚如何通过在编辑文本中输入用户的用户名并单击删除来一次删除一个用户,然后我希望从中删除所有用户信息(用户名、用户名、密码、生日、电话、地址)数据库。 Below is my code and for some reason it isnt working can any one please please help me!!下面是我的代码,由于某种原因它不起作用,请任何人帮助我!! Im very desperate and ive been trying to figure out the problem for hours.我非常绝望,我几个小时都在试图找出问题所在。

DatabaseHelperUser class: DatabaseHelperUser 类:

public class DatabaseHelperUser extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "User.db";
public static final String TABLE_NAME = "User_table";
public static final String COL1 = "ID";
public static final String COL2 = "UserNum";
public static final String COL3 = "UserName";
public static final String COL4 = "Password";
public static final String COL5 = "BirthDate";
public static final String COL6 = "Phone";
public static final String COL7 = "Address";


public DatabaseHelperUser(Context context) {
    super(context, DATABASE_NAME, null, 1);
}


@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, UserNum TEXT,UserName Text,Password Text,BirthDate Text,Phone Text,Address Text)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

public Cursor getData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
    return res;
}

public boolean deleteData(String UserName) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(TABLE_NAME, "UserName" + "=?" + UserName, null) > 0;

}


}

RemoveUser class: RemoveUser 类:

public class RemoveUser extends AppCompatActivity {

Button btdelete;
EditText txtUser;

DatabaseHelperUser myDb;

private String selectedName;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_remove_user);

    btdelete = (Button) findViewById(R.id.butRemove);
    txtUser = (EditText) findViewById(R.id.etxtUserName);


    myDb = new DatabaseHelperUser(this);

    btdelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            boolean delete = myDb.deleteData(txtUser.getText().toString());

            if(delete == true)
            Toast.makeText(RemoveUser.this,"User has been deleted", Toast.LENGTH_LONG).show();

            else
                Toast.makeText(RemoveUser.this,"User has not been deleted", Toast.LENGTH_LONG).show();

        }
    });


}
}

You must pass the variable UserName as the 3d argument of the method delete() , so it will replace the placeholder ?您必须将变量UserName作为方法delete()的 3d 参数传递,以便它将替换占位符? when the statement is executed:执行语句时:

public boolean deleteData(String UserName) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(TABLE_NAME, "UserName = ?", new String[] {UserName}) > 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM