列'objectId'不存在异常(仅在模拟器中)

时间:2011-11-30 13:06:10

标签: android database

我的应用程序有一个非常奇怪的问题,我不知道它为什么会发生(仅在模拟器中)。我正在使用自定义数据库帮助程序类,我没有任何问题。但是我对数据库表中的特定列有一个问题,我经常使用它。因此,根据我从父活动发送的一些extras,我正在运行不同的sql语句。这是我正在使用的:

String sql = "";
    switch (sort) {
    case 1:
            if (extra != 0) {
                sql = "SELECT DISTINCT c.objectId FROM cards AS c "
                        + "INNER JOIN cardtags AS ct "
                        + "ON (c.objectId=ct.cardId) "
                        + "WHERE c.collectionId=" 
                        + collId 
                        + " AND c.repeatsCount>0"+ " AND ct.tagId="
                        + extra;

            } else if (extra == 0) {
                sql = "SELECT DISTINCT c.objectId FROM cards AS c "
                        + "INNER JOIN cardtags AS ct "
                        + "ON (c.objectId=ct.cardId) "
                        + "WHERE c.collectionId=" 
                        + collId 
                        + " AND c.repeatsCount>0";
            }
        break;
    case 2:
            if (extra != 0) {
                sql = "SELECT DISTINCT cd.objectId FROM cards AS cd "
                        + "INNER JOIN categories AS ct "
                        + "ON (cd.categoryId=ct.objectId) "
                        + "WHERE cd.collectionId=" 
                        + collId
                        + " AND ct.objectId=" 
                        + extra 
                        + " AND cd.repeatsCount>0";

            } else if (extra == 0) {
                sql = "SELECT DISTINCT cd.objectId FROM cards AS cd "
                        + "INNER JOIN categories AS ct "
                        + "ON (cd.categoryId=ct.objectId) "
                        + "WHERE cd.collectionId=" 
                        + collId 
                        + " AND cd.repeatsCount>0";
            }
        break;
    case 3:
            String AscDesc = "";
            if (ascDesc == 0) {
                AscDesc = "ASC";
                sql = "SELECT DISTINCT objectId FROM cards "
                        + "WHERE collectionId="
                        + collId 
                        + " AND repeatsCount>0"
                        + " ORDER BY dateCreated "
                        + AscDesc;

            } else if (ascDesc != 0) {
                AscDesc = "DESC";
                sql = "SELECT DISTINCT objectId FROM cards  "
                        + "WHERE collectionId="
                        + collId 
                        + " AND repeatsCount>0"
                        + " ORDER BY dateCreated "
                        + AscDesc;
            }
        break;
    }
    Cursor myCursor = userDbHelper.executeSQLQuery(sql);
    if (myCursor.getCount() == 0) {
        myCursor.close();
    } else if (myCursor.getCount() > 0) {
        myCardID = new ArrayList<Integer>();
        myCardID.clear();
        for (myCursor.move(0); myCursor.moveToNext(); myCursor.isAfterLast()) {
            int cardId1 = myCursor.getInt(myCursor.getColumnIndexOrThrow("objectId"));
            myCardID.add(cardId1);
        }
    }

当我尝试开始这个活动时,它给我一个例外(仅在EMULATOR,在真实设备上 - HTC Desire,HTC EVO 3D没有问题)

"11-30 14:51:18.899: E/AndroidRuntime(14818): Caused by: java.lang.IllegalArgumentException: column 'objectId' does not exist"

在线:int cardId1 = myCursor.getInt(myCursor.getColumnIndexOrThrow("objectId"));

但奇怪的是,当我从模拟器下载数据库文件并打开它时,我可以清楚地看到我在那里有objectId列,我也在其他文件中使用该列,但它是不要骗我一个例外。所以我想知道它是某种模拟器错误,因为我在设备上没有得到这样的东西,或者我做错了。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

问题是select语句中列的别名。仅在情况3中,您的光标实际上包含一个名为objectId的列,因此当您执行myCursor.getColumnIndexOrThrow("objectId")时,它会抛出异常。

在情况1中,光标中的列是“c”,而在情况2中,它是“卡”。