gpt4 book ai didi

android - SQLiteOpenHelper:未在物理设备上调用 onCreate() 方法

转载 作者:太空狗 更新时间:2023-10-29 15:38:33 25 4
gpt4 key购买 nike

我在运行 4.4 版本的 android 模拟器上做了很多测试。在我的应用程序中,我使用 SQLiteOpenHelper 创建了一个包含一个表的 sqlite 数据库:

package com.findwords.modeles;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import com.findwords.MainActivity;
import com.findwords.controleurs.MenuController;

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

/**
* Created by louk on 02/01/14.
*/
public class DictionaryDbHelper extends SQLiteOpenHelper{

// declare constants fields
private static final String DB_PATH = "/data/data/com.findwords/databases/";
private static final String DB_NAME = "dictionary_db";
private static final int DB_VERSION = 1;

// declared constant SQL Expression
private static final String DB_CREATE =
"CREATE TABLE dictionary ( " +
"_id integer PRIMARY KEY AUTOINCREMENT, " +
"word text NOT NULL, " +
"definition text NOT NULL, " +
"length integer NOT NULL " +
");";

private static final String DB_DESTROY =
"DROP TABLE IF EXISTS dictionnary";

/*
* constructor
*/
public DictionaryDbHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}

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

boolean dbExist = checkDataBase();

if(dbExist){
//do nothing - database already exist
}else{

//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();

try {

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

SQLiteDatabase checkDB = null;

try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}catch(SQLiteException e){

//database does't exist yet.

}

if(checkDB != null){

checkDB.close();

}

return checkDB != null ? true : 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{

//Open your local db as the input stream
InputStream myInput = MenuController.getInstance().getMainActivity().getAssets().open(DB_NAME);

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

}

/*
* (non-Javadoc)
* @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DB_CREATE);

try {
createDataBase();
} catch (IOException e) {
e.printStackTrace();
}
}

/*
* (non-Javadoc)
* @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DB_DESTROY);
onCreate(db);
}
}

此外,我还编写了一个带有打开方法的适配器:

/*
* open database connection
*/
public DictionaryDbAdapter open() throws SQLException {
mDbHelper = new DictionaryDbHelper(mContext);
mDb = mDbHelper.getWritableDatabase();
return this;
}

它在模拟器上运行良好,因此调用了 SQLiteOpenHelper 类的 onCreate() 方法并创建了数据库,但在我的手机 (Google Nexus 5) 上没有调用它。

我的手机没有 root,所以我无法访问文件夹/data/data/com.myapp/databases 。

但是我希望这个应用程序可以在任何手机上运行,​​所以我不想对我的手机进行 root。

提前感谢任何可以帮助我的人。

最佳答案

让我试着向您解释一些事情。

在连接到数据库的应用程序中,我们指定数据库的名称和版本。在这种情况下,可能会出现以下情况:

1) 没有数据库 。例如,这可能是在初始设置程序的情况下。在这种情况下,应用程序本身必须创建数据库和其中的所有表。此外,它已经在使用新创建的数据库。

2) 数据库存在,但版本已过时。可能是案例更新。例如,新版本的程序需要旧表或新表中的附加字段。在这种情况下,应用程序必须更新现有表并在必要时创建新表。

3) 有一个数据库和它的实际版本 。在这种情况下,应用程序成功连接到数据库并运行。

如您所知,“应用程序必须”等同于“开发人员必须”,即我们的任务。为了处理上述情况,我们需要创建一个继承SQLiteOpenHelper 的类。称它为 DBHelper。此类将为我们提供创建或更新数据库的方法,以防它们不存在或过时。

onCreate - 如果我们要连接的数据库不存在(这是你的情况),将调用的方法

关于android - SQLiteOpenHelper:未在物理设备上调用 onCreate() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20936272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com