函数删除数据库?

时间:2011-05-05 18:42:49

标签: android database sqlite

有人可以帮我编写从我的数据库中删除的功能吗?这是我的代码:

public class ShopsHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME="shops.db";
    private static final int SCHEMA_VERSION=1;
    public SQLiteDatabase db;
    public ShopsHelper(Context context) {
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE restaurants (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, address TEXT, type TEXT, notes TEXT);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // no-op, since will not be called until 2nd schema
    // version exists
}

public Cursor getAll() {
    return(getReadableDatabase().rawQuery("SELECT _id, name, address, type, notes FROM restaurants ORDER BY name", null));
}

public void insert(String name, String address,String type, String notes) {
    ContentValues cv=new ContentValues();

    cv.put("name", name);
    cv.put("address", address);
    cv.put("type", type);
    cv.put("notes", notes);

    getWritableDatabase().insert("restaurants", "name", cv);
}

public String getName(Cursor c) {
    return(c.getString(1));
}

public String getAddress(Cursor c) {
    return(c.getString(2));
}

public String getType(Cursor c) {
    return(c.getString(3));
}

public String getNotes(Cursor c) {
    return(c.getString(4));
}
}

2 个答案:

答案 0 :(得分:2)

嗨,这对你有帮助..

 int result = db.delete("table_name",column_name=id,null);

 Toast.makeText(this,"Result"+result,2).show();

 // Sucess returns 1 else 0.

答案 1 :(得分:0)

上下文可以是您的活动或服务。

public void deleteDatabase(Context context, String databaseName) {
    ContextWrapper wrapper = new ContextWrapper(context);
    wrapper.deleteDatabase(databaseName);
}