|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using UnityEngine; |
| 4 | + |
| 5 | +//References |
| 6 | +using Mono.Data.Sqlite; |
| 7 | +using System; |
| 8 | +using System.Data; |
| 9 | +using System.IO; |
| 10 | +using UnityEngine.UI; |
| 11 | + |
| 12 | +public class Unity : MonoBehaviour |
| 13 | +{ |
| 14 | + private string conn, sqlQuery; |
| 15 | + IDbConnection dbconn; |
| 16 | + IDbCommand dbcmd; |
| 17 | + private IDataReader reader; |
| 18 | + public InputField t_name, t_Address, t_id; |
| 19 | + public Text data_staff; |
| 20 | + |
| 21 | + string DatabaseName = "Employers.s3db"; |
| 22 | + |
| 23 | + void Start() |
| 24 | + { |
| 25 | + |
| 26 | + string filepath = Application.dataPath + "/Plugins/" + DatabaseName; |
| 27 | + |
| 28 | + //open db connection |
| 29 | + conn = "URI=file:" + filepath; |
| 30 | + |
| 31 | + Debug.Log("Stablishing connection to: " + conn); |
| 32 | + dbconn = new SqliteConnection(conn); |
| 33 | + dbconn.Open(); |
| 34 | + |
| 35 | + // reader_function(); |
| 36 | + } |
| 37 | + //Insert |
| 38 | + public void insert_button() |
| 39 | + { |
| 40 | + insert_function(t_name.text, t_Address.text); |
| 41 | + |
| 42 | + } |
| 43 | + //Search |
| 44 | + public void Search_button() |
| 45 | + { |
| 46 | + data_staff.text = ""; |
| 47 | + Search_function(t_id.text); |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + //Found to Update |
| 52 | + public void F_to_update_button() |
| 53 | + { |
| 54 | + data_staff.text = ""; |
| 55 | + F_to_update_function(t_id.text); |
| 56 | + |
| 57 | + } |
| 58 | + //Update |
| 59 | + public void Update_button() |
| 60 | + { |
| 61 | + update_function(t_id.text, t_name.text, t_Address.text); |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + //Delete |
| 66 | + public void Delete_button() |
| 67 | + { |
| 68 | + data_staff.text = ""; |
| 69 | + Delete_function(t_id.text); |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + //Insert To Database |
| 74 | + private void insert_function(string name, string Address) |
| 75 | + { |
| 76 | + using (dbconn = new SqliteConnection(conn)) |
| 77 | + { |
| 78 | + dbconn.Open(); //Open connection to the database. |
| 79 | + dbcmd = dbconn.CreateCommand(); |
| 80 | + sqlQuery = string.Format("insert into Staff (name, Address) values (\"{0}\",\"{1}\")", name, Address);// table name |
| 81 | + dbcmd.CommandText = sqlQuery; |
| 82 | + dbcmd.ExecuteScalar(); |
| 83 | + dbconn.Close(); |
| 84 | + } |
| 85 | + data_staff.text = ""; |
| 86 | + Debug.Log("Insert Done "); |
| 87 | + |
| 88 | + reader_function(); |
| 89 | + } |
| 90 | + //Read All Data For To Database |
| 91 | + private void reader_function() |
| 92 | + { |
| 93 | + // int idreaders ; |
| 94 | + string Namereaders, Addressreaders; |
| 95 | + using (dbconn = new SqliteConnection(conn)) |
| 96 | + { |
| 97 | + dbconn.Open(); //Open connection to the database. |
| 98 | + IDbCommand dbcmd = dbconn.CreateCommand(); |
| 99 | + string sqlQuery = "SELECT Name, Address " + "FROM Staff";// table name |
| 100 | + dbcmd.CommandText = sqlQuery; |
| 101 | + IDataReader reader = dbcmd.ExecuteReader(); |
| 102 | + while (reader.Read()) |
| 103 | + { |
| 104 | + // idreaders = reader.GetString(1); |
| 105 | + Namereaders = reader.GetString(0); |
| 106 | + Addressreaders = reader.GetString(1); |
| 107 | + |
| 108 | + data_staff.text += Namereaders + Addressreaders + "\n"; |
| 109 | + Debug.Log(" name =" + Namereaders + "Address=" + Addressreaders); |
| 110 | + } |
| 111 | + reader.Close(); |
| 112 | + reader = null; |
| 113 | + dbcmd.Dispose(); |
| 114 | + dbcmd = null; |
| 115 | + dbconn.Close(); |
| 116 | + // dbconn = null; |
| 117 | + |
| 118 | + } |
| 119 | + } |
| 120 | + //Search on Database by ID |
| 121 | + private void Search_function(string Search_by_id) |
| 122 | + { |
| 123 | + using (dbconn = new SqliteConnection(conn)) |
| 124 | + { |
| 125 | + string Name_readers_Search, Address_readers_Search; |
| 126 | + dbconn.Open(); //Open connection to the database. |
| 127 | + IDbCommand dbcmd = dbconn.CreateCommand(); |
| 128 | + string sqlQuery = "SELECT name,address " + "FROM Staff where id =" + Search_by_id;// table name |
| 129 | + dbcmd.CommandText = sqlQuery; |
| 130 | + IDataReader reader = dbcmd.ExecuteReader(); |
| 131 | + while (reader.Read()) |
| 132 | + { |
| 133 | + // string id = reader.GetString(0); |
| 134 | + Name_readers_Search = reader.GetString(0); |
| 135 | + Address_readers_Search = reader.GetString(1); |
| 136 | + data_staff.text += Name_readers_Search + " - " + Address_readers_Search + "\n"; |
| 137 | + |
| 138 | + Debug.Log(" name =" + Name_readers_Search + "Address=" + Address_readers_Search); |
| 139 | + |
| 140 | + } |
| 141 | + reader.Close(); |
| 142 | + reader = null; |
| 143 | + dbcmd.Dispose(); |
| 144 | + dbcmd = null; |
| 145 | + dbconn.Close(); |
| 146 | + |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | + //Search on Database by ID |
| 154 | + private void F_to_update_function(string Search_by_id) |
| 155 | + { |
| 156 | + using (dbconn = new SqliteConnection(conn)) |
| 157 | + { |
| 158 | + string Name_readers_Search, Address_readers_Search; |
| 159 | + dbconn.Open(); //Open connection to the database. |
| 160 | + IDbCommand dbcmd = dbconn.CreateCommand(); |
| 161 | + string sqlQuery = "SELECT name,address " + "FROM Staff where id =" + Search_by_id;// table name |
| 162 | + dbcmd.CommandText = sqlQuery; |
| 163 | + IDataReader reader = dbcmd.ExecuteReader(); |
| 164 | + while (reader.Read()) |
| 165 | + { |
| 166 | + |
| 167 | + Name_readers_Search = reader.GetString(0); |
| 168 | + Address_readers_Search = reader.GetString(1); |
| 169 | + t_name.text = Name_readers_Search; |
| 170 | + t_Address.text = Address_readers_Search; |
| 171 | + |
| 172 | + } |
| 173 | + reader.Close(); |
| 174 | + reader = null; |
| 175 | + dbcmd.Dispose(); |
| 176 | + dbcmd = null; |
| 177 | + dbconn.Close(); |
| 178 | + |
| 179 | + |
| 180 | + } |
| 181 | + |
| 182 | + } |
| 183 | + //Update on Database |
| 184 | + private void update_function(string update_id, string update_name, string update_address) |
| 185 | + { |
| 186 | + using (dbconn = new SqliteConnection(conn)) |
| 187 | + { |
| 188 | + dbconn.Open(); //Open connection to the database. |
| 189 | + dbcmd = dbconn.CreateCommand(); |
| 190 | + sqlQuery = string.Format("UPDATE Staff set name = @name ,address = @address where ID = @id "); |
| 191 | + |
| 192 | + SqliteParameter P_update_name = new SqliteParameter("@name", update_name); |
| 193 | + SqliteParameter P_update_address = new SqliteParameter("@address", update_address); |
| 194 | + SqliteParameter P_update_id = new SqliteParameter("@id", update_id); |
| 195 | + |
| 196 | + dbcmd.Parameters.Add(P_update_name); |
| 197 | + dbcmd.Parameters.Add(P_update_address); |
| 198 | + dbcmd.Parameters.Add(P_update_id); |
| 199 | + |
| 200 | + dbcmd.CommandText = sqlQuery; |
| 201 | + dbcmd.ExecuteScalar(); |
| 202 | + dbconn.Close(); |
| 203 | + Search_function(t_id.text); |
| 204 | + } |
| 205 | + |
| 206 | + // SceneManager.LoadScene("home"); |
| 207 | + } |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | + //Delete |
| 212 | + private void Delete_function(string Delete_by_id) |
| 213 | + { |
| 214 | + using (dbconn = new SqliteConnection(conn)) |
| 215 | + { |
| 216 | + |
| 217 | + dbconn.Open(); //Open connection to the database. |
| 218 | + IDbCommand dbcmd = dbconn.CreateCommand(); |
| 219 | + string sqlQuery = "DELETE FROM Staff where id =" + Delete_by_id;// table name |
| 220 | + dbcmd.CommandText = sqlQuery; |
| 221 | + IDataReader reader = dbcmd.ExecuteReader(); |
| 222 | + |
| 223 | + |
| 224 | + dbcmd.Dispose(); |
| 225 | + dbcmd = null; |
| 226 | + dbconn.Close(); |
| 227 | + data_staff.text = Delete_by_id + " Delete Done "; |
| 228 | + |
| 229 | + } |
| 230 | + |
| 231 | + } |
| 232 | + // Update is called once per frame |
| 233 | + void Update() |
| 234 | + { |
| 235 | + |
| 236 | + } |
| 237 | +} |
0 commit comments