这是我的问题。我有一个已上传到 Google Play 商店的应用程序。该应用程序运行良好,直到它到达我为我在预填充的 sqlite 数据库中创建的级别添加新记录到我的数据库的部分,当我到达这个特定级别时它崩溃。就像没有创建新信息一样。代码如下。我已经读到我需要在 onupgrade() 方法中创建一个版本变量并增加它。但我是这个 sqlite 学习的新手。如果我创建一个版本变量,代码会跟踪它吗?我是否必须用版本号重命名数据库,请帮忙。
我已尝试在此处遵循以下此示例,这似乎是我正在寻找的内容,但我的问题是该示例是所有在代码中完成的版本控制,还是我必须将版本号附加到我的 *. db 文件。
package com.xtremeware.straighthoodtrivia.db;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import com.xtremeware.straighthoodtrivia.quiz.Question;
public class DBHelper extends SQLiteOpenHelper
{
// The androids's default system path of the application database.
private static String DB_PATH = "/data/data/com.xtremeware.straighthoodtrivia /databases/";
private static String DB_NAME = "QuestionsDb";
private SQLiteDatabase myDataBase;
private final Context myContext;
// Constructor
// Takes and keeps a reference of the passed context in order to access to the
// applications assets and resources.
// @ context
public DBHelper(Context context)
{
super(context, DB_NAME, null, 1);
this.myContext = context;
}
// Creates a empty database on the system and rewrites it with my own database
public void createDataBase() throws IOException
{
boolean dbExist = checkDataBase();
if(!dbExist)
{
// By calling this method an empty database will be created into the default system path
// of my application so I am gonna be able to overwrite the database with my database.
this.getReadableDatabase();
try
{
copyDataBase();
}
catch (IOException e)
{
throw new Error("Error copying database");
}
}
}
// Check if the database already exists 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 doesn't exist yet.
}
if(checkDB != null)
{
checkDB.close();
}
return checkDB != null ? true : false;
}
// Copies the 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 transferring bytestreams.
private void copyDataBase() throws IOException
{
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the empty db just created
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 output file
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 = DB_PATH + 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)
{
}