2013-08-02 139 views
0

我們正在嘗試使用PhoneGap/Cordova創建移動應用程序。我們需要使用預填充的SQLite數據庫來發布此應用程序。我們可以使用下面的代碼複製數據庫。但是當我們嘗試在我們的應用程序中訪問複製數據庫中的表時,我們得到'沒有這樣的表'的錯誤。任何人都可以幫助我們找出原因嗎?無法使用Android中的PhoneGap/Cordova訪問預填充的SQLite數據庫

我們使用以下代碼將數據庫複製到data/data/package_name/databases文件夾。

try 
    { 
     File dbFile = getDatabasePath("Bee_dict.db"); 
     if(!dbFile.exists()){ 
      this.copy("Bee_dict.db",dbFile.getAbsolutePath()); 
     } 
    } 
    catch (Exception e) 
    { 
    e.printStackTrace(); 
    } 


//And our copy function: 

    void copy(String file, String folder) throws IOException 
    { 
    File CheckDirectory; 
    CheckDirectory = new File(folder); 


    String parentPath = CheckDirectory.getParent(); 

    File filedir = new File(parentPath); 
    if (!filedir.exists()) { 
     if (!filedir.mkdirs()) { 
      return; 
     } 
    } 

     InputStream in = this.getApplicationContext().getAssets().open(file); 
     File newfile = new File(folder); 
     OutputStream out = new FileOutputStream(newfile); 


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

的index.html 下面的代碼是用來打開和訪問數據庫

function onDeviceReady() 
{ 
    db = window.openDatabase("Bee_dict", "1.0", "Bee_dict", 20000); 
    tx.executeSql('SELECT * FROM data WHERE word_id = ?', [1], querySuccess_definition, errorCB); 
} 

科爾多瓦版本 - 2.9

感謝。

回答

1

排在首位,可以在下一個DB文件名嘗試:

0000000000000001.db 

而對於加載文件:

File dbFile = getDatabasePath(".0000000000000001db"); 

的DB文件需要在未來的路線:

yourProyect/assets/0000000000000001.db 

我推薦使用「SQLitePlugin」:

SQLitePlugin GitHub

在「onDeviceReady()」函數使用:

if(!dbCreated){ 
    db = window.sqlitePlugin.openDatabase("0000000000000001", "1.0", "My Database", -1); 
} 
+0

在糖果和ics工作正常,但不工作在薑餅。 – KratosBala

+1

https://github.com/lite4cordova/Cordova-SQLitePlugin。 這個插件工作完美。 – KratosBala

+0

謝謝@KratosBala更新鏈接:https://github.com/brodysoft/Cordova-SQLitePlugin – bschandramohan

0

我面臨同樣的問題,但答案上面並沒有幫助我,所以我寫我的經驗,它可以幫助。

我通常使用一個名爲科爾多瓦插件:me.rahul.plugins.sqlDB在https://github.com/an-rahulpandey/cordova-plugin-dbcopy

這是一家專注於從科爾多瓦WWW文件夾的文件複製到在Android/iPhone正確的文件夾的插件。

你必須先安裝插件,使用:

$ cordova plugin add https://github.com/an-rahulpandey/cordova-plugin-dbcopy.git 

然後在設備就緒事件:

function onDeviceReady() { 
    console.log(">device is ready"); 
    window.plugins.sqlDB.copy("mydb.sqlite", copySuccess, copyError); 
} 
0

要訪問預填充的數據庫,你應該首先將數據庫文件複製在www目錄。

安裝這個插件DB-Copy pluginWWW目錄數據庫複製到設備中,使用複製的插件,然後使用Sqlite-storage plugin來訪問數據庫。

PS。從www目錄的數據庫的副本是需要它,因爲每個操作系統有不同的位置來存儲數據庫...

相關問題