我是.net开发者,并在空闲时间制作Android应用程序。我制作了一个有大约700首歌曲的音乐库应用程序。我在数据库中添加了歌曲。当我在实际应用程序时,我将从Asset文件夹中复制数据库,然后在SD卡中复制。现在我已经在Play商店中部署了应用程序,我想用更多歌曲更新数据库。我不想更新任何数据库结构,也不想删除任何东西。我想添加更多100首歌曲。所以我想在部署新版本时复制新数据库。
我google了很多,花了很多时间。每个人都说onUpgrade函数调用,我们去更新版本。我的问题是,Control永远不会出现这种方法。我已经把Log语句,我可以看到它永远不会执行。我已经多次更新了db并更新了版本,但是“onUpgrade”从未调用过。
这是我的代码......
public class DatabaseHelper extends SQLiteOpenHelper
{
private static String DB_PATH;
private static String DB_NAME1 = "question.db"; // Asset Folder DB Name
public static final String KEY_NAME = "Name";
protected static String DB_NEWPATH;
private Context myContext;
final static int DB_VERSION = 16;
private SQLiteDatabase myDataBase;
public DatabaseHelper(Context context)
{
super(context,DB_NAME1,null,DB_VERSION);
this.myContext = context;
DB_PATH = "/data/data/"+context.getPackageName()+"/databases/";
Log.i("DatabaseHelper,Path", "********"+DB_PATH);
}
/*
* Creates a empty database on the system a
*
*/
public void CreateDatabase() throws IOException
{
boolean dbExists = checkDatabase();
Log.i("CreateDatabase", "********CreateDatabase,dbExist="+dbExists);
if(!dbExists)
{
this.getReadableDatabase();
try {
copyDatabase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
public boolean checkDatabase()
{
Log.i("checkDatabase", "********checkDatabase");
SQLiteDatabase checkDB = null;
try{
Log.d("DB Path=", DB_NEWPATH);
checkDB = SQLiteDatabase.openDatabase(DB_NEWPATH, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){ }
if(checkDB != null){ checkDB.close(); }
return (checkDB != null) ? true : false;
}
private void copyDatabase() throws IOException
{
//Open your local db as the input stream
Log.i("copyDatabase", "********copyDatabase");
InputStream myInput = myContext.getAssets().open(DB_NAME1); // Path to the asset folder database
// Path to the just created empty db
Log.d("in CopyDB method DB Path=", DB_NEWPATH);
String outFileName = DB_NEWPATH;
FileOutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while((length = myInput.read(buffer)) > 0)
{
myOutput.write(buffer,0,length);
}
Log.i("copyDatabase", "********copy Done");
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
/*
* Open Database
*
*/
public SQLiteDatabase openDB() throws SQLException
{
Log.i("openDB", "********openDB");
myDataBase = SQLiteDatabase.openDatabase(DB_NEWPATH, null, SQLiteDatabase.OPEN_READWRITE);
return myDataBase;
}
/*
* Close Database
*/
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg0) {
/* Database already created */
Log.i("onCreate", "********onCreate");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
Log.i("OnUpgrade", "This method is not going to execute....What is the reason even I am updating version each time....");
/* No upgrade of Database */
}
}
每次部署时,我都会复制新数据库。已安装应用程序的用户未获取我的更新cz此onUpgrade未执行。
任何帮助?