copy sqlite database from assets folder to android persistentpath in unity3d

I have a squlite database in assets folder with name MyDatabse.s3db and I want to copy it to android Application.persistentdatapath.
I have used the following code

string filepath = Application.persistentDataPath + "/MyDatabase.s3db";
if (!File.Exists (filepath))
WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/MyDatabase.s3db");
Debug.Log("yield done");
while(!loadDB.isDone) {}
File.WriteAllBytes(filepath, loadDB.bytes);
Debug.Log("copy done");

The database is created in the Application.persistentDataPath path but it is empty.


For anyone hitting this issue:

This is likely to happen as you are not checking the file system before running this code I guess.

You should do this:

void SqliteFilePrep (string dbName)
    string pathDB = System.IO.Path.Combine (Application.persistentDataPath, dbName);
    string sourcePath = System.IO.Path.Combine (Application.streamingAssetsPath, dbName);

    //if DB does not exist in persistent data folder (folder "Documents" on iOS) or source DB is newer then copy it
    if (!System.IO.File.Exists (pathDB) || (System.IO.File.GetLastWriteTimeUtc(sourcePath) > System.IO.File.GetLastWriteTimeUtc(pathDB))) {

        if (sourcePath.Contains ("://")) {
            // Android  
            WWW www = new WWW (sourcePath);
            while (!www.isDone) {;}
            // you may want to put a timer or catch in the above!!
            if (String.IsNullOrEmpty(www.error)) {                  
                System.IO.File.WriteAllBytes(pathDB, www.bytes);
            } else {
                CanExQuery = false;                                     

        } else {
            // Mac, Windows, Iphone

            //validate the existens of the DB in the original folder (folder "streamingAssets")
            if (System.IO.File.Exists (sourcePath)) {

                //copy file - alle systems except Android
                System.IO.File.Copy (sourcePath, pathDB, true);

            } else {
                CanExQuery = false;
                Debug.Log ("ERROR: the file DB named " + dbName + " doesn't exist in the StreamingAssets Folder, please copy it there.");