簡體   English   中英

找不到Android現有的sqlite數據庫

[英]Android existing sqlite database not found

使用AVD Manager在Android Studio中運行我的代碼(我的應用)效果很好。 首先,我遇到了一個找不到數據庫的問題,但后來我使用了Monitor(DDMS)並將文件放在正確的位置:/data/data/thePackagename/databases/database.sqlite

如果代碼只是在某個地方找到數據庫,我的應用程序就可以正常運行,所有Sqlite查詢也可以正常運行!

現在,在安裝了我的應用程序的真實電話(SGS4)上查找實際數據庫時遇到了問題。 如果我錯了,請糾正我,但是:我想我必須將數據庫從SGS4上的某些存儲卡復制/移動到以下位置: /data/data/thePackagename/databases/database.sqlite ...對嗎? 現在我該怎么做? 也許我應該只將數據庫放在內部或外部存儲卡中,然后復制它,但是我不知道如何。 我已經關注了一些stackoverflow的帖子,但我真的不知道自己在做什么:)在真正的UE上運行時,我真的可以從資產文件夾中獲取數據庫嗎? 該復制技巧甚至無法在AVD管理器UE中使用。 我想我需要一些專業幫助:)

我從這里使用了一些代碼: 如何在Android應用程序中使用現有數據庫

我已經嘗試過了,但是沒有用:

        /**
         * 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();
                }

請幫助我解決這個問題:)

是的,您可以將數據庫從資產文件夾復制到應用程序數據庫文件夾。 但是,您需要了解該過程。 將使用擴展SqliteOpenHelper類。 調用getReadableDatabasegetWritableDatabase它將檢查以查看應用程序數據庫文件夾中是否有數據庫。 如果沒有,它將從您擴展SqliteOpenHelper類的databaseHelper類調用onCreate 在這里,您將使用對copyDatabase的調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM