尝试使用where执行两个条件时没有此类列错误

时间:2013-12-18 13:10:34

标签: database count android-sqlite multiple-columns

我是android开发的新手。在我的项目中,我试图使用以下查询得到行数。我得到了(1)没有这样的列:column_name error.This是我的logcat,

no such column: userid (code 1): , while compiling: select count(*) from Messages where userid = userr AND  statusy = status
 12-18 12:48:13.776: E/AndroidRuntime(1372):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
 12-18 12:48:13.776: E/AndroidRuntime(1372):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
 12-18 12:48:13.776: E/AndroidRuntime(1372):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
 12-18 12:48:13.776: E/AndroidRuntime(1372):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
 12-18 12:48:13.776: E/AndroidRuntime(1372):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
 12-18 12:48:13.776: E/AndroidRuntime(1372):    at  android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
 12-18 12:48:13.776: E/AndroidRuntime(1372):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)

这是我的表创建语句

 String CREATE_MESSAGES_TABLE = "CREATE TABLE " + TABLE_MESSAGES + "(" + KEY_USERID + " TEXT," + KEY_ID + " INTEGER PRIMARY KEY," + KEY_FROM + " TEXT," + KEY_TO + " TEXT,"
            + KEY_BODY + " TEXT," + KEY_DATE + " TEXT," + KEY_STATUS + " TEXT" + ")";
            db.execSQL(CREATE_MESSAGES_TABLE);

这是我的疑问,

   public int Get_Total_Contacts() {


        String countQuery="select count(*) from " + TABLE_MESSAGES + " where " + KEY_USERID + " = userr AND  " + KEY_STATUS + " = status";

        SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);

            int a=cursor.getCount();
            cursor.close();

    return a;

    }

任何人都可以在我错误的地方帮助我吗?

感谢!!!

1 个答案:

答案 0 :(得分:0)

试试这个:

String countQuery="select count(*) from " + TABLE_MESSAGES + " where " + KEY_USERID + " = 'userr' AND  " + KEY_STATUS + " = 'status'";

使用rawquery时请注意每个空格和引号。

例外情况表明,没有名为&#39; userid&#39;在表格&#39;消息&#39;。我希望你之前做过插入,这是成功的。插入后,从设备中提取db文件(可以使用DDMS)并浏览表格以检查它是否正常。

大多数情况下,我们会得到“没有这样的专栏”。由于拼写错误或错误的结构化表格错误。在你的情况下,可能是第二个。