2011-12-15 32 views
0

我想一個SQLite數據庫複製到數據庫文件夾,但不知道如何CopyTo從API的使用:如何使用CopyTo從API中的PhoneGap

function win(entry) { 
console.log("New Path: " + entry.fullPath); 
} 

function fail(error) { 
alert(error.code); 
} 

function copyFile(entry) { 
var parent = document.getElementById('parent').value, 
    parentEntry = new DirectoryEntry({fullPath: parent}); 

// copy the file to a new directory and rename it 
entry.copyTo(parentEntry, "file.copy", success, fail); 
} 

是什麼項目嗎?在哪裏我必須寫我的數據庫路徑表單資產文件夾?是否有人認爲在最後一行說成功但沒有定義它?我必須寫勝利嗎?

回答

1

File API無法訪問資產目錄內的文件。你需要編寫一個插件。

+0

謝謝西蒙 我想創造一個SQLite數據庫,並把它複製到數據庫forlder在第一次運行。所以我看到唯一的辦法是在應用程序的第一次運行中將xml或其他類型的數據庫(如lawnchair)導入到sqlite中。我對嗎? – 2011-12-15 18:58:48

+0

有什麼方法可以在phonegap中使用預先存儲的數據庫嗎? – 2011-12-15 19:09:18

1

我做到了通過添加一些代碼,我的主要活動(java代碼):

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

    import android.app.Activity; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.widget.EditText; 

    public class main extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     String pName = this.getClass().getPackage().getName(); 
     try 
     { 
     this.copy("Databases.db","/data/data/"+pName+"/app_database/"); 
     } 

     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(); 

    } 
    } 

來源: http://gauravstomar.blogspot.com/2011/08/prepopulate-sqlite-in-phonegap.html