首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查询不按预期工作

问查询不按预期工作
EN

Stack Overflow用户
提问于 2013-12-14 05:58:34
回答 2查看 66关注 0票数 0

我有一个查询,我正试图在SQLite表上执行该查询。表中有记录,它们映射到我传递的选择参数。以下是我的查询和全局字符串:

代码语言:javascript
复制
 // Table Notifications
    public static final String  notificationfor = "NotificationFor";
    public static final String  datetonotify = "DateToNotify";
    public static final String  isextraordinary = "IsExtraordinary";
    public static final String  notificationdata = "NotificationData";
    public static final String  notificationdatefor= "NotificationDateFor";
    public static final String  primaryid= "PrimaryId"; 


Cursor c = mDb.rawQuery("select DateToNotify from " + DATABASE_TABLE1 + 
            " where " + primaryid + " = ? AND  " + notificationfor + " = ?", new String[] { D, selection });

如果我将1和1作为参数传递(记录映射到我传递的参数),则在游标中给出一个null。

但是,当我执行以下查询时,它工作得很好:

代码语言:javascript
复制
String Tets = "Select DateToNotify from Notifications WHERE PrimaryId = 1 AND NotificationFor =1";
        Cursor c = mDb.rawQuery(Tets, null);

编辑

我刚试过以下几种方法,效果很好:

代码语言:javascript
复制
String Tets = "Select DateToNotify from Notifications WHERE PrimaryId =" +D+ " AND NotificationFor ="+selection;
        Cursor c = mDb.rawQuery(Tets, null); 

整个函数如下所示:

代码语言:javascript
复制
public Cursor alertQueryYYYY(String D, String selection){

    //  Cursor c = mDb.rawQuery("select DateToNotify from " + DATABASE_TABLE1 + 
    //      " where " + primaryid + " = ? AND  " + notificationfor + " = ?", new String[] { D, selection });

        String Tets = "Select DateToNotify from Notifications WHERE PrimaryId =" +D+ " AND NotificationFor ="+selection;
        Cursor c = mDb.rawQuery(Tets, null); 


        String K = "select DateToNotify from " + DATABASE_TABLE1 + 
                        " where " + primaryid + " = ? AND  " + notificationfor + " = ?"; // new String[] { D, selection })

        System.out.println("Here I am " + K); 
        return c;

    }

这是我的表的图像,因为我没有一个好的图像编辑软件,所以我把它上传到另一个网站明确的原因。

编辑下面是DB创建字符串,正如CL所指出的,错误在于不匹配类型。

代码语言:javascript
复制
private static final String DATABASE_CREATE1 ="create table Notifications(_id integer primary key autoincrement, "
            +"NotificationFor Integer," // Weeks/Seconds etc /7 variables
            +"DateToNotify text,"       // Date of Notification
            +"isExtraordinary Integer," // Special notifications, yet unknown
            +"NotificationData Integer," // Weeks/Seconds/ etc / 7 variables
            +"NotificationDateFor text,"
            +"PrimaryId);";
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-14 08:30:16

数字1与字符串"1"不一样,因此如果搜索错误类型的ID,则不会找到记录。

查询参数对于避免格式化问题和SQL注入攻击非常有用。这些问题只能发生在字符串中,而不是普通整数中,因此要在查询中使用整数值,不应该使用参数,而应该直接插入值:

代码语言:javascript
复制
"... WHERE PrimaryId =" +D+ " AND NotificationFor ="+selection;
票数 1
EN

Stack Overflow用户

发布于 2013-12-14 08:53:37

我更喜欢这种方式来使用查询:

代码语言:javascript
复制
final SQLiteDatabase database = getReadableDatabase();
 final String[] projection = new String[] {datetonotify };
 final String selection = primaryid + "=? " +notificationfor +"=?";
 final String[] having = new String[] {D,selection};
 Cursor cursor = database.query(DATABASE_TABLE1, projection, selection, having,
                null, null, null, null);

您的代码必须工作,如果有错误,它不是在参数中,而是在变量DATABASE_TABLE1中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20580197

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档