数据库没有从资产文件夹复制到设备文件夹、资产、数据库、设备

2023-09-06 13:47:53 作者:人的身体,鱼的记忆

我在资产文件夹中的.db文件。我已经把它复制到数据/数据​​/<包名称> /数据库/ 文件夹中的仿真器和它的工作的罚款。

I have a .db file in my assets folder. I've copied it to the data/data/<packagename>/databases/ folder in the emulator and its working fine..

但是,当我在设备上运行它,它的力量关闭。它显示

But when i run it on device it force closes. It is showing

SQLite exception: no such table: tbl_user

下面是我的code ..

Here is my code..

public class DatabaseHelper extends SQLiteOpenHelper {

        public DatabaseHelper() {

            super(dataContext, DATABASE_NAME, null, 1);

            DB_PATH = "/data/data/"
                    + dataContext.getApplicationContext().getPackageName()
                    + "/databases/";

            Log.d("PATH", DB_PATH);

            boolean dbExist = checkDataBase();
            if (!dbExist) {

                this.getReadableDatabase();
                try {
                    copyDataBase();

                } catch (IOException e) {
                    Log.d("Error", e.toString());
                }
            }

        }

        private void copyDataBase() throws IOException {
            // TODO Auto-generated method stub

            InputStream inFile = dataContext.getAssets().open(DATABASE_NAME);
            String outFileName = DB_PATH + DATABASE_NAME;
            OutputStream myOutput = new FileOutputStream(outFileName);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inFile.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }
            // Close the streams
            myOutput.flush();
            myOutput.close();
            inFile.close();
        }

        private boolean checkDataBase() {
            // TODO Auto-generated method stub

            File dbFile = new File(DB_PATH + DATABASE_NAME);

            return dbFile.exists();

        }

我应该做些别的事情来复制数据库到设备???

Should i have to do something else to copy that db to the device???

谢谢..

推荐答案

得到的答复... :)

Got the answer... :)

从这里..

http://www.anddev.org/networking-database-problems-f29/missing-table-in-sqlite-with-specific-version-of-desire-hd-t50364.html

这是与2.3.6版本的问题......它正在与其他设备......只是增加三条线来解决这个问题...

it was the problem with version 2.3.6... it was working with other devices... just added three lines to solve the problem...

boolean dbExist = checkDataBase();
        SQLiteDatabase db_Read = null;
        if (!dbExist) 
        {
            db_Read = this.getReadableDatabase(); 
            db_Read.close();

            try 
            {
                copyDataBase();
            } 
            catch (IOException e) 
            {
                Log.d("Error", e.toString());
            }
        }