0

I have a pre-made SQLite database that I am downloading from the net via an AsyncTask. It downloads the file and stores it on the sdcard in /data/databases/ I have checked the database file and it is successfully downloading and has all the appropriate tables and data but every time I try and open the database and display the stored data I get the following

    03-19 18:43:10.204: E/AndroidRuntime(3057): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ondrovic.downloader/com.ondrovic.downloader.Main}: android.database.sqlite.SQLiteException: no such table: beers: , while compiling: SELECT * FROM beers ORDER BY _id

which makes no sense because the table is there

maybe my databasehelper class is wrong or I am calling it wrong.

here is my database.java

   package com.ondrovic.downloader;

   import java.io.File;

   import android.content.Context;
   import android.database.sqlite.SQLiteDatabase;
   import android.database.sqlite.SQLiteOpenHelper;
   import android.os.Environment;

   public class Database extends SQLiteOpenHelper{
//File rDIR = Environment.getExternalStorageDirectory();
private static String DBPATH = "/data/databases/BOOMBOZZ/";
private static String DBNAME = "boombozz.db";
private static int DBVER = 1;

private SQLiteDatabase db;
private final Context dbContext;

public Database(Context context) {
    super(context, DBNAME, null, DBVER);
    this.dbContext = context;
}

public void open() {
    String myPath = DBPATH + DBNAME;
    db = SQLiteDatabase.openDatabase(Environment.getExternalStorageDirectory() + myPath, null, SQLiteDatabase.OPEN_READWRITE);
}

public synchronized void close() {

    db.close();
    super.close();
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

   }
}

And here is where I am calling it in my main class

 db = (new Database(this)).getWritableDatabase();

Any suggestions?

Thanks

ondrovic
  • 1,105
  • 2
  • 23
  • 40

1 Answers1

1

maybe my databasehelper class is wrong

Yes.

You need to implement onCreate() and onUpgrade() on a SQLiteOpenHelper subclass. You, for whatever reason, decided not to do that. As a result, your SQLiteOpenHelper will not work.

However, since SQLiteOpenHelper is not designed to support your download-the-database scenario, you should just make Database not extend SQLiteOpenHelper, and open and close the database yourself, as you are already partially doing.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So I did some checking and it is downloading the file I want but then I am also get a database in /data//datbases which is where it is trying to find the table that doesn't exist. I will have another go at it – ondrovic Mar 19 '12 at 23:28
  • would copying the download database file from the sdcard to the /data/data//db.db work with SQLiteOpenHelper? – ondrovic Mar 20 '12 at 01:33
  • @ondrovic: Yes, insofar as `SQLiteOpenHelper` will then see the database file there, not need to invoke your `onCreate()`, and should be able to open and close it normally. I assumed there were reasons why you were downloading to external storage. – CommonsWare Mar 20 '12 at 11:01
  • I am still new to java and android programming and I originally had the database being created on the 1st run of the app but there is a UI freeze and lag I was trying to provide a progress dialog that would show the status when it was writing the database tables but couldn't figure it out so I decided to try this method instead. here is the link to my original post [link]http://stackoverflow.com/questions/9640298/progress-dialog-when-creating-database-for-the-first-time – ondrovic Mar 20 '12 at 14:49
  • I ended up just opening the database from the location on the sd card and it seems to be working a lot faster. – ondrovic Mar 22 '12 at 13:14