Android SQLiteOpenHelper在第一次执行期间崩溃

Android SQLiteOpenHelper在第一次执行期间崩溃,android,sqlite,Android,Sqlite,当我第一次执行应用程序时,我的DbHelper会创建数据库。但是,当我稍后尝试打开数据库时(在同一个应用程序执行期间,在另一个活动中),会再次创建数据库,因为checkDatabase()返回false,当然,这会产生一个异常,因为表存在。当我第一次创建数据库时,我打开并关闭它,所以我想问题是文件没有刷新(这是第二次checkDatabase()返回false的唯一原因) 重要的是,这只发生在第一次执行期间。“不幸的是,已停止”消息后,数据库访问正常工作。有什么想法吗?您可以参考下面的代码,了解

当我第一次执行应用程序时,我的DbHelper会创建数据库。但是,当我稍后尝试打开数据库时(在同一个应用程序执行期间,在另一个活动中),会再次创建数据库,因为checkDatabase()返回false,当然,这会产生一个异常,因为表存在。当我第一次创建数据库时,我打开并关闭它,所以我想问题是文件没有刷新(这是第二次checkDatabase()返回false的唯一原因)


重要的是,这只发生在第一次执行期间。“不幸的是,已停止”消息后,数据库访问正常工作。有什么想法吗?

您可以参考下面的代码,了解何时创建新数据库,检查现有数据库

public class DataBaseHelper extends SQLiteOpenHelper {

private static String DB_PATH;
private static String DB_NAME = "podcastDB.db";
private SQLiteDatabase myDataBase;
private final Context myContext;
SharedPreferences sharedPreferences;

public DataBaseHelper(Context context) {

    super(context, DB_NAME, null, 1);
    this.myContext = context;
    DB_PATH = "/data/data/" + myContext.getPackageName() + "/databases/";

    sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(myContext);
}

/**
 * Creates a empty database on the system and rewrites it with your own
 * database.
 * */
public void createDataBase() throws IOException {

    // ---Check whether database is already created or not---
    boolean dbExist = checkDataBase();

    if (dbExist) {
    } else {
        this.getReadableDatabase();
        try {
            // ---If not created then copy the database---
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }

}

/**
 * Check if the database already exist to avoid re-copying the file each
 * time you open the application.
 * 
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase() {
    try {
        String myPath = DB_PATH + DB_NAME;
        File f = new File(myPath);
        if (f.exists())
            return true;
        else
            return false;
    } catch (SQLiteException e) {
        Log.e("Podcast", "There was an error", e);
        return false;
    }
}

/**
 * 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 {

    InputStream myInput = myContext.getAssets().open(DB_NAME);

    String outFileName = DB_PATH + DB_NAME;

    OutputStream 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();
}

public void openDataBase() throws SQLException {

    // --- Open the database---
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);

}

@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) {

}
}

喜欢下面的链接。。没有你的代码,这将成为一个猜测游戏!发布一些代码。例如,
checkDatabase()
。谢谢你!出于任何奇怪的原因,checkDB=SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN\u READONLY);在检查数据库时给了我一个异常,但您的文件版本是完美的。我原以为这个问题会有一个解决方案,它只依赖SQLite类,即不需要检查文件系统。