如何在Android Studio中从SQLite数据库中删除用户?

huangapple go评论84阅读模式
英文:

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

问题

我正在尝试弄清楚如何通过在编辑框中输入用户用户名然后点击删除按钮逐个删除用户然后我希望从数据库中删除所有用户信息用户名用户编号密码出生日期电话地址)。下面是我的代码但出现了一些问题不知何故它无法正常工作能否有人帮帮我我非常绝望我已经尝试了几个小时来找出问题所在

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=?", new String[]{UserName}) > 0;
    }
}

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) {
                    Toast.makeText(RemoveUser.this, "用户已删除", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(RemoveUser.this, "用户未删除", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}
英文:

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:

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:

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

答案1

得分: 0

你必须将变量UserName作为方法delete()的第三个参数传递,这样在执行语句时它将替换占位符?

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

You must pass the variable UserName as the 3d argument of the method delete(), so it will replace the placeholder ? when the statement is executed:

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

huangapple
  • 本文由 发表于 2020年10月8日 12:03:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64255599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定