user1741398
user1741398

Reputation: 55

SQLite "no such table"

I've made an Adnroid program in Eclipse that reads a csv file and inputs the info into an SQLite database, and using

dbCursor = database.query(DATABASE_TABLE, new String[]{KEY_ROWID, KEY_ITEMTYPE, KEY_QUANTITY},  null, null, null, null, null);  

    int iRow = dbCursor.getColumnIndex(KEY_ROWID);
    int iType = dbCursor.getColumnIndex(KEY_ITEMTYPE);
    int iQuantity = dbCursor.getColumnIndex(KEY_QUANTITY);

        for(dbCursor.moveToFirst(); !dbCursor.isAfterLast(); dbCursor.moveToNext()){
            allData = allData + " " + dbCursor.getString(iRow) + "\t " + dbCursor.getString(iType) + "\t\t\t " + dbCursor.getString(iQuantity) + "\n";      
        }

and then printing out allData in a textview box has worked just fine.

Then I wanted to use

dbCursor = database.rawQuery("SELECT KEY_ROWID , SUM(KEY_ITEMTYPE) FROM DATABASE_TABLE GROUP BY KEY_ROWID", null);          
for(dbCursor.moveToFirst(); !dbCursor.isAfterLast(); dbCursor.moveToNext()){

    allDataSum = allDataSum + " " + dbCursor.getString(0);
} //I know this will probably not print what I want correctly but that's a problem for another day

When I try to print this I get the error "10-25 22:06:35.016: I/Database(1911): sqlite returned: error code = 1, msg = no such table: DATABASE_TABLE".

I then opened DDMS File Explorer and copied the database onto my desktop and then tried viewing the database with a command tool but again got an error, "SQL error: no such table: inventory".

How can the emulator show me what I want to see from .query() (but not from .rawQuery) when apparently there is no table in the database? Everything about I use the query and rawQuery is identical, so it's not a case of not opening the db for rawQuery or something like that.

Upvotes: 3

Views: 1367

Answers (2)

Khantahr
Khantahr

Reputation: 8528

It appears that you're using a static variables named DATABASE_TABLE, KEY_ROWID, and KEY_ITEMTYPE containing your actual names. When you use this with rawQuery, you'll have to do

dbCursor = database.rawQuery("SELECT " + KEY_ROWID + ", SUM(" + KEY_ITEMTYPE + ") FROM " + DATABASE_TABLE + " GROUP BY " + KEY_ROWID, null);

Notice that when you make the query using database.query you don't have these variables in quotes, that's why it works.

Upvotes: 1

PatchyFog
PatchyFog

Reputation: 1601

Not sure if the SUM() aggregate is causing you trouble, but you can double check the table's existence using the sqlite command line and the ".tables" command:

sqlite3 DATABASE
SQLite version 3.7.8 2011-09-19 14:49:19
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sql>.tables

Upvotes: 0

Related Questions