Column 'objectId' does not exist Exception (only in emulator)

I have a really strange issue with my application and I don't know why it's happening (only in emulator). I'm using a custom database helper class and I don't have any problems with it. But I have an issue with a specific column in a database table which I'm using really often. So depending on a some extras which I send from a parent activity I'm running different sql statements. Here is what I'm using :

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);
        }
    }

When I try to start this activity it's throwing me an exception (ONLY IN EMULATOR,on real device - HTC Desire, HTC EVO 3D there is no problem)

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

on line : int cardId1 = myCursor.getInt(myCursor.getColumnIndexOrThrow("objectId"));

But the strange things is that when I download the database file from the emulator and open it, I can see clearly that I have column objectId there and I'm using that column in other files too, but it's not throwing me an exception. So I'm wondering is it some kind of emulator bug, because I don't get anything like this on device or maybe I'm doing something wrong.

Any ideas?

Answers


The problem is the alias for the column in the select statement. Only in case 3 does your cursor actually contain a column called objectId so when you do myCursor.getColumnIndexOrThrow("objectId") it throws an exception.

In case 1 the column in the cursor is "c" and in case 2 it is "cards".


Need Your Help

DataTable attached to SQL Database with function in query not automatically updating

c# sql sql-server data-binding datagridview

I am trying to bind my Datagridview to a SQL Database. When I update the database from another source (from the SQL Query in Visual Stuido) the changes aren't shown in the DataGridView or the under...

How to manually query from the database using symfony2 and doctrine

php symfony2 doctrine2 doctrine

I'm trying to query the database in my user repository like this (symfony2):

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.