将表从外部数据库传输到内部表Sqlite Android

时间:2018-07-22 08:19:56

标签: android sqlite android-sqlite sqliteopenhelper

我需要将外部数据库中的表复制到我的Android应用程序中的内部数据库中。

下面是我要上的课。

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class DataBaseHelper extends SQLiteOpenHelper {

    //The Android's default system path of your application database.
    private static String DB_PATH = "";

    private static String DB_NAME = "db_main";

    private SQLiteDatabase myDataBase;

    private final Context myContext;

    /**
     * Constructor
     * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
     *
     * @param context
     */
    public DataBaseHelper(Context context) {

        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }


    public void createDataBase() throws IOException {

        this.getReadableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }


    private void copyDataBase() throws IOException {

        DB_NAME = "db_main.db";

        DB_PATH = "/data/data/" + myContext.getPackageName() + "/databases/";

        File DOWNLOADS_DIRECTORY = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

        File downloadedDbFile = new File(DOWNLOADS_DIRECTORY, "Database_export.db");


        //Open your local db as the input stream
        InputStream myInput = new FileInputStream(downloadedDbFile);

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

        //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();

    }

    public void openDataBase() throws SQLException {

        //Open the database
        String myPath = "/data/data/" + myContext.getPackageName() + "/databases/" + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }

    @Override
    public synchronized void close() {

        if (myDataBase != null)
            myDataBase.close();

        super.close();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    // Add your public helper methods to access and get content from the database.
    // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
    // to you to create adapters for your views.

}

然后从我的活动处理程序中调用类似

DataBaseHelper myDbHelper = new DataBaseHelper(ListCreatedSurvey.this);
myDbHelper = new DataBaseHelper(this);

try {
     myDbHelper.createDataBase();

} catch (IOException ioe) {
     throw new Error("Unable to create database");
}

执行此代码时,它在logcat中未显示任何错误。在try catch块中,所有代码行都执行而没有移动到catch块。我所有的信息日志都已打印。

2 个答案:

答案 0 :(得分:0)

您从未调用过createDatabase()方法

答案 1 :(得分:0)

  1. copyDataBase()类中公开DataBaseHelper
  2. 在您的活动中创建dbHelper的实例:

    DataBaseHelper myDbHelper =新的DataBaseHelper(this);

  3. 致电:myDbHelper.copyDataBase()

您知道这是一个还原过程,如果成功,它将删除内部存储器中的文件,并将其替换为新文件。
编辑:实际上,最好在活动或其他实用程序类中加入copyDataBase(),因为它不执行任何数据库操作,而仅执行文件替换。