我有一个MyDBAdapter类,可以做选择,但我也需要做
DELETE FROM user Where email="given value by variable String"
我不知道如何在Android上使用MyDBAdapter进行删除,我对此非常新手,有人可以通过电子邮件添加该功能来删除用户吗?
这是我的班级:
public class MyDbAdapter {
private static final String TAG = "NotesDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "gpslocdb";
private static final String PERMISSION_TABLE_CREATE = "CREATE TABLE permission ( fk_email1 varchar, fk_email2 varchar, validated tinyint, hour1 time default '08:00:00', hour2 time default '20:00:00', date1 date, date2 date, weekend tinyint default '0', fk_type varchar, PRIMARY KEY (fk_email1,fk_email2))";
private static final String USER_TABLE_CREATE = "CREATE TABLE user ( email varchar, password varchar, fullName varchar, mobilePhone varchar, mobileOperatingSystem varchar, PRIMARY KEY (email))";
private static final int DATABASE_VERSION = 2;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS user");
db.execSQL("DROP TABLE IF EXISTS permission");
db.execSQL(PERMISSION_TABLE_CREATE);
db.execSQL(USER_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//db.execSQL("DROP TABLE IF EXISTS user");
//db.execSQL("DROP TABLE IF EXISTS permission");
//onCreate(db);
}
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
//onCreate(db);
}
public void clearDb(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS user");
db.execSQL("DROP TABLE IF EXISTS permission");
onCreate(db);
}
}
/**
* Constructor - takes the context to allow the database to be
* opened/created
*
* @param ctx the Context within which to work
*/
public MyDbAdapter(Context ctx) {
this.mCtx = ctx;
}
/**
* Open the database. If it cannot be opened, try to create a new
* instance of the database. If it cannot be created, throw an exception to
* signal the failure
*
* @return this (self reference, allowing this to be chained in an
* initialization call)
* @throws SQLException if the database could be neither opened or created
*/
public MyDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
//clearDB();
mDbHelper.close();
}
public long createUser(String email, String password, String fullName, String mobilePhone, String mobileOperatingSystem) {
ContentValues initialValues = new ContentValues();
initialValues.put("email", email);
initialValues.put("password", password);
initialValues.put("fullName", fullName);
initialValues.put("mobilePhone", mobilePhone);
initialValues.put("mobileOperatingSystem", mobileOperatingSystem);
return mDb.insert("user", null, initialValues);
}
public long createPermission(String email1, String email2, String validated, String hour1, String hour2,
String date1, String date2, String weekend, String fk_type) {
ContentValues initialValues = new ContentValues();
initialValues.put("fk_email1", email1);
initialValues.put("fk_email2", email2);
initialValues.put("validated", validated);
initialValues.put("hour1", hour1);
initialValues.put("hour2", hour2);
initialValues.put("date1", date1);
initialValues.put("date2", date2);
initialValues.put("weekend", weekend);
initialValues.put("fk_type", fk_type);
return mDb.insert("permission", null, initialValues);
}
public void clearDB() {
mDbHelper.clearDb(mDb);
}
public Cursor fetchAllUsers() {
return mDb.query("user", new String[]{"email", "password", "fullName", "mobilePhone", "mobileOperatingSystem"}, null, null, null, null, null);
}
public Cursor fetchAllPermissions() {
return mDb.query("permission", new String[]{"fk_email1", "fk_email2", "validated", "hour1", "hour2", "date1", "date2", "weekend", "fk_type"}, null, null, null, null, null);
}
public Cursor fetchUser(String email) throws SQLException {
Cursor mCursor = mDb.query(true, "user", new String[]{"email", "password", "fullName", "mobilePhone", "mobileOperatingSystem"}
, "email" + "=" + email, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public void deleteUser(String email) throws SQLException {
Delete From permission Where fk_email1 = 'pablo@upv.es' and fk_email2 = 'fanny@test.es'
}
public List<Friend> retrieveAllFriends() {
List<Friend> friends = new ArrayList<Friend>();
Cursor result = fetchAllUsers();
if (result.moveToFirst()) {
do {
friends.add(new Friend(result.getString(result.getColumnIndexOrThrow("email")), result.getString(result.getColumnIndexOrThrow("password")), result.getString(result.getColumnIndexOrThrow("fullName")), result.getString(result.getColumnIndexOrThrow("mobilePhone")), result.getString(result.getColumnIndexOrThrow("mobileOperatingSystem"))));
} while (result.moveToNext());
}
return friends;
}
public List<Permission> retrieveAllPermissions() {
List<Permission> permissions = new ArrayList<Permission>();
Cursor result = fetchAllPermissions();
if (result.moveToFirst()) {
do {
permissions.add(new Permission(result.getString(result.getColumnIndexOrThrow("fk_email1")),
result.getString(result.getColumnIndexOrThrow("fk_email2")),
Integer.parseInt(result.getString(result.getColumnIndexOrThrow("validated"))),
result.getString(result.getColumnIndexOrThrow("hour1")),
result.getString(result.getColumnIndexOrThrow("hour2")),
result.getString(result.getColumnIndexOrThrow("date1")),
result.getString(result.getColumnIndexOrThrow("date2")),
Integer.parseInt(result.getString(result.getColumnIndexOrThrow("weekend"))),
result.getString(result.getColumnIndexOrThrow("fk_type"))));
} while (result.moveToNext());
}
return permissions;
}
}
答案 0 :(得分:2)
mDb.delete(DATABASE_NAME, "email" + "=" + value_to_delete, null);
答案 1 :(得分:0)
添加以下内容(以此为例):
@Override
public int delete(Uri uri, String where, String[] whereArgs) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
int count;
String id;
switch (sUriMatcher.match(uri)) {
case MATCHES:
count = db.delete(DBMatches.TABLENAME, where, whereArgs);
break;
case MATCH_ID:
id = uri.getPathSegments().get(1);
count = db.delete(DBMatches.TABLENAME, DBMatches._ID + "=" + id
+ (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}