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

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

我想弄清楚如何通过在编辑文本中输入用户用户名并单击删除来一次删除一个用户,然后我需要所有用户信息(用户名、用户编号、密码、生日,phone, address) 从数据库中删除。以下是我的代码,由于某种原因它无法正常工作任何人都可以帮助我!!我非常绝望,我已经尝试找出问题几个小时了。

DatabaseHelperUserclass:

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;

}


}

删除用户 class:

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();

        }
    });


}
}

你必须传递变量UserName作为方法delete()的3d参数,所以它会在语句执行时替换占位符?

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