2013-06-01 124 views
1

我开发了一个应用程序,其中包含资产数据库文件夹。运行时将数据库副本从资产文件夹复制到设备。代码如下:更新SQLite数据库没有反映

public class DataBaseHelperClass extends SQLiteOpenHelper{ 
private final Context context; 

private static String DB_PATH = ""; 
// Data Base Name. 
private static final String DATABASE_NAME = "db.sqlite"; 
// Data Base Version. 
private static final int DATABASE_VERSION = 1; 
// Table Names of Data Base. 
static final String TABLE = "MyTable"; 

static SQLiteDatabase sqliteDataBase; 

@SuppressLint("SdCardPath") 
public DataBaseHelperClass(Context context) {  
    super(context, DATABASE_NAME, null ,DATABASE_VERSION); 
    this.context = context; 
    //The Android's default system path of your application database. 
    DB_PATH = "/data/data/com.abc.myapp/databases/"; 
} 

public void createDataBase() throws IOException{ 
    //check if the database exists 
    boolean databaseExist = checkDataBase(); 

    if(databaseExist){ 
     this.getWritableDatabase(); 
    }else{ 
     this.getReadableDatabase(); 
     copyDataBase(); 
    }// end if else dbExist 
} // end createDataBase(). 

public boolean checkDataBase(){ 
    File databaseFile = new File(DB_PATH + DATABASE_NAME); 
    return databaseFile.exists();   
} 

private void copyDataBase() throws IOException{ 
    //Open your local db as the input stream 
    InputStream myInput = context.getAssets().open(DATABASE_NAME); 
    // Path to the just created empty db 
    String outFileName = DB_PATH + DATABASE_NAME; 
    //Open the empty db as the output stream 
    OutputStream myOutput = new FileOutputStream(outFileName); 
    //transfer bytes from the input file 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 + DATABASE_NAME; 
    sqliteDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); 
} 

@Override 
public synchronized void close() { 
    if(sqliteDataBase != null) 
     sqliteDataBase.close(); 
    super.close(); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    // No need to write the create table query. 
    // As we are using Pre built data base. 
    // Which is ReadOnly. 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

} 

此代码正常工作。

我创建了数据基础的新表,

变化记录,并在现有的表中添加记录:但是现在我作为取得了数据库的一些变化。

,改变了的onUpdate数据库辅助类的如:

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    if(newVersion > oldVersion){ 
     //db.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME); 
     context.deleteDatabase(DATABASE_NAME); 
    } 
} 

并增加从1数据库版本为2

产生了新的APK和设备安装。它取代旧的应用程序并安装新的应用程序,但数据库中所做的更改未反映。

现在,我应该如何删除旧版本的数据库,并在更新应用程序时将新的数据库从assets文件夹复制到设备中。

请指导我解决这个问题,因为我在Google上搜索这个问题,但没有得到任何工作解决方案来删除和更新新的数据库。

回答

0

你的OnCreate()应该是:

@Override 
public void onCreate(SQLiteDatabase db){ 
    createDB(); 
} 

OnUpgrade()方法是:

@Override 
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion){ 
    db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE); 
    onCreate(db); 
} 

的CreateDatabase()是这样的:

public void createDataBase(){ 
    boolean dbExist = dbExists(); 
    if(!dbExist){ 
     copyDataBase(); 
    } 
    else if(dbExist){ 
     copyDataBase(); 
    } 
} 

然后,它会正常工作。

+0

似乎你的两个答案实际上很少添加,马上被@ManojFegde接受。如果你是同一个人,那么你做错了。您可以拥有多个帐户,但以游戏声誉系统的方式行事是不行的。 – laalto

2

您可以使用此代码对你的帮助是:

public class DataBaseHelper extends SQLiteOpenHelper { 

private static final String DB_PATH = "/data/data/com.project.mydb/databases/"; 
private static final String DB_NAME = "mydb.db"; 
private static final String DB_TABLE = "words"; 
private static final int DB_VERSION = 6; 
private static final String TAG = "DataBaseHelper"; 
int id = 0; 
Random random = new Random(); 
private SQLiteDatabase myDataBase; 
private final Context myContext; 

public DataBaseHelper(Context context){ 
    super(context, DB_NAME, null, DB_VERSION); 
    this.myContext = context; 
} 

@Override 
public void onCreate(SQLiteDatabase db){ 
    createDB(); 
} 

@Override 
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion){ 
    Log.w(TAG, "Upgrading DB from version " + oldVersion + " to " + 
      newVersion + ", which will destroy all old data"); 
    db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE); 
    onCreate(db); 
} 

public void createDataBase(){ 
    createDB(); 
} 

private void createDB(){ 
    boolean dbExist = dbExists(); 
    if(!dbExist){ 
     copyDataBase(); 
    } 
    else if(dbExist){ 
     copyDataBase(); 
    } 
} 

private boolean dbExists(){ 
    SQLiteDatabase db = null; 
    try{ 
     String dbPath = DB_PATH + DB_NAME; 
     db = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE); 
     db.setLocale(Locale.getDefault()); 
     db.setLockingEnabled(true); 
     db.setVersion(DB_VERSION); 
    } 
    catch(SQLiteException e){ 
     Log.e("SQL Helper", "database not found"); 
    } 
    if(db != null){ 
     db.close(); 
    } 
    return db != null ? true : false; 
} 

private void copyDataBase(){ 
    InputStream iStream = null; 
    OutputStream oStream = null; 
    String outFilePath = DB_PATH + DB_NAME; 
    try{ 
     iStream = myContext.getAssets().open(DB_NAME); 
     oStream = new FileOutputStream(outFilePath); 
     byte[] buffer = new byte[1024]; 
     int length; 
     while((length = iStream.read(buffer))>0){ 
      oStream.write(buffer,0,length); 
     } 
     oStream.flush(); 
     oStream.close(); 
     iStream.close(); 
    } 
    catch(IOException ioe){ 
     throw new Error("Problem copying database from resource file."); 
    } 
} 

public void openDataBase() throws SQLException { 
    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(); 
} 
} 

希望这会帮助你。