Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- using System;
- using System.IO;
- using System.Threading;
- using System.Diagnostics;
- public class MaHoaSQLiteScript : 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};
- Text View;
- // Use this for initialization
- void Start () {
- View = GameObject.Find ("View").GetComponent<Text>();
- }
- // Update is called once per frame
- void Update () {
- }
- void OnGUI(){
- //#if ENCRYPTION
- //
- // ENCRYPTION
- //
- if ( GUI.Button(new Rect (170,170,150,70), "Run SQLite\nfor ENCRYPTION scenario") )
- {
- print("Thieu Mao");
- try {
- db = new SQLiteDB();
- log = "";
- // a product persistant database path.
- string filename = Application.persistentDataPath + "/demo_as_sqlcipher.db";
- print("filename " + filename);
- View.text = filename;
- 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
- }
- void Test2( SQLiteDB db, ref string log )
- {
- SQLiteQuery qr;
- //
- // 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.";
- //
- // if we reach that point it's mean we pass the test!
- db.Close(); log += "\nDatabase closed!\nTest succeeded!";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment