资产文件夹中的Android外部数据库

时间:2022-07-09 00:27:59

I have an android application that is supposed to read and expand a database that is already created on sqlite...it works fine on emulator by putting database in "data/data/(packagename)/database" folder on the file explorer of emulator. Now problem is occuring with the real device. Obviously it doesnt have the database to open.I tried to put database in assets folder but I am not getting to open it with the openhelper.

我有一个android应用程序,它应该读取和扩展一个已经在sqlite上创建的数据库……在模拟器上的“数据/数据/(packagename)/数据库”文件夹中放入数据库,可以在模拟器上运行良好。现在问题出现在真正的设备上。显然,它没有打开数据库。我试图将数据库放在assets文件夹中,但是我无法用openhelper打开它。

5 个解决方案

#1


6  

you should copy the .db file from your assets folder to an internal/external storage. You can use following codes,

应该将.db文件从assets文件夹中复制到内部/外部存储中。你可以使用以下代码,

private static String DB_PATH = "/data/data/your package/database/";  
private static String DB_NAME ="final.db";// Database name 

To create a database,

创建一个数据库,

public void createDataBase() throws IOException 
{ 
  //If database not exists copy it from the assets 

   boolean mDataBaseExist = checkDataBase(); 
   if(!mDataBaseExist) 
   { 
      try  
      { 
        //Copy the database from assests 
        copyDataBase(); 
        Log.e(TAG, "createDatabase database created"); 
      }  
      catch (IOException mIOException)  
      { 
         throw new Error("ErrorCopyingDataBase"); 
     } 
  } 
} 

Check that the database exists here: /data/data/your package/database/DB Name

检查数据库是否存在:/data/data/您的包/数据库/DB名称

private boolean checkDataBase() 
{ 
    File dbFile = new File(DB_PATH + DB_NAME); 
    return dbFile.exists(); 
} 

Copy the database from assets

从资产中复制数据库

  private void copyDataBase() throws IOException 
  { 
    InputStream mInput = getApplicationContext().getAssets().open(DB_NAME); 
    String outFileName = DB_PATH + DB_NAME; 
    OutputStream mOutput = new FileOutputStream(outFileName); 
    byte[] mBuffer = new byte[1024]; 
    int mLength; 
    while ((mLength = mInput.read(mBuffer))>0) 
    { 
        mOutput.write(mBuffer, 0, mLength); 
    } 
    mOutput.flush(); 
    mOutput.close(); 
    mInput.close(); 
}

i hope it should help you.

我希望它能对你有所帮助。

#2


3  

you cant access the database from asset folder directly you need to copy it first to the path data/data/(packagename)/database then using it :

您不能直接从资产文件夹访问数据库,您需要先将其复制到路径数据/数据/(packagename)/数据库,然后使用它:

 private String DB_PATH = "/data/data/" + "yourpackaename" + "/databases/" + "db.db";

in your onCreate()

在onCreate()

 is = getAssets().open("db.db");
 write(is);

Then the method to call:

然后调用方法:

public void write(InputStream is) {
    try {
        OutputStream out = new FileOutputStream(new File(DB_PATH));
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = is.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        is.close();
        out.flush();
        out.close();
        System.err.println(out + "\n");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

#3


0  

You cannot directly open files from assets folder. Instead, you need to copy the contents of your assets folder on an internal/external storage and later use the File path to open the file. In emulators, its easier for you to access the data folder of your apps. However, on a real non-rooted android device, its not possible due to security reasons.

不能直接从assets文件夹中打开文件。相反,您需要将资产文件夹的内容复制到内部/外部存储中,然后使用文件路径打开文件。在模拟器中,更容易访问应用程序的数据文件夹。然而,在一个真正的非根的android设备上,这是不可能的,因为安全原因。

#4


0  

You need to first copy the Database file from assests to application data location using java code.Can You Post some code to show How are you opening or handling the database?

您需要首先使用java代码将数据库文件从assests复制到应用程序数据位置。您可以发布一些代码来显示如何打开或处理数据库吗?

#5


0  

Do you have a pre-populated database and looking to integrate into your app? If yes, you can simply do with my library

您是否有一个预填充的数据库,并希望集成到您的应用程序中?如果可以,你可以用我的图书馆

On your app's first launch after installation

在你的应用安装后的第一次启动

SuperDatabase database=new SuperDatabase(getApplicationContext(),"foods.db", AssetDatabaseMode.COPY_TO_SYSTEM);

On subsequent launches

在随后的发布

SuperDatabase database=new SuperDatabase(getApplicationContext(),"foods.db", AssetDatabaseMode.READ_FROM_DEVICE);

Simply fire SQL queries

只是火SQL查询

database.sqlInject("INSERT INTO food VALUES('Banana','Vitamin A');");

Get results on Array in CSV, JSON, XML

在CSV、JSON、XML中获取数组的结果

ArrayList<String> rows=new ArrayList<String>();
rows=database.sqlEjectCSV("SELECT * FROM food;");
for (int i=0;i<rows.size();i++)
{
    //Do stuffs with each row
}

You need to include my library for this. Documentations here:
https://github.com/sangeethnandakumar/TestTube

你需要包括我的图书馆。文件:https://github.com/sangeethnandakumar/TestTube

#1


6  

you should copy the .db file from your assets folder to an internal/external storage. You can use following codes,

应该将.db文件从assets文件夹中复制到内部/外部存储中。你可以使用以下代码,

private static String DB_PATH = "/data/data/your package/database/";  
private static String DB_NAME ="final.db";// Database name 

To create a database,

创建一个数据库,

public void createDataBase() throws IOException 
{ 
  //If database not exists copy it from the assets 

   boolean mDataBaseExist = checkDataBase(); 
   if(!mDataBaseExist) 
   { 
      try  
      { 
        //Copy the database from assests 
        copyDataBase(); 
        Log.e(TAG, "createDatabase database created"); 
      }  
      catch (IOException mIOException)  
      { 
         throw new Error("ErrorCopyingDataBase"); 
     } 
  } 
} 

Check that the database exists here: /data/data/your package/database/DB Name

检查数据库是否存在:/data/data/您的包/数据库/DB名称

private boolean checkDataBase() 
{ 
    File dbFile = new File(DB_PATH + DB_NAME); 
    return dbFile.exists(); 
} 

Copy the database from assets

从资产中复制数据库

  private void copyDataBase() throws IOException 
  { 
    InputStream mInput = getApplicationContext().getAssets().open(DB_NAME); 
    String outFileName = DB_PATH + DB_NAME; 
    OutputStream mOutput = new FileOutputStream(outFileName); 
    byte[] mBuffer = new byte[1024]; 
    int mLength; 
    while ((mLength = mInput.read(mBuffer))>0) 
    { 
        mOutput.write(mBuffer, 0, mLength); 
    } 
    mOutput.flush(); 
    mOutput.close(); 
    mInput.close(); 
}

i hope it should help you.

我希望它能对你有所帮助。

#2


3  

you cant access the database from asset folder directly you need to copy it first to the path data/data/(packagename)/database then using it :

您不能直接从资产文件夹访问数据库,您需要先将其复制到路径数据/数据/(packagename)/数据库,然后使用它:

 private String DB_PATH = "/data/data/" + "yourpackaename" + "/databases/" + "db.db";

in your onCreate()

在onCreate()

 is = getAssets().open("db.db");
 write(is);

Then the method to call:

然后调用方法:

public void write(InputStream is) {
    try {
        OutputStream out = new FileOutputStream(new File(DB_PATH));
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = is.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        is.close();
        out.flush();
        out.close();
        System.err.println(out + "\n");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

#3


0  

You cannot directly open files from assets folder. Instead, you need to copy the contents of your assets folder on an internal/external storage and later use the File path to open the file. In emulators, its easier for you to access the data folder of your apps. However, on a real non-rooted android device, its not possible due to security reasons.

不能直接从assets文件夹中打开文件。相反,您需要将资产文件夹的内容复制到内部/外部存储中,然后使用文件路径打开文件。在模拟器中,更容易访问应用程序的数据文件夹。然而,在一个真正的非根的android设备上,这是不可能的,因为安全原因。

#4


0  

You need to first copy the Database file from assests to application data location using java code.Can You Post some code to show How are you opening or handling the database?

您需要首先使用java代码将数据库文件从assests复制到应用程序数据位置。您可以发布一些代码来显示如何打开或处理数据库吗?

#5


0  

Do you have a pre-populated database and looking to integrate into your app? If yes, you can simply do with my library

您是否有一个预填充的数据库,并希望集成到您的应用程序中?如果可以,你可以用我的图书馆

On your app's first launch after installation

在你的应用安装后的第一次启动

SuperDatabase database=new SuperDatabase(getApplicationContext(),"foods.db", AssetDatabaseMode.COPY_TO_SYSTEM);

On subsequent launches

在随后的发布

SuperDatabase database=new SuperDatabase(getApplicationContext(),"foods.db", AssetDatabaseMode.READ_FROM_DEVICE);

Simply fire SQL queries

只是火SQL查询

database.sqlInject("INSERT INTO food VALUES('Banana','Vitamin A');");

Get results on Array in CSV, JSON, XML

在CSV、JSON、XML中获取数组的结果

ArrayList<String> rows=new ArrayList<String>();
rows=database.sqlEjectCSV("SELECT * FROM food;");
for (int i=0;i<rows.size();i++)
{
    //Do stuffs with each row
}

You need to include my library for this. Documentations here:
https://github.com/sangeethnandakumar/TestTube

你需要包括我的图书馆。文件:https://github.com/sangeethnandakumar/TestTube