试图导入我的SQLite数据库到Android的 [英] Trying to import my sqlite database into Android

查看:131
本文介绍了试图导入我的SQLite数据库到Android的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继教程这里我一直在试图复制这一点,我不断收到对申报的OutputStream线路错误。

Following the tutorial here I've been trying to copy this and I keep getting errors on the Outputstream declaration line.

我的关注code是在这里:

My following code is here:

private void copyDataBase() throws IOException{
                                                                            System.out.println("copyDatabase Start");
    //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);
                                                                            System.out.println("copyDatabase: InputStream Set");
    // Path to the just created empty db
    String outFileName = "/data/data/com.example.sqllitedb_user/databases/";
                                                                            System.out.println("copyDatabase: outFileName:" + outFileName);
    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);
                                                                            System.out.println("copyDatabase: FileOutputStream set to above name");
    //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);
    }
                                                                            System.out.println("copyDatabase: byte buffer thing ran" );
    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
    System.out.println("copy Database complete");
}

我看到一些其他人有同样的问题,我想知道解决的办法是什么。

I see some other people have had the same problems and I was wondering what the solution is.

有另一种(容易)的方式导入和访问我自己的SQLite表?

Is there another (easier) way to import and access my own sqlite table?

忽略这个帖子 - 原来我忘了打电话初始化方法在我的主要活动

IGNORE THIS POST - Turns out I forgot to call the initialisation method on my main activity.

推荐答案

您可以尝试这样的事情:

You can try something like this:

/**
 * Copies your database from your local assets-folder to the just created
 * empty database in the system folder, from where it can be accessed and
 * handled. This is done by transfering bytestream.
 **/
private void copyDataBase() throws IOException {
    // Open your local db as the input stream
    final InputStream myInput = new FileInputStream(context.getCacheDir().getPath() + "/" + DB_NAME);
        //context.getAssets().open(DB_NAME);

    // Path to the just created empty db
    final String outFileName = dbPath + DB_NAME;

    // Open the empty db as the output stream
    final 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();
}

这篇关于试图导入我的SQLite数据库到Android的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆