How to access and query the database that is copied to the assets folder?

前端 未结 1 909
慢半拍i
慢半拍i 2021-01-16 08:00

I have read few tutorials where in a pre-existing database is copied to assets folder and write code for copying this database on to the default system path of the applicati

相关标签:
1条回答
  • 2021-01-16 08:49

    You need to copy your database file to application directory folder then use this file to open database

    I use this to copyDatabase

    private void copyDataBase()
    {
        Log.i("Database", "New database is being copied to device!");
        byte[] buffer = new byte[1024];
        OutputStream myOutput = null;
        int length;
        // Open your local db as the input stream
        InputStream myInput = null;
        try
        {
            myInput = myContext.getAssets().open(DB_NAME);
            // transfer bytes from the inputfile to the
            // outputfile
            myOutput = new FileOutputStream(DB_PATH + DB_NAME);
            while((length = myInput.read(buffer)) > 0)
            {
                myOutput.write(buffer, 0, length);
            }
            myOutput.close();
            myOutput.flush();
            myInput.close();
            Log.i("Database", "New database has been copied to device!");
            cmn.mailDetails();
    
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
    

    and

    Open using this

    protected Boolean openDatabase()
    {
        if(isDatabaseExist(false))
        {
            // Open the database
            String myPath = DB_PATH + DB_NAME;
            try
            {
                Log.i("Database", "Trying to Open Database!");
                if(myDataBase != null)
                {
                    if(!myDataBase.isOpen())
                    {
                        Log.i("Database", "Database is closed now opening it!");
                        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    
                    }
                    else
                    {
                        Log.i("Database", "Database is already Open!");
                    }
                    Log.i("Database", "Database is Opened successfully in OPEN_READWRITE  Mode !");
                    return true;
    
                }
                else
                {
                    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
                    Log.i("Database", "Database is Opened successfully in OPEN_READWRITE  Mode !");
                    return true;
                }
    
            }
            catch(Exception e)
            {
                Log.e("Database", "Some error occured while opening Database Error:" + e.getMessage());
                myDataBase = null;
                return false;
            }
    
        }
        else
        {
            copyDataBase();
        }
        return false;
    }
    
    0 讨论(0)
提交回复
热议问题