2014-01-10 62 views
1

我正在使用SQLLite for Android基於phonegap的應用程序。我面臨的問題是,只要應用程序處於打開狀態,我就能夠獲取存儲的任何數據local.db文件。 對於例如我有一個設置功能,用戶可以保存他/她的設置,並進入db.Now我面臨的問題是,我無法獲取用戶在應用程序關閉後保存的數據,只要應用程序處於打開狀態,我就可以獲取數據。 所以問題是數據庫不持久。任何人都可以告訴即使應用程序關閉時如何將數據存儲在內存中?使用Phonegap從Android的SQLLite數據庫填充數據

這是我mainActivity文件

public class Demo extends CordovaActivity 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     super.init(); 
     super.loadUrl(Config.getStartUrl());    
     try { 
      String pName = this.getClass().getPackage().getName(); 
      this.copy("Databases.db", "/data/data/" + pName + "/app_database/"); 
      this.copy("0000000000000001.db", "/data/data/" + pName 
        + "/app_database/file__0/"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

    void copy(String file, String folder) throws IOException { 

     File CheckDirectory; 
     CheckDirectory = new File(folder); 
     if (!CheckDirectory.exists()) { 
      CheckDirectory.mkdir(); 
      InputStream in = getApplicationContext().getAssets().open(file); 
     OutputStream out = new FileOutputStream(folder + file); 

     // Transfer bytes from in to out 
     byte[] buf = new byte[1024]; 
     int len; 
     while ((len = in.read(buf)) > 0) 
      out.write(buf, 0, len); 
     in.close(); 
     out.close(); 
     } 
    } 
} 

這些都是我給的權限: -

 <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.VIBRATE" /> 
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

複製代碼移動到if條件由提到NigelK做了之後trick.But卸載後構建並創建一個新的版本給我以下錯誤: -

01-10 16:11:34.466: E/SQLiteLog(29253): (1) no such table: CacheGroups 
01-10 16:11:34.466: D/WebKit(29253): ERROR: 
01-10 16:11:34.466: D/WebKit(29253): Application Cache Storage: failed to execute statement "DELETE FROM CacheGroups" error "no such table: CacheGroups" 
01-10 16:11:34.466: D/WebKit(29253): external/webkit/Source/WebCore/loader/appcache/ApplicationCacheStorage.cpp(558) : bool WebCore::ApplicationCacheStorage::executeSQLCommand(const WTF::String&) 
01-10 16:11:34.466: E/SQLiteLog(29253): (1) no such table: Caches 
01-10 16:11:34.466: D/WebKit(29253): ERROR: 
01-10 16:11:34.466: D/WebKit(29253): Application Cache Storage: failed to execute statement "DELETE FROM Caches" error "no such table: Caches" 
01-10 16:11:34.466: D/WebKit(29253): external/webkit/Source/WebCore/loader/appcache/ApplicationCacheStorage.cpp(558) : bool WebCore::ApplicationCacheStorage::executeSQLCommand(const WTF::String&) 
01-10 16:11:34.466: E/SQLiteLog(29253): (1) no such table: Origins 
01-10 16:11:34.466: D/WebKit(29253): ERROR: 
01-10 16:11:34.466: D/WebKit(29253): Application Cache Storage: failed to execute statement "DELETE FROM Origins" error "no such table: Origins" 
01-10 16:11:34.466: D/WebKit(29253): external/webkit/Source/WebCore/loader/appcache/ApplicationCacheStorage.cpp(558) : bool WebCore::ApplicationCacheStorage::executeSQLCommand(const WTF::String&) 
01-10 16:11:34.476: E/SQLiteLog(29253): (1) no such table: DeletedCacheResources 

有關如何解決此問題的任何想法?

回答

2

你的數據庫是持久性的,問題是你的應用程序將啓動每次,在製作的onCreate副本()方法調用()與資產舉辦的副本再次覆蓋數據庫。我想你只想複製一次。一種方法是隻能做副本,如果目標目錄不存在:

if (!CheckDirectory.exists()) { 
    CheckDirectory.mkdir(); 
    ...move the copy code inside this if block 
} 

或者你也可以存儲在共享偏好的標誌,並設置爲true時,你的副本。在onCreate中,只有在該標誌爲false的情況下才能執行副本。

編輯:

要使用共享偏好標誌:

在的onCreate:

SharedPreferences sp = getSharedPreferences("MYPREFS", Activity.MODE_PRIVATE); 

//If no shared prefs exist, e.g. first install, it doesn't matter - the following will return false as a default 
Boolean database_copied = sp.getBoolean("database_copied", false); 

if (!database_copied) 
{ 
    try { 
     String pName = this.getClass().getPackage().getName(); 
     this.copy("Databases.db", "/data/data/" + pName + "/app_database/"); 
     this.copy("0000000000000001.db", "/data/data/" + pName 
       + "/app_database/file__0/"); 
     SharedPreferences.Editor editor = sp.edit(); 
     editor.putBoolean("database_copied", true); 
     editor.apply(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
+0

是的複製代碼移動到if條件做了trick.Thanks很多:) –

+0

NigelK:卸載以前的構建和創建一個新的構建現在向我顯示一個錯誤。這是我得到的錯誤: - E/SQLiteLog(26682):(1)沒有這樣的表:CacheGroups。任何想法我做錯了什麼? –

+0

我用錯誤日誌更新了我的問題,請檢查並讓我知道我做錯了什麼。 –

0

只需要創建一個空數據庫,並與新的資產DB更換..

Thread getData = new Thread() { 
     public void run() { 

      Data.qh = new QueryHandler(mContext); // for creating a blank DB 
      DB_PATH = getDatabasePath("htest.sqlite").getAbsolutePath(); // get the path of blank DB 
      copyDataBase(mContext); 

     }; 
    }; 


void copyDataBase(Context mContext) { 

     try { 

      String outFileName = DB_PATH; 

      OutputStream myOutput = new FileOutputStream(outFileName); 

      byte[] buffer = new byte[1024]; 
      int length; 

      InputStream myInput = mContext.getAssets().open("htest.sqlite"); 
      while ((length = myInput.read(buffer)) > 0) { 
       myOutput.write(buffer, 0, length); 
      } 
      myInput.close(); 

      myOutput.flush(); 
      myOutput.close(); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

    } 
0

下面是示例代碼,這是在HTML頁面來實現JavaScript調用

function onDeviceReady() 
{ 
    var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000); 
    db.transaction(populateDB, errorCB, successCB); 
} 

然後我們你必須填寫表中的數據,調用方法

function populateDB(tx) 
{ 
    var id=$('#id').val(); 
    var firstname=$('#firstname').val();   
    tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, firstname)'); 
    tx.executeSql('INSERT INTO DEMO (id, firstname) VALUES (?, ?)', [id, firstname]); 
} 

要保存數據,請執行以下操作在任何情況下

<button class="button" id="clickMe" onclick="onDeviceReady();" >Save</button> 
+0

我已經做了你所說的話,其實這個問題是數據庫心不是獲取已保存的記錄以防應用程序關閉,否則在應用程序打開時工作正常。 –