0

I know this was asked and answered quite some time ago, but i'm still having problems with my code and I am very new to Android dev.

I would like to check that a table exists before moving forward with any processing.

I found : https://stackoverflow.com/a/7863401/3669516

But I still get an error:

E/SQLiteLog: (1) no such table: challenge

W/Challenge_DBAdapter: no such table: challenge (code 1): , while compiling: SELECT COUNT(*) FROM challenge

Error Code : 1 (SQLITE_ERROR)

Caused By : SQL(query) error or missing database. (no such table: challenge (code 1): , while compiling: SELECT COUNT(*) FROM challenge)

Here is my Try()Catch{} snippet

try{
            Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM challenge",null);
            Log.w(TAG, "check cursor");
            if(cursor != null)
                if(cursor.getCount() < 1) {
                    if(setupDB())
                        db_is_set = true;
                } else {
                    db_is_set = true;
                }
        } catch (Exception e){
            Log.w(TAG, e.getMessage());         
        }

My code crashes before hitting the Log for 'check cursor'.

My desired result is to get a boolean (of sorts) so i can continue processing. Currently, I have the code working by calling the desired function "setupDB()" in the catch block, but I would like to know why my program fails when it hits the query vs going into the if(cursor != null) block.

Community
  • 1
  • 1
  • that is unusual. why can the table be missing? – juergen d Aug 03 '16 at 02:03
  • Your code behaves different because it uses an entirely different query. – CL. Aug 03 '16 at 07:55
  • can you help me understand that a little better, CL? I wanted to get back "results" from the DB so I can confirm that I did or did not hit the DB table i'm looking for. – user3669516 Aug 04 '16 at 00:47
  • @juergend I'm not sure I understand what you're asking. my understanding of the error messages thrown is, the obv, the table doesn't exist because the onCreate code did not exe. The other is because of the first, the sql tried to run on a table that simply doesn't exist. – user3669516 Aug 04 '16 at 00:50
  • Personally I deploy my app with the DB already built as Asset – juergen d Aug 04 '16 at 01:04
  • Which i agree make perfect sense, but since i'm learning i'm exploring and I came across this question so i decided to ask. (Thank you for your contribution so far) – user3669516 Aug 04 '16 at 01:48

0 Answers0