没有找到Android现有的sqlite数据库

时间:2014-04-30 10:51:35

标签: android sql database sqlite

使用AVD管理器从Android Studio运行我的代码(我的应用程序)运行良好。首先我遇到了数据库找不到的问题,但后来我使用Monitor(DDMS)并将文件放在正确的位置:/data/data/thePackagename/databases/database.sqlite

我的应用程序运行正常,如果代码只是在某处找到数据库,所有Sqlite查询都可以正常工作!

现在我在真实手机(SGS4)上找到实际数据库时出现问题我安装了我的应用。 如果我错了,请纠正我但是:我想我必须将我的数据库从我的SGS4上的某个存储卡复制/移动到这个位置: /data/data/thePackagename/databases/database.sqlite ......对吗? 现在我该怎么做?也许我应该把数据库放在内部或外部存储卡,然后只是复制它,但我不知道如何。我已经关注了一些stackoverflow帖子,但我真的不知道我在做什么:) 在真实UE上运行时,我真的可以从assets文件夹中获取数据库吗?这种复制技巧甚至不适用于AVD管理器UE。我想我需要一些专业帮助:))

我使用了一些代码: How to use an existing database with an Android application

我试过这个,但它不起作用:

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

请帮我解决这个问题:)

1 个答案:

答案 0 :(得分:0)

是的,您可以将数据库从assets文件夹复制到应用程序数据库文件夹。但是,您需要了解该过程。将使用扩展SqliteOpenHelper类。当您调用getReadableDatabasegetWritableDatabase时,它会检查您的应用程序数据库文件夹中是否有数据库。如果没有,则会从您扩展onCreate课程的databaseHelper课程中调用SqliteOpenHelper。在那里,您可以使用copyDatabase的电话。