· 6 years ago · Jun 17, 2019, 02:06 AM
1using UnityEngine;
2using System.Data;
3using Mono.Data.Sqlite;
4using System.IO;
5
6public class DBstartup : MonoBehaviour
7
8
9{ //database name
10 public const string dbnaam = "landstedeClickerr2";
11
12 //dbconnection
13 public string connection;
14
15 // Use this for initialization
16 void Start()
17 {
18
19 //dbconnection
20 connection = "URI=file:" + Application.persistentDataPath + "/" + dbnaam;
21
22
23 //log for file check
24 Debug.Log(File.Exists(Application.persistentDataPath + "/" + dbnaam) ? "jaaaa" : "neeej");
25
26 // check if database is already created or not
27 if (File.Exists(Application.persistentDataPath + "/" + dbnaam))
28 {
29 Debug.Log("jaa");
30 } else
31 {
32 // Create database
33
34
35 // Open connection
36 IDbConnection dbcon = new SqliteConnection(connection);
37 dbcon.Open();
38
39
40
41
42 // Create table users
43 IDbCommand user;
44 user = dbcon.CreateCommand();
45 string q_createTable_user = "CREATE TABLE IF NOT EXISTS user (id INT PRIMARY KEY," +
46 "totalclicks INT, " +
47 "lifetimepoints BIGINT, " +
48 "startdate DATE, " +
49 "prestige INT, " +
50 "user_theme INT, " +
51 "user_sound BOOLEAN, " +
52 "user_effect BOOLEAN)";
53
54 user.CommandText = q_createTable_user;
55 user.ExecuteReader();
56
57 // Create table achievements
58 IDbCommand achieve;
59 achieve = dbcon.CreateCommand();
60 string q_createTable_achievements = "CREATE TABLE IF NOT EXISTS achievements (id INTEGER PRIMARY KEY AUTOINCREMENT," +
61 "name VARCHAR, " +
62 "image INT, " +
63 "description VARCHAR," +
64 "unlocked BOOLEAN)";
65
66 achieve.CommandText = q_createTable_achievements;
67 achieve.ExecuteReader();
68
69 // Create table upgrades {pps = points per second}
70 IDbCommand upgrade;
71 upgrade = dbcon.CreateCommand();
72 string q_createTable_upgrade = "CREATE TABLE IF NOT EXISTS upgrades (id INTEGER PRIMARY KEY AUTOINCREMENT," +
73 "name VARCHAR, " +
74 "image INT, " +
75 "price BIGINT," +
76 "pps BIGINT," +
77 "amount INT)";
78
79 upgrade.CommandText = q_createTable_upgrade;
80 upgrade.ExecuteReader();
81
82 // Create table theme
83 IDbCommand theme;
84 theme = dbcon.CreateCommand();
85 string q_createTable_theme = "CREATE TABLE IF NOT EXISTS theme (id INTEGER PRIMARY KEY AUTOINCREMENT," +
86 "name VARCHAR, " +
87 "image INT, " +
88 "price BIGINT," +
89 "unlocked BOOLEAN)";
90
91 theme.CommandText = q_createTable_theme;
92 theme.ExecuteReader();
93
94 // Close connection
95
96 dbcon.Close();
97
98
99 //Insert values in table
100
101 Insert_user();
102
103 Insert_upgrades();
104
105
106 }
107
108
109 }
110
111 // Update is called once per frame
112 void Update()
113 {
114
115 }
116
117 //Insert values in tabbles
118 // start with user -> theme -> upgrades -> achieve
119
120 //default info for the user
121 private void Insert_user()
122 {
123
124
125 IDbConnection dbcon = new SqliteConnection(connection);
126 dbcon.Open();
127
128 IDbCommand cmd;
129 cmd = dbcon.CreateCommand();
130 cmd.CommandText = "INSERT INTO user (id , totalclicks, lifetimepoints, " +
131 "startdate, prestige, user_theme, " +
132 "user_sound, user_effect)" +
133 " VALUES (1, 0, 0, CURRENT_TIMESTAMP, 0, 1, 1, 0)";
134 cmd.ExecuteNonQuery();
135
136 // Close connection
137
138 dbcon.Close();
139 }
140
141
142
143
144 //default upgrades
145 private void Insert_upgrades()
146 {
147
148
149 IDbConnection dbcon = new SqliteConnection(connection);
150 dbcon.Open();
151
152 IDbCommand cmd;
153 cmd = dbcon.CreateCommand();
154 cmd.CommandText = "INSERT INTO upgrades (id , name, image, " +
155 "price, pps, amount) " +
156 " VALUES (1, test, 0, 5, 10, 0)";
157
158 cmd.ExecuteNonQuery();
159
160
161
162 // Close connection
163
164 dbcon.Close();
165
166 }
167
168 // uitleg voor data insert
169 // IDbCommand cmnd = dbcon.CreateCommand();
170 // cmnd.CommandText = "INSERT INTO koekje (id, val) VALUES (7, 225)";
171 // cmnd.ExecuteNonQuery();
172
173
174
175
176}