-
Notifications
You must be signed in to change notification settings - Fork 14
/
UnitySqlite
237 lines (193 loc) · 6.84 KB
/
UnitySqlite
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//References
using Mono.Data.Sqlite;
using System;
using System.Data;
using System.IO;
using UnityEngine.UI;
public class Unity : MonoBehaviour
{
private string conn, sqlQuery;
IDbConnection dbconn;
IDbCommand dbcmd;
private IDataReader reader;
public InputField t_name, t_Address, t_id;
public Text data_staff;
string DatabaseName = "Employers.s3db";
void Start()
{
string filepath = Application.dataPath + "/Plugins/" + DatabaseName;
//open db connection
conn = "URI=file:" + filepath;
Debug.Log("Stablishing connection to: " + conn);
dbconn = new SqliteConnection(conn);
dbconn.Open();
// reader_function();
}
//Insert
public void insert_button()
{
insert_function(t_name.text, t_Address.text);
}
//Search
public void Search_button()
{
data_staff.text = "";
Search_function(t_id.text);
}
//Found to Update
public void F_to_update_button()
{
data_staff.text = "";
F_to_update_function(t_id.text);
}
//Update
public void Update_button()
{
update_function(t_id.text, t_name.text, t_Address.text);
}
//Delete
public void Delete_button()
{
data_staff.text = "";
Delete_function(t_id.text);
}
//Insert To Database
private void insert_function(string name, string Address)
{
using (dbconn = new SqliteConnection(conn))
{
dbconn.Open(); //Open connection to the database.
dbcmd = dbconn.CreateCommand();
sqlQuery = string.Format("insert into Staff (name, Address) values (\"{0}\",\"{1}\")", name, Address);// table name
dbcmd.CommandText = sqlQuery;
dbcmd.ExecuteScalar();
dbconn.Close();
}
data_staff.text = "";
Debug.Log("Insert Done ");
reader_function();
}
//Read All Data For To Database
private void reader_function()
{
// int idreaders ;
string Namereaders, Addressreaders;
using (dbconn = new SqliteConnection(conn))
{
dbconn.Open(); //Open connection to the database.
IDbCommand dbcmd = dbconn.CreateCommand();
string sqlQuery = "SELECT Name, Address " + "FROM Staff";// table name
dbcmd.CommandText = sqlQuery;
IDataReader reader = dbcmd.ExecuteReader();
while (reader.Read())
{
// idreaders = reader.GetString(1);
Namereaders = reader.GetString(0);
Addressreaders = reader.GetString(1);
data_staff.text += Namereaders + Addressreaders + "\n";
Debug.Log(" name =" + Namereaders + "Address=" + Addressreaders);
}
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbconn.Close();
// dbconn = null;
}
}
//Search on Database by ID
private void Search_function(string Search_by_id)
{
using (dbconn = new SqliteConnection(conn))
{
string Name_readers_Search, Address_readers_Search;
dbconn.Open(); //Open connection to the database.
IDbCommand dbcmd = dbconn.CreateCommand();
string sqlQuery = "SELECT name,address " + "FROM Staff where id =" + Search_by_id;// table name
dbcmd.CommandText = sqlQuery;
IDataReader reader = dbcmd.ExecuteReader();
while (reader.Read())
{
// string id = reader.GetString(0);
Name_readers_Search = reader.GetString(0);
Address_readers_Search = reader.GetString(1);
data_staff.text += Name_readers_Search + " - " + Address_readers_Search + "\n";
Debug.Log(" name =" + Name_readers_Search + "Address=" + Address_readers_Search);
}
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbconn.Close();
}
}
//Search on Database by ID
private void F_to_update_function(string Search_by_id)
{
using (dbconn = new SqliteConnection(conn))
{
string Name_readers_Search, Address_readers_Search;
dbconn.Open(); //Open connection to the database.
IDbCommand dbcmd = dbconn.CreateCommand();
string sqlQuery = "SELECT name,address " + "FROM Staff where id =" + Search_by_id;// table name
dbcmd.CommandText = sqlQuery;
IDataReader reader = dbcmd.ExecuteReader();
while (reader.Read())
{
Name_readers_Search = reader.GetString(0);
Address_readers_Search = reader.GetString(1);
t_name.text = Name_readers_Search;
t_Address.text = Address_readers_Search;
}
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbconn.Close();
}
}
//Update on Database
private void update_function(string update_id, string update_name, string update_address)
{
using (dbconn = new SqliteConnection(conn))
{
dbconn.Open(); //Open connection to the database.
dbcmd = dbconn.CreateCommand();
sqlQuery = string.Format("UPDATE Staff set name = @name ,address = @address where ID = @id ");
SqliteParameter P_update_name = new SqliteParameter("@name", update_name);
SqliteParameter P_update_address = new SqliteParameter("@address", update_address);
SqliteParameter P_update_id = new SqliteParameter("@id", update_id);
dbcmd.Parameters.Add(P_update_name);
dbcmd.Parameters.Add(P_update_address);
dbcmd.Parameters.Add(P_update_id);
dbcmd.CommandText = sqlQuery;
dbcmd.ExecuteScalar();
dbconn.Close();
Search_function(t_id.text);
}
// SceneManager.LoadScene("home");
}
//Delete
private void Delete_function(string Delete_by_id)
{
using (dbconn = new SqliteConnection(conn))
{
dbconn.Open(); //Open connection to the database.
IDbCommand dbcmd = dbconn.CreateCommand();
string sqlQuery = "DELETE FROM Staff where id =" + Delete_by_id;// table name
dbcmd.CommandText = sqlQuery;
IDataReader reader = dbcmd.ExecuteReader();
dbcmd.Dispose();
dbcmd = null;
dbconn.Close();
data_staff.text = Delete_by_id + " Delete Done ";
}
}
// Update is called once per frame
void Update()
{
}
}