我为Android设备创建了一个琐事测验。我有一个数据库,其中包含assets文件夹和dbHelper类中的问题。一切正常,但现在我想修复一些拼写错误并添加更多问题。我需要更新数据库,但我不知道该怎么做。我在互联网上发现了这个dbHelper类,我自己也没有足够的知识。我知道我应该比较新数据库和现有数据库的版本,如果它们不匹配,我需要删除旧数据库并安装新数据库。
onUpgrade方法为空
class dbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "questions.db";
private static final int SCHEMA_VERSION = 1;
public SQLiteDatabase dbSglite;
private String mDBPath;
private final Context myContext;
public dbHelper(Context context) {
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
this.myContext=context;
this.mDBPath=context.getDatabasePath(DATABASE_NAME).getParent();
}
@Override
public void onCreate(SQLiteDatabase db){
Log.d("ONCREATE","OnCreate Method Called.");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void createDatabase(){
createDB();
}
private void createDB(){
boolean dbExist = DBExists();
if(!dbExist){
//this.getReadableDatabase();
copyDBFromResource();
}
dbSglite=getReadableDatabase();
}
private boolean DBExists(){
SQLiteDatabase db = null;
try {
String databasePath = myContext.getDatabasePath(DATABASE_NAME).getPath();
db = SQLiteDatabase.openDatabase(databasePath,null, SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
} catch (SQLiteException e) {
Log.e("SqlHelper", "database not found");
}
if (db != null) {
db.close();
}
return db != null;
}
private void copyDBFromResource() {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = myContext.getAssets().open(DATABASE_NAME);
File databasedir = new File(myContext.getDatabasePath(DATABASE_NAME).getParent());
databasedir.mkdirs();
outputStream = new FileOutputStream(mDBPath+"/"+DATABASE_NAME);
byte[] buffer = new byte[1024];
int length;
while ((length=inputStream.read(buffer))>0){
outputStream.write(buffer, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
throw new Error("Problem copying database.");
}
}
public void openDataBase() throws SQLException {
String myPath = myContext.getDatabasePath(DATABASE_NAME).getPath();
dbSglite = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
}
答案 0 :(得分:0)
根据Gabe Sechan的评论,最简单的方法是每次启动应用程序时从资源文件夹复制数据库,即更改: -
private void createDB(){
boolean dbExist = DBExists();
if(!dbExist){
this.getReadableDatabase();
copyDBFromResource();
}
dbSglite=getReadableDatabase();
}
来: -
private void createDB(){
copyDBFromResource();
dbSglite=getReadableDatabase();
}
你显然有担心评论
“每次活动开始时都不会复制数据库。”
是的,它会(会那么糟糕吗? - 修辞)。
但是,假设您要使用数据库版本,即根据当前版本检查assets文件夹中的版本。您仍然基本上需要从assets文件夹访问数据库,因此您将检查一个数据库与另一个数据库(至少打开它们)。所以仍然会有一些开销。
可能不太密集的选项是检查资产文件的上次修改日期与共享首选项中上次复制的资产文件的日期。 (File
lastModified
方法)File - lastModified。
在类似的视图中,另一个选项是检查包版本与最后实施的包,再次存储在共享首选项中。PackageInfo - versionCode。
当然,在这两个选项中,只有在存在差异(增加)时才会从资产文件中替换数据库。
以下示例( dbHelper 类中的所有更改)将在包版本增加(或数据库不存在)的情况下从资产中复制数据库: -
class dbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "questions.db";
private static final int SCHEMA_VERSION = 1;
private static final String SHARED_PREFS = "shared_prefs";
private static final String SHARED_PREFKEY_QUESTIONSDBLASTUPDATED = "spkey_qdblastupdated";
public SQLiteDatabase dbSglite;
private String mDBPath;
private final Context myContext;
public dbHelper(Context context) {
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
this.myContext=context;
this.mDBPath=context.getDatabasePath(DATABASE_NAME).getParent();
}
@Override
public void onCreate(SQLiteDatabase db){
Log.d("ONCREATE","OnCreate Method Called.");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void createDatabase(){
createDB();
}
private void createDB(){
if (isQuestionsDBNew() || (!DBExists())) {
Log.d("COPYFROMASSET", "Copying Questions From Assets");
copyDBFromResource();
setQuestionsDBNew(getPackageVersion());
} else {
Log.d("COPYFROMASSET",
"Questions not copied from Assets - New Questions Check result was " +
Boolean.toString(isQuestionsDBNew()) +
" DB Exists result was " + Boolean.toString(DBExists())
);
}
dbSglite=getReadableDatabase();
}
private boolean DBExists(){
SQLiteDatabase db = null;
try {
String databasePath = myContext.getDatabasePath(DATABASE_NAME).getPath();
db = SQLiteDatabase.openDatabase(databasePath,null, SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
} catch (SQLiteException e) {
Log.e("SqlHelper", "database not found");
}
if (db != null) {
db.close();
}
return db != null;
}
private void copyDBFromResource() {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = myContext.getAssets().open(DATABASE_NAME);
File databasedir = new File(myContext.getDatabasePath(DATABASE_NAME).getParent());
databasedir.mkdirs();
outputStream = new FileOutputStream(mDBPath+"/"+DATABASE_NAME);
byte[] buffer = new byte[1024];
int length;
while ((length=inputStream.read(buffer))>0){
outputStream.write(buffer, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
throw new Error("Problem copying database.");
}
}
public void openDataBase() throws SQLException {
String myPath = myContext.getDatabasePath(DATABASE_NAME).getPath();
dbSglite = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
private boolean isQuestionsDBNew() {
SharedPreferences prefs = myContext.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
long stored_lastused = prefs.getLong(SHARED_PREFKEY_QUESTIONSDBLASTUPDATED,-1);
Log.d("NEWQUESTIONS?", "Result of testing package version " +
String.valueOf(stored_lastused) +
" against " +
String.valueOf( getPackageVersion()) +
" was " + String.valueOf(stored_lastused < getPackageVersion()));
return (stored_lastused < getPackageVersion());
}
private long getPackageVersion() {
PackageInfo pi;
try {
pi = myContext.getPackageManager().getPackageInfo(myContext.getPackageName(),0);
} catch (PackageManager.NameNotFoundException e) {
return -1;
}
Log.d("PACKAGEVERSION", "The version of package " +
myContext.getPackageName() +
" was " +
String.valueOf(pi.versionCode)
);
return pi.versionCode;
}
private void setQuestionsDBNew(long lastused) {
SharedPreferences.Editor editor = myContext.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE).edit();
editor.putLong(SHARED_PREFKEY_QUESTIONSDBLASTUPDATED,lastused);
editor.apply();
}
}
isQuestionsDBNew
如果包版本大于共享首选项中的版本存储,则返回true(共享首选项中的任何内容都不会产生-1,因此应该小于任何包版本。)getPackageVersion
将包版本作为long返回。setQuestionsDBNew
更新适用的共享偏好。01-05 19:46:44.849 26692-26692/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
01-05 19:46:44.850 26692-26692/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
01-05 19:46:44.850 26692-26692/m.com.so48103235_updateprepopdb D/NEWQUESTIONS?: Result of testing package version -1 against 1 was true
01-05 19:46:44.850 26692-26692/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
01-05 19:46:44.850 26692-26692/m.com.so48103235_updateprepopdb D/COPYFROMASSET: Copying Questions From Assets
01-05 19:46:44.855 26692-26692/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
01-05 19:48:10.375 26755-26755/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
01-05 19:48:10.376 26755-26755/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
01-05 19:48:10.376 26755-26755/m.com.so48103235_updateprepopdb D/NEWQUESTIONS?: Result of testing package version 1 against 1 was false
01-05 19:48:10.376 26755-26755/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
01-05 19:48:10.381 26755-26755/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
01-05 19:48:10.381 26755-26755/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
01-05 19:48:10.381 26755-26755/m.com.so48103235_updateprepopdb D/NEWQUESTIONS?: Result of testing package version 1 against 1 was false
01-05 19:48:10.382 26755-26755/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
01-05 19:48:10.387 26755-26755/m.com.so48103235_updateprepopdb D/COPYFROMASSET: Questions not copied from Assets - New Questions Check result was false DB Exists result was true