0

I've used the GUI to create a DB which has 1650 records in it.

I'm trying to query this DB but it's always returning nothing. I've tried writing a simple getrowcount() method to see if I'm getting anything at all, but it always returns zero. I must be missing something obvious here, if someone can help point out what's going on.

In my main app.java:

    db = new DbHandler(this);
    String sIcao1 = "ROW COUNT = " + String.valueOf(db.getRowCount());

In my dbhandler.java:

package com.jammo.mywidget4;

<snip - standard includes>

public class DbHandler extends SQLiteOpenHelper {

    private static SQLiteDatabase db;
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "airports";
    private static final String TABLE_AIRPORTS = "airports";

    public DbHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

        db = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db = this.getWritableDatabase();
    }

    int getRowCount() {

        int nCount = -1;
        //SQLiteDatabase db = this.getReadableDatabase();
        Cursor cur = db.rawQuery("SELECT * FROM airports", null);
        nCount = cur.getCount();
        if (cur != null) {
            //cur.moveToFirst();                      
            //nCount = cur.getInt(0);
            //if (cur.getInt (0) == 0) {              

            //}
        }

        return nCount;
    }

}

In the GUI (SQLite DB Browser) I'm doing a simple

select * from airports

... and I'm getting back the full number of rows. When I debug the Java, cursor returns nothing.

Also, the DB created by the GUI is located in myapp/assets/airports.db.

Any ideas?

andr
  • 15,970
  • 10
  • 45
  • 59
Jammo
  • 1,838
  • 4
  • 25
  • 39
  • I recommend using the [SQLiteAssetHelper](https://github.com/jgilfelt/android-sqlite-asset-helper) library. It has APIs to move your database from `/assets` to `/databases`, so you don't have to "re-invent the wheel". – Sam Mar 04 '13 at 21:04

1 Answers1

0

I think you need to include the .db in the DATABASE_NAME.

Try changing this:

private static final String DATABASE_NAME = "airports";

to this:

private static final String DATABASE_NAME = "airports.db";

Edit:

Actually I think even with this change it is not going to work for you. SQLiteOpenHelper is expecting your db file to be inside of /data/data/your.package/databases/ So I think you'll need to copy it from assets to there if you want it to work with an unmodified SQLiteOpenHelper.

See here to learn how to copy it over: Android: Accessing assets folder sqlite database file with .sqlite extension

Community
  • 1
  • 1
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • I see, so just having the DB in /assets/ is not adequate. I have to copy the DB (oncreate) each time to the new /data/data/ (ie each time the app runs) ? Will give this a shot and let you know - thanks. – Jammo Mar 04 '13 at 19:14
  • "so just having the DB in /assets/ is not adequate" - correct. "each time the app runs" - No, under normal circumstances you'd only need to copy it once, the first time the app runs. After that you'd never need to copy it again unless you make changes to the db file in assets, that you want to be reflected within the application. – FoamyGuy Mar 04 '13 at 19:16
  • Alrighty, I've tried both methods (answers) described on the link you posted. Both are causing the app to crash, however the second method is saying "no such table airports"; db=/data/data/com.mypackage.myapp/databases/airports.db --- I could post my new code as an "answer" here for you? – Jammo Mar 04 '13 at 20:20
  • @Jammo post a (new) separate question with all of the relavant new code, and log cat errors. – FoamyGuy Mar 04 '13 at 20:37
  • Cheers Foamy http://stackoverflow.com/questions/15211004/android-sqlite-rowcount-is-always-zero-part-2 – Jammo Mar 04 '13 at 20:58