尝试选择时,SQLite Exception没有此类列 [英] SQLite Exception no such column when trying to select

查看:52
本文介绍了尝试选择时,SQLite Exception没有此类列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据库帮助程序类:

I have the following DB helper class:

public int studentExists(String studid) {
    Cursor dataCount = mDb.rawQuery("select count(*) from usertable where " + KEY_STUDID + "=" + studid, null);
    dataCount.moveToFirst();
    int count = dataCount.getInt(0);
    dataCount.close();
    return count;
}

我在应用程序中使用它来查看以前是否输入了学生证.

I use this in my app to see if a student id has been previously entered.

当学生ID为整数(346742)时,此方法很好用,但是每当我尝试添加字母数字ID(PB3874)时,它都会强制关闭应用程序.

This works fine when the student id's are ints (346742) but whenever I try to add an alpha-numeric id (PB3874) it force closes the app.

错误:

06-13 18:22:20.554:ERROR/AndroidRuntime(8088):android.database.sqlite.SQLiteException:无此类列:pb3874 :,而在编译时:从用户表中选择count(*),其中studid = pb3874

我不认为这是数据类型问题(因为我使用的是文本类型):

I don't think its a data type issue (because I use the text type):

    private static final String DATABASE_CREATE =
    "create table usertable (_id integer primary key autoincrement, "
    + "studid text not null);";

但是我很困惑为什么错误会显示no such column: pb3874,因为我试图从被研究的列中简单地选择该值.以及为什么这对于任何int值都非常适用.任何人都有解决问题的建议吗?

But I'm confused why the error is saying no such column: pb3874 as I'm trying to simply select that value from the studid column. And also why this works perfectly for any int value. Anyone have any problem solving advice?

推荐答案

这里发生的是SQLite认为'pb3874'实际上是列名,而不是字符串/文本文字.

What's happening here is that SQLite thinks that 'pb3874' is actually a column name, rather than a string/text literal.

要指定它是文本文字,您需要确保将文本值包装在适当的单引号中

To specify that it's a text literal, you'll want to ensure your text value is wrapped in the appropriate single quotes:

为防止SQL注入攻击,每当您从用户那里获取输入时,请使用参数化查询:

To prevent SQL injection attacks, whenever you're taking input from the user, use a parameterized query:

("select count(*) from usertable where " + KEY_STUDID + "=?", studid);

不进行参数化(在接受用户输入时非常不鼓励):

Without parameterization (very much discouraged when taking user input):

("select count(*) from usertable where " + KEY_STUDID + "='" + studid + "'", null);  

您的数字值不产生此值的原因:SQLite为您转换了这些数字文字.

The reason your numeric values didn't produce this: SQLite converted those numeric literals for you.

这篇关于尝试选择时,SQLite Exception没有此类列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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