0

I have followed several tutorials on importing an external SQLite DB on an Android App and it looks I do everything "good" but I always get the same error:

Caused by: android.database.sqlite.SQLiteException: no such table: games (code 1): , while compiling: SELECT * FROM games

I created the DB on "DB Browser for SQLite: Here

I saved the DB as "testDB.db" and I put it on "assets" folder of Android Studio Project.

Here the code:

public class DatabaseHelper extends SQLiteOpenHelper{

   private static String DB_NAME = "testDB.db";
   SQLiteDatabase mDb;
   private final Context myContext;

   //The Android's default system path
   private static String DB_PATH = "/data/data/com.example.jack.testimport/databases/";

   public DatabaseHelper(Context context) {
       super(context, DB_NAME, null, 1);
       this.myContext = context;
   }

   public void createDataBase() throws IOException{
       boolean dbExist = checkDataBase();
       if(dbExist){
           //do nothing - database already exist
       }else{
           mDb = this.getReadableDatabase();
           if (mDb.isOpen()){
               mDb.close();
           }

           try {
               copyDataBase();
           } catch (IOException e) {
               throw new Error("Error copying database");
           }
       }

   }


   public boolean checkDataBase(){
       File dbFile = myContext.getDatabasePath(DB_NAME);
       return dbFile.exists();
   }


   private void copyDataBase() throws IOException{
       InputStream myInput = myContext.getAssets().open(DB_NAME);

       String outFileName = DB_PATH + DB_NAME;

       OutputStream myOutput = new FileOutputStream(outFileName);

       byte[] buffer = new byte[1024];
       int length;
       while ((length = myInput.read(buffer))>0){
           myOutput.write(buffer, 0, length);
       }

       //Close the streams
       myOutput.flush();
       myOutput.close();
       myInput.close();
   }

   public void openDataBase() throws SQLException{
       String myPath = DB_PATH + DB_NAME;
       mDb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
   }

   @Override
   public synchronized void close() {
       if(mDb != null)
           mDb.close();
       super.close();
   }

   @Override
   public void onCreate(SQLiteDatabase db) {
   }

   @Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   }

   //Query all data:
   public Cursor fetchGames(){
       return mDb.query("games", null,null,null,null,null,null);
   }

   public static String getDbName(){
       return DB_NAME;
   }

}

And from my activity I call this method:

int counter=0;
DatabaseHelper myDbHelper = new DatabaseHelper(ImportDatabase.this);

try {
   myDbHelper.createDataBase();
} catch (IOException ioe) {
   throw new Error("Unable to create database");
}
try {
    myDbHelper.openDataBase();
    counter=myDbHelper.fetchGames().getCount();
} catch (SQLException sqle) {
    throw sqle;
}

Counter is always 0 and I get the error on top of this thread. I tried to clean/rebuild the project, delete all data and reinstalling the app, changing the path, I followed all the related question (so I hope you don't report it as duplicate), etc...I'm becoming crazy! What can I do to fix this error?

Thank you in advance.

Community
  • 1
  • 1
  • Try deleting the App's Data or uninstalling the App and then rerunning the App. – MikeT May 13 '18 at 16:33
  • Already done but does not change anything – michymallo91 May 13 '18 at 17:37
  • OK go to [this link here](https://stackoverflow.com/questions/46642269/are-there-any-methods-that-assist-with-resolving-common-sqlite-issues/46697342#46697342) copy the code into a new class called **CommonSQLiteUtilities** then add the line `CommonSQLiteUtilities.logDatabaseInfo(myDBHelper.getWritableDatabase());` after line `myDbHelper.openDataBase();` (i.e,. before fetchgames call) and retry. Look at the output in the log. You might have inadvertently not saved the DB before copying/ table name might be wrong. The output will list the tables. – MikeT May 13 '18 at 17:49
  • I suspect that you have an empty DB. If so remove these lines `mDb = this.getReadableDatabase(); if (mDb.isOpen()){ mDb.close(); }` from **createDataBase** method, delete the App's data and retry. – MikeT May 13 '18 at 18:01
  • I re-created the db file and the project and now it works. I used the same code so I don't understand why now it works.. Thank you for the answer! – michymallo91 May 13 '18 at 18:07
  • It's probably because you didn't save it I think DB Browser for SQLite creates an empty db file and only rewrites the file when you save. Helped others with similar issue. – MikeT May 13 '18 at 18:09

1 Answers1

0

I've tested your code working in my device. But,

  1. Can you test with my createDatabase method next few line of code?

    public void createDataBase() throws IOException{
        try
        {
            openDataBase();
        }catch(SQLException se)
        {
        }
    
        //if database does exists
        if(mDb != null)
        {
            //close it before open
            mDb.close();
        }
        else
        {
            //if the database does not exists..onCreate method will be called
            this.getReadableDatabase();
            //copy it from the assets directory.
            copyDataBase();
        }
    }
    
  2. Is 'com.example.jack.testimport' your package name in your AndroidManifest.xml?

You would get such an error message when your database is located in a different location from your package like 'com.example.jack' for example.

Your database location must be

'/data/data/[package name in your AndroidManifest.xml]/databases/'

I got a similar message with yours. enter image description here

I've got a message above when my database path is "com.tobee.myapp.db" and my package location is "com.tobee.myapp".

I hope this will help..

tommybee
  • 2,409
  • 1
  • 20
  • 23
  • @michymallo91, your code is okey, so i add another one. – tommybee May 13 '18 at 16:04
  • I added you first point. 2. This is the package on my Manifest.xml package="com.example.jack.testimport" But I get the same error. If I get the path from myContext.getDatabasePath(DB_NAME).getAbsolutePath(); I get data/user/0/com.example.jack.testimport/databases/testDB.db (If I use this path to open the file I get the same error...) What am I doing wrong? :( Thank you! – michymallo91 May 13 '18 at 17:36
  • However, the database is copied because the app size increases by a few MB after running the method to copy the database.. – michymallo91 May 13 '18 at 17:40
  • Can you upload your db file? – tommybee May 13 '18 at 17:40
  • I re-created the db file and the project and now it works. I used the same code so I don't understand why now it works.. Thank you for the answer! – michymallo91 May 13 '18 at 18:07
  • Good, Have a nice day! – tommybee May 14 '18 at 02:36