Android的SQLite的删除查询不工作 [英] android sqlite delete query not working

查看:82
本文介绍了Android的SQLite的删除查询不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下SQLite的查询不会删除其ID的开头零。

The following sqlite query does not delete IDs which starts with zero.

Android的SQLite表结构

Android Sqlite Table structure

 String CREATE_TABLE_BUS = "CREATE TABLE " + TABLE_BUS + "("
                    + KEY_TID + " INTEGER PRIMARY KEY AUTOINCREMENT,"    
                    + KEY_BUS_NUM + " TEXT,"
                    + KEY_BUS_NAME + " TEXT,"
                    + KEY_FROM + " TEXT,"
                    + KEY_TO + " TEXT,"
                    + KEY_TYPE + " TEXT"
                    + ")";

            db.execSQL(CREATE_TABLE_BUS);

我一直BUS_NUM为文本,而不是为int的某种目的。

I kept BUS_NUM as text and not as int for some purpose.

这是功能我呼吁删除行。

And this is the function I call to delete a row..

        public void Delete_Bus(String bus_num) {
            SQLiteDatabase db = this.getWritableDatabase();

            db.delete(TABLE_BUS, KEY_BUS_NUM+"="+bus_num , null);
              Log.e("deleting bus num", bus_num);

            db.close(); // Closing database connection
        }

这code工作很细,当code不从零开始。

This code works very fine when the code does not start with zero..

它的工作原理为76555,而不是为09877.请告诉我错了我的code

It works for 76555 and not for 09877. Whats wrong with my code

推荐答案

本code生成的查询结尾是这样的:

The query generated by this code ends like this:

DELETE FROM BusTable WHERE BusNum = 012345

该数据库将跨preT这是一个数字,而不是字符串。

The database will interpret this as a number, not a string.

在SQL中,字符串必须用引号引起来:

In SQL, strings must be quoted:

DELETE FROM BusTable WHERE BusNum = '012345'

不过,你可以避免通过参数来格式化字符串:

However, you can avoid having to format the string by using a parameter:

db.delete(TABLE_BUS, KEY_BUS_NUM + " = ?", new String[] { bus_num });

这篇关于Android的SQLite的删除查询不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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