猿问

在Android中删除SQLite中的行

这可能是一个愚蠢的问题,但是我是SQLite的新手,我似乎无法弄清楚。我有了列1台KEY_ROWID,KEY_NAME,KAY_LATITUDE,和KEY_LONGITUDE。我希望用户能够选择一个并删除它;谁能给我一个开始的方向?我的问题是仅给定名称的行的实际删除。


相关代码:


public class BeaconDatabase {


    public static final String KEY_ROWID = "_id";

    public static final String KEY_NAME = "beacon_name";

    public static final String KEY_LATITUDE = "beacon_lat";

    public static final String KEY_LONGITUDE = "beacon_lon";


    private static final String DATABASE_NAME ="BeaconDatabase";

    private static final String DATABASE_TABLE ="beaconTable";

    private static final int DATABASE_VERSION = 1;


    private DbHelper helper;

    private final Context context;

    private SQLiteDatabase db;


    public BeaconDatabase(Context context) {

        this.context = context;

    }


    public BeaconDatabase open() {

        helper = new DbHelper(this.context);

        db = helper.getWritableDatabase();

        return this;

    }


    public void close() {

        helper.close();

    }


    public long createEntry(String name, Double lat, Double lon) {

        ContentValues cv = new ContentValues();

        cv.put(KEY_NAME, name);

        cv.put(KEY_LATITUDE, lat);

        cv.put(KEY_LONGITUDE, lon);

        return db.insert(DATABASE_TABLE, null, cv);

    }


    public void deleteEntry(long row) {


              // Deletes a row given its rowId, but I want to be able to pass

              // in the name of the KEY_NAME and have it delete that row.

              //db.delete(DATABASE_TABLE, KEY_ROWID + "=" + row, null);

    }


    public String getData() {

        String[] columns = { KEY_ROWID, KEY_NAME, KEY_LATITUDE, KEY_LONGITUDE };

        Cursor cursor = db.query(DATABASE_TABLE, columns, null, null, null, null, null);

        String result = "";


        int iRow = cursor.getColumnIndex(KEY_ROWID);

        int iName = cursor.getColumnIndex(KEY_NAME);

        int iLat = cursor.getColumnIndex(KEY_LATITUDE);

        int iLon = cursor.getColumnIndex(KEY_LONGITUDE);


MYYA
浏览 803回答 4
4回答

ABOUTYOU

您可以这样尝试: //---deletes a particular title---public boolean deleteTitle(String name) {    return db.delete(DATABASE_TABLE, KEY_NAME + "=" + name, null) > 0;}要么public boolean deleteTitle(String name) {    return db.delete(DATABASE_TABLE, KEY_NAME + "=?", new String[]{name}) > 0;}

米脂

那样尝试,可能会找到解决方案String table = "beaconTable";String whereClause = "_id=?";String[] whereArgs = new String[] { String.valueOf(row) };db.delete(table, whereClause, whereArgs);

智慧大石

最好也使用whereargs;db.delete("tablename","id=? and name=?",new String[]{"1","jack"});这就像使用以下命令:delete from tablename where id='1' and name ='jack'并且以这种方式使用delete函数是很好的,因为它可以删除sql注入。
随时随地看视频慕课网APP

相关分类

Android
我要回答