外部数据库访问,我的应用程序崩溃

时间:2018-06-19 06:54:50

标签: java android sqlite

是的,我已经搜索了好几天的解决方案。当我尝试访问外部数据库(通过" 数据库浏览器为Sqlite创建时,我仍然存在的问题是我的应用在开始时崩溃 >"命名" test.db")。 唯一的目的当然是使用一些数据预填充我的应用。

我已将数据库文件保存在android studio中的assets文件夹中。最后,我实现了一个辅助类来访问数据库。

这是辅助类:

public class DataBaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "test.db";
private static final int DATABASE_VERSION = 1;
private static String DATABASE_PATH = "";

private SQLiteDatabase myDataBase;
private final Context myContext;

public DataBaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.myContext = context;
    if (Build.VERSION.SDK_INT >= 17) {
        DATABASE_PATH = context.getApplicationInfo().dataDir + "/databases/";
    } else {
        DATABASE_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    }
}

@Override
public void onCreate(SQLiteDatabase db) {

}

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

}

public void createDatabase() throws IOException {
    createDB();
}

private void createDB() {
    boolean dbExists = checkDatabase();
    if (dbExists) {
        //do nothing
    } else {
        this.getReadableDatabase();
        try {
            copyDatabase();
        } catch (IOException ie) {
            throw new Error("Error copying database");
        }
    }
}

private boolean checkDatabase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DATABASE_PATH + DATABASE_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
        checkDB.setLocale(Locale.getDefault());
    } catch (SQLiteException e) {
        throw new Error("SQLite exception");
    }
    if (checkDB != null) {
        checkDB.close();
    }
    return checkDB != null;
}

private void copyDatabase() throws IOException {
    InputStream myInput = null;
    OutputStream myOutput = null;
    String outFileName = DATABASE_PATH + DATABASE_NAME;

    try{
        myInput = myContext.getAssets().open(DATABASE_NAME);
        myOutput = new FileOutputStream(outFileName);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }catch (IOException ie){
        throw new Error("Copydatabase run error ");
    }

}

@Override
public synchronized void close() {
    if(myDataBase != null){
        myDataBase.close();
    }
    super.close();
}
}

在Main活动中,我刚刚调用了helper类的createDatabase()方法。代码如下:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DataBaseHelper myDbHelper = new DataBaseHelper(this);
    try{
        myDbHelper.createDatabase();
    }catch (IOException ioe){
        throw new Error("Unable to CREATE DATABASE");
    }
}
}

logcat将问题表示为:

  

AndroidRuntime:FATAL EXCEPTION:main java.lang.Error:SQLite   DataBaseHelper.checkDatabase(DataBaseHelper.java:79)处的异常   DataBaseHelper.createDB(DataBaseHelper.java:59)at   DataBaseHelper.createDatabase(DataBaseHelper.java:55)at   MainActivity.onCreate(MainActivity.java:18)

指向我的checkDatabase()方法。但现在我无法解决问题。

我正在使用Android Studio 3.0.1,数据库浏览器用于Sqlite 3.10.1,最低API级别使用17。

请帮助!

1 个答案:

答案 0 :(得分:0)

试试这个

 public static final String DATABASE_NAME = "abc.db";
 public final static String DATABASE_PATH = "/data/data/"+ PACKAGE_NAME + 
 "/databases/";
 public static final int DATABASE_VERSION = 1;


  public void createDatabase()  {
    boolean dbExist1 = checkDataBase();
    if (!dbExist1) {
        this.getReadableDatabase();
        try {
            this.close();
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}

// Check database already exist or not
private boolean checkDataBase() {
    boolean checkDB = false;
    try {
        String myPath = DATABASE_PATH + DATABASE_NAME;
        File dbfile = new File(myPath);
        checkDB = dbfile.exists();
    } catch (SQLiteException e) {
    }
    return checkDB;
}

// Copies your database from your local assets-folder to the just created
// empty database in the system folder
public void copyDataBase() throws IOException {
    String outFileName = DATABASE_PATH + DATABASE_NAME;
    OutputStream myOutput = new FileOutputStream(outFileName);
    InputStream myInput = mContext.getAssets().open(DATABASE_NAME);

    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }
    myInput.close();
    myOutput.flush();
    myOutput.close();
}