-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitySQLiteExample.cs
More file actions
91 lines (66 loc) · 2.09 KB
/
Copy pathUnitySQLiteExample.cs
File metadata and controls
91 lines (66 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using SQLite4Unity3d;
public class test
{
public int test_id { get; set; }
public int test_seq { get; set; }
}
public class UnitySQLiteExample : MonoBehaviour
{
public Text text;
// Use this for initialization
void Start1()
{
}
// Update is called once per frame
void Update()
{
}
void Start()
{
var dbName = "sqlite3.sq3";
var dbPath = string.Empty;
var persistentDataPath = string.Format("{0}/{1}", Application.persistentDataPath, dbName);
#if UNITY_EDITOR
dbPath = string.Format(@"Assets/StreamingAssets/{0}", dbName);
#elif UNITY_IOS
if (File.Exists(persistentDataPath) == false)
{
var rawPath = Application.dataPath + "/Raw/" + dbName;
File.Copy(rawPath, persistentDataPath);
}
dbPath = persistentDataPath;
#elif UNITY_ANDROID
if (File.Exists(persistentDataPath) == false)
{
var androidPath = new WWW("jar:file://" + Application.dataPath + "!/assets/" + dbName);
// CAREFUL here, for safety reasons you shouldn't let this while loop unattended, place a timer and error check
while (!androidPath.isDone) { }
File.WriteAllBytes(persistentDataPath, androidPath.bytes);
}
dbPath = persistentDataPath;
#endif
var dbConn = new SQLiteConnection(dbPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
int affectedRows = 0;
//var cmdCreate = dbConn.CreateCommand("CREATE TABLE test (test_id INTEGER, test_seq INTEGER)");
//affectedRows = cmdCreate.ExecuteNonQuery();
//text.text = affectedRows.ToString();
var cmdInsert = dbConn.CreateCommand("INSERT INTO test VALUES(?, ?)", 1, 2);
affectedRows = cmdInsert.ExecuteNonQuery();
text.text = affectedRows.ToString();
var cmdSelect = dbConn.CreateCommand("SELECT test_id, test_seq FROM test");
var rows = cmdSelect.ExecuteQuery<test>();
Debug.Log(rows.Count);
foreach (var row in rows)
{
Debug.Log(row.test_id);
Debug.Log(row.test_seq);
}
dbConn.Close();
dbConn.Dispose();
Debug.Log("OK");
text.text = "OK";
}
}