【问题标题】:Android: Unable to delete data from the databaseAndroid:无法从数据库中删除数据
【发布时间】:2012-11-15 10:26:15
【问题描述】:

我想从列表视图中删除特定行,其中数据是从数据库中填充的。在setOnItemLongClickListener 上,我已经能够从列表视图中删除数据,但是当我重新启动我的活动时,我可以看到我已删除的行在那里,因为在onCreate() 我从数据库中获取数据。

我做了什么:

MainActivity.java

db = new AndroidOpenDbHelper(this);
    showing_history
            .setOnItemLongClickListener(new OnItemLongClickListener() {

                @Override
                public boolean onItemLongClick(AdapterView<?> arg0,
                        View arg1, final int pos, final long id) {
                    final AlertDialog.Builder b = new AlertDialog.Builder(
                            MainActivity.this);
                    b.setIcon(android.R.drawable.ic_dialog_alert);
                    b.setMessage("Delete this from history?");
                    b.setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {

                                    HistoryNamesList.remove(pos);
                                    ((BaseAdapter) mHistoryListAdapter)
                                            .notifyDataSetChanged();
                                    del(pos);
                                }
                            });
                    b.setNegativeButton("No",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                    dialog.cancel();
                                }
                            });

                    b.show();
                    return true;
                }
            });

我在 MainActivity.java 中的删除方法:

private void del(int j) {

    if (db.deleteTitle(j))
        Toast.makeText(this, "Delete successful.", Toast.LENGTH_LONG)
                .show();
    else
        Toast.makeText(this, "Delete failed.", Toast.LENGTH_LONG).show();
    db.close();

}

我在 AndroidOpenDbHelper.java 中的删除查询

public boolean deleteTitle(long rowId) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(TABLE_NAME_HISTORY, ID + "=" + rowId, null) > 0;
}

同样,我可以从列表视图中删除数据,但不能从数据库中删除。请帮我解决这个问题。

【问题讨论】:

  • 任何,比如,错误,堆栈跟踪,日志?
  • 你可能想看看 ContentProviders。发布您的 onCreate 方法。
  • 位置不是 id。还有,什么是 deleteTitle ?
  • 代替del(pos) 使用del(id);
  • 完全没有错误,只是吐司说删除失败。 del(id),无助于清理数据库数据。

标签: android sqlite android-listview android-sqlite onlongclicklistener


【解决方案1】:

首先你使用以下代码获取所有行 ID

private int[] mRowId;
    dba.open();
        Cursor cr=dba.fetchData();
        cr.moveToFirst();
int i=0;
while(!cr.isAfterLast())
        {
mRowId[i]==cr.getInt(cr.getColumnIndex("colname"));
i++;
cr.moveToNext();

}

在列表视图项目上单击

list.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
         db.deleteTitle(mRowId[pos]);
            return true;
        }

public void deleteTitle(int rowid) {
        try {
            database.execSQL("DELETE FROM  TableName" + " where columnname="
                    + rowid + ";");
        } catch (Exception e) {
        }
    }

【讨论】:

  • fetchData() 方法在哪里,能不能也分享一下。
  • public Cursor fetchData() { try { return database.query(TableName, null, null, null, null, null, null); } 捕捉(异常 e){ 返回空值; } }
  • @sai- 您能否正确格式化您的答案,因为缺少一些变量,这让我感到非常困惑。请你这样做,这样我就可以完成这件事。
  • 对不起,我不能,这只是我正在做的代码 sn-p。这对我来说将是一个问题。你不能在这里帮我吗?
【解决方案2】:

尝试使用 db.raw 查询而不是 db.delete...我希望这可以帮助您..

【讨论】:

  • 使用命令 propmpt adb shell 检查查询工作正常,但我无法从代码中删除。
  • 您是否尝试过使用此 SQLiteDatabase 数据库;而不是 SQLiteDatabase db = this.getWritableDatabase();
【解决方案3】:

试试这个代码

public void deleteTableRow(int rowid) {
        try {
            database.execSQL("DELETE FROM  TableName" + " where columnname="
                    + rowid + ";");
        } catch (Exception e) {
        }
    }

【讨论】:

  • 你用不同的方式说同样的话,无论如何都试过了,没有得到与 Delted Failed 相同的 Toast。
  • 拉取你的数据库文件并在Sq Lite浏览器中检查一次。表行是否删除,否则在sq lite浏览器中执行一次。
  • 是的,所有查询都运行良好,但我无法在长按按钮时删除。
  • ur r 发送 pos 我的东西 pos 不是你尊重的行 id,所以得到与 Delted 相同的 Toast 失败,首先你将所有行 id 保存在一个数组元素中,然后使用 pos 像这样 arr 传递这个数组元素[位置]。
  • 你能指导我完成吗,我已经尝试了所有方法,对 m 没有任何作用。
猜你喜欢
  • 2010-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-15
  • 1970-01-01
  • 2018-06-23
  • 2016-04-02
相关资源
最近更新 更多