简体   繁体   中英

Android existing sqlite database not found

To run my code(my app) from Android Studio with AVD manager worked well. First I had a problem with the database not found there but then I used Monitor(DDMS) and just put the file in the right spot: /data/data/thePackagename/databases/database.sqlite

My app works just fine and all Sqlite queries works just fine if the code just finds the database somewhere!

Now I'm having problems with finding the actual database on a real phone(SGS4) where I have installed my app. Correct me if I'm wrong but: I guess I have to copy/move my database from some memory card on my SGS4 to this location: /data/data/thePackagename/databases/database.sqlite ...right? Now how do I do that? Maybe I should just put the database in the internal or the external memory card then just copy it but I dont know how. I have followed some stackoverflow posts but I really dont know what I'm doing:) Can I really get the database from the assets folder when running on a real UE? This copy trick did not even work with the AVD manager UE. I guess I need some professional help:)

I have used some of the code from here: How to use an existing database with an Android application

I have tried this but it does not work:

        /**
         * Creates a empty database on the system and rewrites it with your own database.
         * By calling this method and empty database will be created into the default system path
         * of your application so we are gonna be able to overwrite that database with our database.
         */
        public void createDataBase() throws IOException {
                //check if the database exists
                boolean databaseExist = checkDataBase();

                if (databaseExist) {
                // Do Nothing.
                } else {
                Log.d("Haze", "Create DB...");
                SQLiteDb = SQLiteDatabase.openOrCreateDatabase(DB_NAME_AND_PATH, null);
                Log.d("Haze", "Just created DB...");
                copyDataBase();
                Log.d("Haze", "Just copied DB...");
                }// end if else dbExist
                } // end createDataBase().

        /**
         * Check if the database already exist to avoid re-copying the file each time you open the application.
         *
         * @return true if it exists, false if it doesn't
         */
        public boolean checkDataBase() {
                File databaseFile = new File(DB_PATH + DB_NAME);
                return databaseFile.exists();
                }

        /**
         * Copies your database from your local assets-folder to the just created empty database in the
         * system folder, from where it can be accessed and handled.
         * This is done by transferring byte stream.
         */
        private void copyDataBase() throws IOException {
                //Open your local db as the input stream
                InputStream myInput = context.getAssets().open(DB_NAME);
                // 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 input file to the output file
                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();
                }

Please help me with this problem:)

Yes, you can copy your database from the assets folder to your applications database folder. But, you need to understand the process. Extending the SqliteOpenHelper class would be used. When you call either the getReadableDatabase or the getWritableDatabase it checks to see if there is a database in your application database folder. If there is not, then it calls onCreate from your databaseHelper class where you extended the SqliteOpenHelper class. In there you would use your call to copyDatabase .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM