IEnumerator PerformanceTestCoroutine() { db = new SQLiteDB(); log = ""; yield return(StartCoroutine( CopyFileFromStreamingAssetsToPersistanceFolder("db.sqlite"))); // a product persistant database path. string filename = ExtensionMethods.AppWritableDirectory() + "/db.sqlite"; // it mean we already download prebuild data base and store into persistantPath // lest update, I will call Test try{ // // initialize database // db.Open(filename); log += "Database created! filename:" + filename; PerformanceTest(db, ref log); } catch (Exception e) { log += "\nTest Fail with Exception " + e.ToString(); } }
private static void CopyDRJSONFiles(string lang) { try { string drJSONBaseFolder = ExtensionMethods.AppWritableDirectory() + DRJSONBaseFolderPath; if (!Directory.Exists(drJSONBaseFolder)) { Directory.CreateDirectory(drJSONBaseFolder); } string drJSONFolder = ExtensionMethods.AppWritableDirectory() + DRJSONFolderPath(lang); if (!Directory.Exists(drJSONFolder)) { Directory.CreateDirectory(drJSONFolder); } } catch (IOException ex) { Debug.Log(ex.Message); } string relPath = DRJSONRelativePath(lang, drCacheFileName); string drCachePath = ExtensionMethods.AppWritableDirectory() + relPath; if (!File.Exists(drCachePath)) { File.Copy(Application.streamingAssetsPath + relPath, drCachePath); } relPath = DRJSONRelativePath(lang, drDisplayDateMapFileName); string drDisplayDateMapPath = ExtensionMethods.AppWritableDirectory() + relPath; if (!File.Exists(drDisplayDateMapPath)) { File.Copy(Application.streamingAssetsPath + relPath, drDisplayDateMapPath); } }
// Use this for initialization void Start() { asyncDB = new SQLiteAsync(); // open database asyncDB.Open(ExtensionMethods.AppWritableDirectory() + "/demo_for_async_db.db", null, null); // create test table. asyncDB.Query(queryCreate, CreateQueryCreated, null); }
private static Dictionary <string, string> FetchDRDisplayDateMap(string lang) { Dictionary <string, string> drDisplayDateMap = new Dictionary <string, string> (); string drDisplayDateMapPath = ExtensionMethods.AppWritableDirectory() + DRJSONRelativePath(lang, drDisplayDateMapFileName); JsonHelper drJson = new JsonHelper(); Dictionary <string, object> allDRObjects = drJson.ReadAndDeserializeJson(drDisplayDateMapPath, false); foreach (string date in new List <string>(allDRObjects.Keys)) { drDisplayDateMap [date] = (string)(allDRObjects [date]); } return(drDisplayDateMap); }
private static Dictionary <string, DailyReflection> FetchDRMapFromJson(string lang) { Dictionary <string, DailyReflection> drMap = new Dictionary <string, DailyReflection> (); string drCachePath = ExtensionMethods.AppWritableDirectory() + DRJSONRelativePath(lang, drCacheFileName); JsonHelper drJson = new JsonHelper(); Dictionary <string, object> allDRObjects = drJson.ReadAndDeserializeJson(drCachePath, false); foreach (string date in new List <string>(allDRObjects.Keys)) { DailyReflection dr = ExtractDR(allDRObjects [date]); drMap [date] = dr; } return(drMap); }
IEnumerator AsyncPerformanceTestCoroutine() { // Copy Database yield return(StartCoroutine( CopyFileFromStreamingAssetsToPersistanceFolder("db.sqlite"))); SQLiteExt.Handle handle = new SQLiteExt.Handle(); // Open Database yield return(StartCoroutine( this.SQLiteOpenDatabase(ExtensionMethods.AppWritableDirectory() + "/db.sqlite", handle))); // Select yield return(StartCoroutine( this.SQLiteQuery("SELECT * FROM en WHERE word like 'a%' LIMIT 1", handle))); // Step yield return(StartCoroutine( this.SQLiteStep(handle))); if (handle.Success) { UnityEngine.Debug.Log(handle.Query.GetString("word")); } // release query yield return(StartCoroutine( this.SQLiteRelease(handle))); // Close database yield return(StartCoroutine( this.SQLiteCloseDatabase(handle))); log += "\ndone."; }
IEnumerator CopyFileFromStreamingAssetsToPersistanceFolder(string dbfilename) { // a product persistant database path. string filename = ExtensionMethods.AppWritableDirectory() + "/" + dbfilename; yield return(0); // check if database already exists. if (!File.Exists(filename)) { // ok , this is first time application start! // so lets copy prebuild dtabase from StreamingAssets and load store to persistancePath with Test2 byte[] bytes = null; #if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX string dbpath = "file://" + Application.streamingAssetsPath + "/" + dbfilename; log += "asset path is: " + dbpath; WWW www = new WWW(dbpath); yield return(www); bytes = www.bytes; #elif UNITY_WEBPLAYER string dbpath = "StreamingAssets/" + dbfilename; log += "asset path is: " + dbpath; WWW www = new WWW(dbpath); yield return(www); bytes = www.bytes; #elif UNITY_IPHONE string dbpath = Application.dataPath + "/Raw/" + dbfilename; log += "asset path is: " + dbpath; try{ using (FileStream fs = new FileStream(dbpath, FileMode.Open, FileAccess.Read, FileShare.Read)){ bytes = new byte[fs.Length]; fs.Read(bytes, 0, (int)fs.Length); } } catch (Exception e) { log += "\nTest Fail with Exception " + e.ToString(); log += "\n"; } #elif UNITY_ANDROID string dbpath = Application.streamingAssetsPath + "/" + dbfilename; log += "asset path is: " + dbpath; WWW www = new WWW(dbpath); yield return(www); bytes = www.bytes; #endif if (bytes != null) { try{ // // // copy database to real file into cache folder using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write)) { fs.Write(bytes, 0, bytes.Length); log += "\nCopy database from streaminAssets to persistentDataPath: " + filename; } } catch (Exception e) { log += "\nTest Fail with Exception " + e.ToString(); log += "\n\n Did you copy test.db into StreamingAssets ?\n"; } } } }
IEnumerator CopyDB2PlayerPrefs() { log = ""; // a product persistant database path. string filename = ExtensionMethods.AppWritableDirectory() + "/test.db"; yield return(0); // check if database already exists. if (!File.Exists(filename)) { // ok , this is first time application start! // so lets copy prebuild dtabase from StreamingAssets and load store to persistancePath with Test2 string dbfilename = "test.db"; byte[] bytes = null; #if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX string dbpath = "file://" + Application.streamingAssetsPath + "/" + dbfilename; log += "asset path is: " + dbpath; WWW www = new WWW(dbpath); yield return(www); bytes = www.bytes; #elif UNITY_WEBPLAYER string dbpath = "StreamingAssets/" + dbfilename; log += "asset path is: " + dbpath; WWW www = new WWW(dbpath); yield return(www); bytes = www.bytes; #elif UNITY_IPHONE string dbpath = Application.dataPath + "/Raw/" + dbfilename; log += "asset path is: " + dbpath; try{ using (FileStream fs = new FileStream(dbpath, FileMode.Open, FileAccess.Read, FileShare.Read)){ bytes = new byte[fs.Length]; fs.Read(bytes, 0, (int)fs.Length); } } catch (Exception e) { log += "\nTest Fail with Exception " + e.ToString(); log += "\n"; } #elif UNITY_ANDROID string dbpath = Application.streamingAssetsPath + "/" + dbfilename; log += "asset path is: " + dbpath; WWW www = new WWW(dbpath); yield return(www); bytes = www.bytes; #endif if (bytes != null) { try{ StringBuilder sb = new StringBuilder(bytes.Length * 2); foreach (byte b in bytes) { string bz = System.Convert.ToString(b, 16); if (bz.Length == 1) { bz = "0" + bz; } sb.Append(bz); } string sqlitedb = sb.ToString(); PlayerPrefs.SetString("sqlitedb", sqlitedb); } catch (Exception e) { log += "\nTest Fail with Exception " + e.ToString(); log += "\n\n Did you copy test.db into StreamingAssets ?\n"; } } } }
IEnumerator TestBestForRealProject() { db = new SQLiteDB(); log = ""; // a product persistant database path. string filename = ExtensionMethods.AppWritableDirectory() + "/demo_from_streamingAssets2.db"; // check if database already exists. yield return(0); if (!File.Exists(filename)) { // ok , this is first time application start! // so lets copy prebuild dtabase from StreamingAssets and load store to persistancePath with Test2 string dbfilename = "test.db"; byte[] bytes = null; #if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX string dbpath = "file://" + Application.streamingAssetsPath + "/" + dbfilename; log += "asset path is: " + dbpath; WWW www = new WWW(dbpath); yield return(www); bytes = www.bytes; #elif UNITY_WEBPLAYER string dbpath = "StreamingAssets/" + dbfilename; log += "asset path is: " + dbpath; WWW www = new WWW(dbpath); yield return(www); bytes = www.bytes; #elif UNITY_IPHONE string dbpath = Application.dataPath + "/Raw/" + dbfilename; log += "asset path is: " + dbpath; try{ using (FileStream fs = new FileStream(dbpath, FileMode.Open, FileAccess.Read, FileShare.Read)){ bytes = new byte[fs.Length]; fs.Read(bytes, 0, (int)fs.Length); } } catch (Exception e) { log += "\nTest Fail with Exception " + e.ToString(); log += "\n"; } #elif UNITY_ANDROID string dbpath = Application.streamingAssetsPath + "/" + dbfilename; log += "asset path is: " + dbpath; WWW www = new WWW(dbpath); yield return(www); bytes = www.bytes; #endif if (bytes != null) { try{ // // // copy database to real file into cache folder using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write)) { fs.Write(bytes, 0, bytes.Length); log += "\nCopy database from streaminAssets to persistentDataPath: " + filename; } // // initialize database // db.Open(filename); log += "\nDatabase created! filename: " + filename; Test2(db, ref log); } catch (Exception e) { log += "\nTest Fail with Exception " + e.ToString(); log += "\n\n Did you copy test.db into StreamingAssets ?\n"; } } } else { // it mean we already download prebuild data base and store into persistantPath // lest update, I will call Test try{ // // initialize database // db.Open(filename); log += "Database created! filename:" + filename; Test(db, ref log); } catch (Exception e) { log += "\nTest Fail with Exception " + e.ToString(); log += "\n on WebPlayer it must give an exception, it's normal."; } } }
void OnGUI() { if (db == null) { if (GUI.Button(new Rect(10, 10, 150, 50), "Run SQLite Test \nat persistentDataPath")) { db = new SQLiteDB(); string filename = ExtensionMethods.AppWritableDirectory() + "/demo_1.db"; log = ""; try{ // // initialize database // db.Open(filename); log += "Database created! filename:" + filename; Test(db, ref log); } catch (Exception e) { log += "\nTest Fail with Exception " + e.ToString(); log += "\n on WebPlayer it must give an exception, it's normal."; } } if (GUI.Button(new Rect(10, 70, 150, 70), "Run SQLite Test \nat :memory:\nbest for cache")) { db = new SQLiteDB(); log = ""; try{ // // initialize database // db.OpenInMemory(); log += "Database created! in :memory:"; Test(db, ref log); } catch (Exception e) { log += "\nTest Fail with Exception " + e.ToString(); log += "\n"; } } if (GUI.Button(new Rect(10, 150, 150, 70), "Run SQLite Test \nwith stream\nbest for WebPlayer")) { db = new SQLiteDB(); MemoryStream memStream = new MemoryStream(); log = ""; try{ // // initialize database // db = new SQLiteDB(); db.OpenStream("stream1", memStream); log += "Database created! named stream: stream1"; Test(db, ref log); } catch (Exception e) { log += "\nTest Fail with Exception " + e.ToString(); log += "\n"; } } if (GUI.Button(new Rect(10, 230, 150, 70), "Run SQLite Test \nwith streamingAssets")) { StartCoroutine(TestWithStreamingAssets()); } if (GUI.Button(new Rect(170, 10, 150, 70), "Run SQLite Test \nfor streamingAssets db\nwhich moved to \npersistentDataPath")) { StartCoroutine(TestWithStreamingAssetsCopiedToPersistentDataPath()); } // // Very common scenario for most products. // if (GUI.Button(new Rect(170, 90, 150, 70), "Run SQLite\nfor real project scenario")) { StartCoroutine(TestBestForRealProject()); } #if ENCRYPTION // // ENCRYPTION // if (GUI.Button(new Rect(170, 170, 150, 70), "Run SQLite\nfor ENCRYPTION scenario")) { try { db = new SQLiteDB(); log = ""; // a product persistant database path. string filename = ExtensionMethods.AppWritableDirectory() + "/demo_from_encryption.db"; File.Delete(filename); db.Open(filename); // // set ENCRYPTION // SQLiteQuery qr = new SQLiteQuery(db, "PRAGMA hexkey=\"0x0102030405060708090a0b0c0d0e0f10\";"); qr.Step(); qr.Release(); // // create table // qr = new SQLiteQuery(db, queryCreate); qr.Step(); qr.Release(); log += "\nTable created."; // // insert string and blob // qr = new SQLiteQuery(db, queryInsert); qr.Bind(testString); qr.Bind(testBlob); qr.Step(); qr.Release(); log += "\nInsert test string and blob."; // close db.Close(); // and open again db.Open(filename); // // set ENCRYPTION AGAIN, you could try change to see ENCRYPTION works // qr = new SQLiteQuery(db, "PRAGMA hexkey=\"0x0102030405060708090a0b0c0d0e0f10\";"); qr.Step(); qr.Release(); // do test Test2(db, ref log); } catch (Exception e) { log += "\nTest Fail with Exception " + e.ToString(); log += "\n on WebPlayer it must give an exception, it's normal."; } } #endif // // PlayerPrefs // if (GUI.Button(new Rect(170, 250, 150, 50), "Copy db to\n PlayerPrefs")) { StartCoroutine(CopyDB2PlayerPrefs()); } if (GUI.Button(new Rect(170, 310, 150, 50), "Run SQLite Test \nfor PlayerPrefs")) { db = new SQLiteDB(); MemoryStream memStream = new MemoryStream(); log = ""; string testdbz = PlayerPrefs.GetString("sqlitedb"); log += "Read Database from PlayerPrefs! : " + testdbz.Substring(0, 64) + "...\n"; if (testdbz.Length > 0) { for (int i = 0; i *2 < testdbz.Length; i++) { memStream.WriteByte(Convert.ToByte(testdbz.Substring(i * 2, 2), 16)); } try{ // // initialize database // db = new SQLiteDB(); db.OpenStream("stream3", memStream); log += "Database created from PlayerPrefs! named stream: stream3"; Test2(db, ref log); } catch (Exception e) { log += "\nTest Fail with Exception " + e.ToString(); log += "\n"; } } else { log += "Please push button above to put database into PlayerPrefs!\n"; } } } else { if (GUI.Button(new Rect(10, 10, 150, 50), "Back")) { db = null; } GUI.Label(new Rect(10, 70, 600, 600), log); } }
IEnumerator TestWithStreamingAssetsCopiedToPersistentDataPath() { db = new SQLiteDB(); log = ""; string dbfilename = "test.db"; byte[] bytes = null; #if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX string dbpath = "file://" + Application.streamingAssetsPath + "/" + dbfilename; log += "asset path is: " + dbpath; WWW www = new WWW(dbpath); yield return(www); bytes = www.bytes; #elif UNITY_WEBPLAYER string dbpath = "StreamingAssets/" + dbfilename; log += "asset path is: " + dbpath; WWW www = new WWW(dbpath); yield return(www); bytes = www.bytes; #elif UNITY_IPHONE yield return(0); string dbpath = Application.dataPath + "/Raw/" + dbfilename; log += "asset path is: " + dbpath; try{ using (FileStream fs = new FileStream(dbpath, FileMode.Open, FileAccess.Read, FileShare.Read)){ bytes = new byte[fs.Length]; fs.Read(bytes, 0, (int)fs.Length); } } catch (Exception e) { log += "\nTest Fail with Exception " + e.ToString(); log += "\n"; } #elif UNITY_ANDROID string dbpath = Application.streamingAssetsPath + "/" + dbfilename; log += "asset path is: " + dbpath; WWW www = new WWW(dbpath); yield return(www); bytes = www.bytes; #endif if (bytes != null) { try{ string filename = ExtensionMethods.AppWritableDirectory() + "/demo_from_streamingAssets.db"; // // // copy database to real file into cache folder using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write)) { fs.Write(bytes, 0, bytes.Length); log += "\nCopy database from streaminAssets to persistentDataPath: " + filename; } // // initialize database // db.Open(filename); log += "\nDatabase created! filename: " + filename; Test2(db, ref log); } catch (Exception e) { log += "\nTest Fail with Exception " + e.ToString(); log += "\n\n Did you copy test.db into StreamingAssets ?\n"; } } yield return(null); }
void Start() { FILE = ExtensionMethods.AppWritableDirectory() + "/demo4.db"; }