Created
April 14, 2014 02:55
-
-
Save masayuki5160/10612626 to your computer and use it in GitHub Desktop.
SQLiteKit usage example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| using System; | |
| using System.IO; | |
| using System.Text; | |
| using System.Threading; | |
| using System.Diagnostics; | |
| public class FirstSQLite : MonoBehaviour { | |
| private SQLiteDB db = null; | |
| private string log; | |
| private string queryDelete = "DROP TABLE IF EXISTS test_values;"; | |
| private string queryCreate = "CREATE TABLE IF NOT EXISTS test_values (id INTEGER PRIMARY KEY, str_field TEXT, blob_field BLOB);"; | |
| private string queryInsert = "INSERT INTO test_values (str_field,blob_field) VALUES(?,?);"; | |
| private string querySelect = "SELECT * FROM test_values;"; | |
| private string testString = "1231 \n\r \t weqw"; | |
| private byte[] testBlob = new byte[] {2,3,5,78,98,21,32,255}; // Binary Large OBject | |
| // Use this for initialization | |
| void Start () { | |
| db = new SQLiteDB(); | |
| string filename = Application.persistentDataPath + "/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."; | |
| } | |
| } | |
| void Test( SQLiteDB db, ref string log ) | |
| { | |
| SQLiteQuery qr; | |
| // | |
| // delete table if exists | |
| // | |
| qr = new SQLiteQuery(db, queryDelete); | |
| qr.Step(); | |
| qr.Release(); | |
| log += "\nTable deleted."; | |
| // | |
| // 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."; | |
| // | |
| // read strings | |
| // | |
| string testStringFromSelect = ""; | |
| qr = new SQLiteQuery(db, querySelect); | |
| while( qr.Step() ) | |
| { | |
| testStringFromSelect = qr.GetString("str_field"); | |
| if( testStringFromSelect != testString ) | |
| { | |
| throw new Exception( "Test string are not equal!" ); | |
| } | |
| byte[] testBlobFromSelect = qr.GetBlob("blob_field"); | |
| if( testBlobFromSelect.Length != testBlob.Length ) | |
| { | |
| throw new Exception( "Test blobs are not equal!" ); | |
| } | |
| for (int i = 0; i < testBlobFromSelect.Length; i++) | |
| { | |
| if( testBlobFromSelect[i] != testBlob[i] ) | |
| { | |
| throw new Exception( "Test blobs are not equal!" ); | |
| } | |
| } | |
| } | |
| if( testStringFromSelect == "" ) | |
| { | |
| throw new Exception( "Unknowm problem!" ); | |
| } | |
| qr.Release(); | |
| log += "\nRead and test strings and blobs."; | |
| // | |
| // | |
| // delete table | |
| // | |
| qr = new SQLiteQuery(db, queryDelete); | |
| qr.Step(); | |
| qr.Release(); | |
| log += "\nTable deleted."; | |
| // | |
| // if we reach that point it's mean we pass the test! | |
| db.Close(); | |
| log += "\nDatabase closed!\nTest succeeded!"; | |
| UnityEngine.Debug.Log(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 | |
| 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 = Application.persistentDataPath + "/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"; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment