2013-10-18 54 views
0

如果當時數據庫中移動我的應用程序負載店以下路徑:安卓:從應用程序包緩存刷新數據庫

DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"; 

我覺得這個數據庫是在緩存中存儲。

現在的問題是,當我更新數據庫讓我們說,我添加一個表並啓動我的應用程序,然後舊版本的數據庫不更新與更新之一。因此它給了我SQLException。

當我啓動我的應用程序時,是否有任何方法更新數據庫,如果它在緩存中?

編輯

請告訴我什麼是錯在我的代碼。 這裏是我的代碼:

​​3210

,這裏是DatabaseHandler.java文件

public class DatabaseHandler extends SQLiteOpenHelper { 
private static String TAG = "DatabaseHandler"; // Tag just for the LogCat 
               // window 
// destination path (location) of our database on device 
private static String DB_PATH = ""; 
private static String DB_NAME = "ItemManager";// Database name 
private SQLiteDatabase mDataBase; 
private final Context mContext; 

public DatabaseHandler(Context context) { 
    super(context, DB_NAME, null, 1);// 1? its Database Version 
    DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"; 
    this.mContext = context; 
} 

public void createDataBase() throws IOException { 
    // If database not exists copy it from the assets 

    boolean mDataBaseExist = checkDataBase(); 
    if (!mDataBaseExist) { 
     this.getReadableDatabase(); 
     this.close(); 
     try { 
      // Copy the database from assests 
      copyDataBase(); 
      Log.e(TAG, "createDatabase database created"); 
     } catch (IOException mIOException) { 
      throw new Error("ErrorCopyingDataBase"); 
     } 
    } 
} 

// Check that the database exists here: /data/data/your package/databases/Da 
// Name 
private boolean checkDataBase() { 
    File dbFile = new File(DB_PATH + DB_NAME); 
    // Log.v("dbFile", dbFile + " "+ dbFile.exists()); 
    return dbFile.exists(); 
} 

// Copy the database from assets 
private void copyDataBase() throws IOException { 
    InputStream mInput = mContext.getAssets().open(DB_NAME); 
    String outFileName = DB_PATH + DB_NAME; 
    OutputStream mOutput = new FileOutputStream(outFileName); 
    byte[] mBuffer = new byte[1024]; 
    int mLength; 
    while ((mLength = mInput.read(mBuffer)) > 0) { 
     mOutput.write(mBuffer, 0, mLength); 
    } 
    mOutput.flush(); 
    mOutput.close(); 
    mInput.close(); 
} 

// Open the database, so we can query it 
public boolean openDataBase() throws SQLException { 
    String mPath = DB_PATH + DB_NAME; 
    // Log.v("mPath", mPath); 
    mDataBase = SQLiteDatabase.openDatabase(mPath, null, 
      SQLiteDatabase.CREATE_IF_NECESSARY); 
    // mDataBase = SQLiteDatabase.openDatabase(mPath, null, 
    // SQLiteDatabase.NO_LOCALIZED_COLLATORS); 
    return mDataBase != null; 
} 

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

@Override 
public void onCreate(SQLiteDatabase arg0) { 

} 

,並在OnCreate()在方法MainActivity.java文件:

DataBaseAdapter mDbHelper = new DataBaseAdapter(this); 
    mDbHelper.createDatabase(); 
    mDbHelper.open(); 

編輯

登錄貓

10-18 19:33:56.000: E/AndroidRuntime(14285): java.lang.RuntimeException: Unable to start activity 

ComponentInfo{com.example.demo/com.example.demo.EmployeeActivity}: android.database.sqlite.SQLiteException: no such table: Video: , while compiling: SELECT * FROM Video WHERE EMPID=1 
10-18 19:33:56.000: E/AndroidRuntime(14285): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at android.os.Looper.loop(Looper.java:130) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at android.app.ActivityThread.main(ActivityThread.java:3687) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at java.lang.reflect.Method.invokeNative(Native Method) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at java.lang.reflect.Method.invoke(Method.java:507) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at dalvik.system.NativeStart.main(Native Method) 
10-18 19:33:56.000: E/AndroidRuntime(14285): Caused by: android.database.sqlite.SQLiteException: no such table: Video: , while compiling: SELECT * FROM Video WHERE EMPID=1 
10-18 19:33:56.000: E/AndroidRuntime(14285): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1358) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1326) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at com.example.demo.DatabaseHandler.getAllVideos(DatabaseHandler.java:151) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at com.example.demoEmployeeActivity.setAdapter(VideoPlayActivity.java:52) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at com.example.demo.EmployeeActivity.onCreate(VideoPlayActivity.java:42) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
10-18 19:33:56.000: E/AndroidRuntime(14285): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 

在此先感謝。

+0

我相信你必須創建自己的數據庫,然後添加表。本教程可能會幫助http://www.vogella.com/articles/AndroidSQLite/article.html –

+0

是@BhanuKaushik。我正在SQLite數據庫瀏覽器中創建數據庫,並將該文件複製到Assets文件夾中。我完成了它的操作。但我沒有得到更新的表意味着數據庫無法在緩存中更新,當我重新啓動應用程序,這是一個問題。 – SSD

+0

哦......你是否在你的onCreate方法中創建一個新的數據庫。你的「MainActivity」? –

回答

0

您將要升級時刪除數據庫,以便您的代碼可以自動重新創建它。爲此,請添加

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // This database is only a cache for online data, so its upgrade policy is 
    // to simply to discard the data and start over 
    db.execSQL("DROP TABLE IF EXISTS " + DB_NAME); 
    createDataBase(); 
} 

然後,無論何時升級版本號,數據庫都將被刪除並重新創建。

注意:我沒有真正測試過它,但是這應該起作用。