在Android中将sqlite表从资产复制到数据/数据

在Android中将sqlite表从资产复制到数据/数据,android,Android,我正在制作一个提供sqlite数据库更新的函数。我在assets文件夹中有一个数据库(比如demo.db),当我第一次进入Android应用程序时,它会复制到我的data文件夹中。但是,当我的应用程序有了新版本时,我需要替换数据/数据文件夹中已创建的资产文件夹中的sqlite表。以下是我的代码: MySQLiteHelpter扩展了SQLiteOpenHelper: @Override public void onUpgrade(SQLiteDatabase db, int oldVer

我正在制作一个提供sqlite数据库更新的函数。我在assets文件夹中有一个数据库(比如demo.db),当我第一次进入Android应用程序时,它会复制到我的data文件夹中。但是,当我的应用程序有了新版本时,我需要替换数据/数据文件夹中已创建的资产文件夹中的sqlite表。以下是我的代码:

MySQLiteHelpter扩展了SQLiteOpenHelper:

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        for(int i = oldVersion; i <=  newVersion; i++){
            switch (i){
                case 8:
                    //delete the older table 
                    db.execSQL("drop table if exists " + DFY_BD_DISTRICT);

                    //do something.
                    //I need to copy the table from the assets folder even I have already created the database in the previous version.

                    break;
            }
        }
    }
当我在互联网上搜索时,我发现了一些与我类似的问题: 我很高兴,如果你能给我一些帮助:)
如果您需要更多代码或更多问题,我将尽快粘贴我的代码。

您可以使用以下功能将本地资产数据库复制到android设备

String DB_PATH = "/data/data/com.example.app/databases/"


public String initialiseDB(String dbName,String copyas,Context context)
    {

        try
        {               
            File f = new File(DB_PATH);
            if (!f.exists())
            {
                f.mkdir();
            }

            // Open your local db as the input stream
            InputStream myInput = context.getAssets().open(dbName);

            // File for creating the database
            String outFileName = DB_PATH + copyas;

            File tmpFile = new File(outFileName);


            // check if the database already exist

            if (!tmpFile.exists()){
            //Log.d(LOG_TAG, outFileName + "database not available, initialising seed");
            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();
}


        } catch (Exception e)
        {
            e.printStackTrace();
            Log.e(LOG_TAG, e.toString());
            return "failure";
        }

        return "SUCCESS";

    }

我希望这会对您有所帮助…如果您有任何困难,请告诉我。

谢谢您的代码~但您的代码似乎是在我第一次进入应用程序时复制了数据库,而不是将数据库中的表从资产复制到数据/数据文件夹~在您的情况下,@LinaInverce----您可以在更新中删除旧数据库数据,并通过编程从资产中复制新数据库。我的问题与@LinaInverce的问题相同。我不想完全传输文件,只想将一个表传输到我的数据库文件中。我相信这是可能的,但仍然找不到答案。我试图在onCreate函数中编写代码来实现这一点。
String DB_PATH = "/data/data/com.example.app/databases/"


public String initialiseDB(String dbName,String copyas,Context context)
    {

        try
        {               
            File f = new File(DB_PATH);
            if (!f.exists())
            {
                f.mkdir();
            }

            // Open your local db as the input stream
            InputStream myInput = context.getAssets().open(dbName);

            // File for creating the database
            String outFileName = DB_PATH + copyas;

            File tmpFile = new File(outFileName);


            // check if the database already exist

            if (!tmpFile.exists()){
            //Log.d(LOG_TAG, outFileName + "database not available, initialising seed");
            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();
}


        } catch (Exception e)
        {
            e.printStackTrace();
            Log.e(LOG_TAG, e.toString());
            return "failure";
        }

        return "SUCCESS";

    }