3

I know that in order to access the data folder on the device, it needs to be rooted. However, if I just want to copy the database from my assets folder to the data folder on my device, will the copying process works on an unrooted phone? The following is my Database Helper class. From logcat, I can verify that the methods call to copyDataBase(), createDataBase() and openDataBase() are returned successfully. However, I got this error message

android.database.sqlite.SQLiteException: no such table: TABLE_NAME:

when my application is executing rawQuery. I'm suspecting the database file is not copied successfully (cannot be too sure as I do not have access to data folder), yet the method call to copyDatabase() are not throwing any exception. What could it be? Thanks.

ps: My device is still unrooted, I hope it is not the main cause of the error.

public DatabaseHelper(Context context) {

        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }   

 public void createDataBase() throws IOException{

        boolean dbExist = checkDataBase();

        String s = new Boolean(dbExist).toString();
        Log.d("dbExist", s );

        if(dbExist){
            //do nothing - database already exist
            Log.d("createdatabase","DB exists so do nothing");
        }else{


        this.getReadableDatabase();

            try {

                copyDataBase();
                Log.d("copydatabase","Successful return frm method call!");

            } catch (IOException e) {

                throw new Error("Error copying database");

            }
        }

    }


    private boolean checkDataBase(){

        File dbFile = new File(DB_PATH + DB_NAME);
        return dbFile.exists();
            }


    private void copyDataBase() throws IOException{

        //Open your local db as the input stream
        InputStream myInput = null;


            myInput = myContext.getAssets().open(DB_NAME);


            Log.d("copydatabase","InputStream successful!");


        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;


        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        //transfer bytes from the inputfile to the outputfile
        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{

        //Open the database
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }

/*    @Override
    public synchronized void close() {

            if(myDataBase != null)
                myDataBase.close();

            super.close();

    }*/

    public void close() {
        // NOTE: openHelper must now be a member of CallDataHelper;
        // you currently have it as a local in your constructor
        if (myDataBase != null) {
            myDataBase.close();
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }



}
yurain
  • 31
  • 5
  • Try this complete/working tutorial, with source code: http://stackoverflow.com/questions/9109438/how-to-use-existing-database-with-android-app/9109728#9109728 – Yaqub Ahmad Oct 09 '12 at 04:25

1 Answers1

0

I assume that your DB_NAME is not the same to the Database which exists in your assests folder.

If you are following reigndesign, then check this line:

private static String DB_NAME ="myDBName";  

DB_NAME here is the name of your database. It is assumed that you have a copy of the database in the assets folder. Your DB_NAME must be the same which exists in the assets folder.

OR

Follow this.

Community
  • 1
  • 1
Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149
  • I have went through the link and all the methods either in copying/checking database are similar to what I'm having now. In emulator, it works. My application is not forced close when retrieving from the database. However, I noticed this(sqlite returned: error code = 1, msg = no such column: table_name) in the logcat. Do you think it is suggesting some error despite the apps still able to run smoothly on emulator? I have checked the sqlite file in DDMS and it has all the tables I need. – yurain Oct 09 '12 at 04:58
  • According to my understanding, the database is created without any table (EMPTY database) OR you are referencing wrong DataBase Name in your code. – Yaqub Ahmad Oct 09 '12 at 05:06
  • Please confirm that your database exists, follow this: http://stackoverflow.com/questions/9846336/android-how-can-i-view-a-sql-database-created-in-my-app-im-running-it-on-the – Yaqub Ahmad Oct 09 '12 at 05:08
  • Ok. I will try to run the sample project. Just to reconfirm, this has nothing to do with an unrooted phone and the app is still able to acess(retrieve) the data in sqlite file? – yurain Oct 09 '12 at 07:30
  • Your sample project works on my real device! Thanks for the example.Now I will try to adapt it into my own project. – yurain Oct 09 '12 at 07:51
  • I'm able to access my database file on real device at last. I'm suspecting in my original code, although copying and opening database are successful, however SQLiteDatabase.getReadableDatabase() returns null which causes rawQuery failed. – yurain Oct 10 '12 at 13:45