删除数据表数据库? [英] delete data form database?

查看:147
本文介绍了删除数据表数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能在数据库中删除数据时,我列表视图中单击一个项目?请给我一些code到这一点。
这是我的方法来选择列表视图项:

how can i delete data in database when i click an item in listview? please give me some code to that. this my method to choose item in listview:

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

这是我的数据库类:

this my database class:

公共类数据库处理器扩展SQLiteOpenHelper {

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

}

推荐答案

添加一种方法在乌拉圭回合的数据库类code

Add one method in ur database class code

如果ü要删除表中的所有原始然后用

if u want to Delete Table All Raw then use

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

其他ü要在表中删除特定的原

other u want to deleted particular Raw in table

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

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

list view item click event use

    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

                    }
                });

这篇关于删除数据表数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆