【问题标题】:How to read preexisting database into Android using ORMLite如何使用 ORMLite 将预先存在的数据库读入 Android
【发布时间】:2013-07-02 04:56:54
【问题描述】:

我想在 android 中通过 ORMLite 使用预先存在的数据库文件。我有已经创建的数据库的 database.db 文件。我想在我的应用中使用它。

我的课程扩展了 OrmLiteSqliteOpenHelper。

任何人都可以有一个想法吗?请帮忙

我使用将数据库文件复制到数据路径中
public static void copyDataBase(String path,Context c) throws IOException{

    //Open your local db as the input stream
    InputStream myInput = c.getAssets().open("Mydb.db");

    // Path to the just created empty db
    String outFileName = "/data/data/packageName/databases/databaseName";
    String outFileName2 = "/data/data/packageName/databases/";
    File file = new File(outFileName2);
    if(!file.exists())
        file.mkdirs();
    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    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(); 
}

但这对我没有帮助。

【问题讨论】:

  • 到目前为止你尝试了什么,你得到了什么错误?

标签: android database ormlite


【解决方案1】:

这是一种暗中的尝试,因为我不知道您的确切问题是什么,但是您不能在 OrmLiteSqliteOpenHelper 构造函数中指定数据库文件(和路径)吗?

OrmLiteSqliteOpenHelper(android.content.Context context, String databaseName, android.database.sqlite.SQLiteDatabase.CursorFactory factory, int databaseVersion)

在这个论坛上也有一些关于打开自定义数据库文件的问题。 This 问题不是 ORMLite 特定的,但它们可能会让您前进,因为它打开了一个自定义数据库文件。

希望这对您有所帮助。

【讨论】:

    【解决方案2】:

    我已经通过下面的实现解决了这个问题

    try {
    
                String destPath = "/data/data/" + context.getPackageName()
                        + "/databases";
                Log.v("LOG", destPath);
    
                File f = new File(destPath);
                if (!f.exists()) {
                    f.mkdirs();
                    File outputFile = new File("/data/data/"
                            + context.getPackageName() + "/databases",
                            "Name ofyour Database");
                    outputFile.createNewFile();
    
                    String DatabaseFile = "database file name from asset folder";
    
                    InputStream in = context.getAssets().open(DatabaseFile);
                    OutputStream out = new FileOutputStream(outputFile);
    
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = in.read(buffer)) > 0) {
                        out.write(buffer, 0, length);
                    }
                    in.close();
                    out.close();
                }
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                Log.v("TAG", "ioexeption");
                e.printStackTrace();
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多