O_Casper_ne 2013-02-17 02:41 采纳率: 7.7%
浏览 4987
已采纳

如何删除数据库中的数据?

当我点击列表视图中的一个选项时,如何删除数据库中的数据?我使用下面的方法来选择列表视图的选项。

listview.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView adapterView, View convertView, int position, long id)
        {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AndroidSpinnerFromSQLiteActivity.this);
            alertDialog.setTitle("Confirm Delete...");
            alertDialog.setMessage("Are you sure you want delete this?");
            alertDialog.setIcon(R.drawable.delete);
            alertDialog.setPositiveButton("YES",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int which) {
                        }
                    });
            alertDialog.setNegativeButton("NO",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    });
            alertDialog.show();
            }
        });
    };

数据库类:

public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "spinnerExample";
private static final String TABLE_LABELS = "labels";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT" +")";
    db.execSQL(CREATE_CATEGORIES_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS);
    onCreate(db);
}
public void insertLabel(String label){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_NAME, label);
    db.insert(TABLE_LABELS, null, values);
    db.close(); 
}
public List<String> getAllLabels(){
    List<String> labels = new ArrayList<String>();
    String selectQuery = "SELECT  * FROM " + TABLE_LABELS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            labels.add(cursor.getString(1));
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return labels;
}
}
  • 写回答

3条回答 默认 最新

  • LiDaming_and 2013-02-17 03:42
    关注

    在你的数据库类的代码中添加一个方法。如果你想删除所有的原始表单,使用

    db.execSQL("DELETE from "+Table_Name);
    

    如果想删除表格中特定的行,使用

     db.delete(Table_Name, "Raw_Field_Name" + "='" + Raw_field_value+"'", null);
    

    列表视图项的点击事件使用

     Listview lv;
    //on create method 
         lv = (ListView) findViewById(R.id.list_contact_name);
    
                  lv.setOnItemClickListener(new OnItemClickListener() 
                    {
                        public void onItemClick(AdapterView<?> arg0, View v, int position,
                                long id) {
                          // call DELETE method here
                        }
                    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP