-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
347 lines (292 loc) · 11.9 KB
/
Program.cs
File metadata and controls
347 lines (292 loc) · 11.9 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
using System;
using System.Data.SQLite;
using System.Collections.Generic;
public class DbInteraction
{
static void Main()
{
Coolio stuff = new Coolio();
string cs = stuff.cs;
// testing to get data from external file
// Console.WriteLine($"The connection string is {cs}.");
// System.Environment.Exit(0);
using var con = new SQLiteConnection(cs);
con.Open();
using var cmd = new SQLiteCommand(con);
// create user interaction here
string x = "\x0A";
var option = 10;
// create sample db
SampleDb(cs);
while (option != 0)
{
Print_New_Lines(3);
Console.WriteLine($@"Welcome to the Database tutorial! By running this program,
you are starting your first database. A database is a way to store data, into column
and row structures. This data can be accessed, changed, deleted, or displayed.{x}");
Console.WriteLine("For this tutorial, you will select an option listed below.\n");
Console.WriteLine($@"You will be able to:
1 - Look up data in a sample database
2 - Insert data into the existing database
3 - Create your own table and insert information (this will display all of your data afterwards{x}
0 - Will end the program{x}");
Console.Write("Which option will you chooose: ");
try
{
// did not know where to add first open paren, so did it here
option = Int16.Parse(Console.ReadLine());
}
catch (System.Exception)
{
// need to add single quotes for all string values
Console.WriteLine("That is an invalid entry. Please try again.\n\n");
option = 10;
}
if (option == 1)
{
// read data from db
ReadSample(cs);
}
else if (option == 2)
{
// insert data call
InsertData(cs);
}
else if (option == 3)
{
// create table call
CreateTable(cs);
}
}
}
/// <summary>Create sample db</summary>
public static void SampleDb(string cs)
{
using var con = new SQLiteConnection(cs);
con.Open();
using var cmd = new SQLiteCommand(con);
cmd.CommandText = "DROP TABLE IF EXISTS cars";
cmd.ExecuteNonQuery();
cmd.CommandText = @"CREATE TABLE cars(id INTEGER PRIMARY KEY,
name TEXT, price INT)";
cmd.ExecuteNonQuery();
// figuring out how to dynamically enter data
string[] table_info = { "Audi", "52642" };
cmd.CommandText = @"INSERT INTO cars(name, price)
VALUES
(@audi,52642)
, ('Mercedes',57127)
, ('Skoda',9000)
, ('Volvo',29000)
, ('Bentley',350000)
, ('Citroen',21000)
, ('Hummer',41400)
, ('Volkswagen',21600)";
cmd.Parameters.AddWithValue("@audi", table_info[0]);
cmd.ExecuteNonQuery();
}
/// <summary>Read the db and display contents</summary>
public static void ReadSample(string cs)
{
using var con = new SQLiteConnection(cs);
con.Open();
string stm = "SELECT * FROM cars";
using var cmd = new SQLiteCommand(stm, con);
using SQLiteDataReader rdr = cmd.ExecuteReader();
// outputs
Print_New_Lines(2);
Console.WriteLine($"{rdr.GetName(0),-3} {rdr.GetName(1),-12} {rdr.GetName(2),8}");
while (rdr.Read())
{
Console.WriteLine($@"{rdr.GetInt32(0),-3} {rdr.GetString(1),-12} {rdr.GetInt32(2),8}");
}
}
/// <summary>Read the db and display contents</summary>
public static void ReadTable(string cs)
{
using var con = new SQLiteConnection(cs);
con.Open();
Console.WriteLine("Which table would you like to read from?");
string table = Console.ReadLine();
string stm = "SELECT * FROM " + table;
using var cmd = new SQLiteCommand(stm, con);
using SQLiteDataReader rdr = cmd.ExecuteReader();
// outputs
Console.WriteLine($"{rdr.GetName(0),-3} {rdr.GetName(1),-12} {rdr.GetName(2),8}");
while (rdr.Read())
{
Console.WriteLine($@"{rdr.GetInt32(0),-3} {rdr.GetString(1),-12} {rdr.GetInt32(2),8}");
}
rdr.Close();
}
//how to create summary for c# functions
/// <summary>Void function that will input data into the tables</summary>
static void InsertData(string cs)
{
// insert into a table the user specifies
using var con = new SQLiteConnection(cs);
con.Open();
using var cmd = new SQLiteCommand(con);
Console.Write("What car name and price would you like to include? (write both words separated by a space) ");
string data = Console.ReadLine();
string[] info = data.Split(' ');
string insert = @"INSERT INTO cars(name, price) VALUES(@name, @price)";
cmd.Parameters.AddWithValue("@name", info[0]);
cmd.Parameters.AddWithValue("@price", info[1]);
cmd.CommandText = insert;
cmd.ExecuteNonQuery();
}
/// <summary>Void function that will create tables and their colummn names</summary>
static void CreateTable(string cs)
{
// insert into a table the user specifies
using var con = new SQLiteConnection(cs);
con.Open();
using var cmd = new SQLiteCommand(con);
Print_New_Lines(2);
Console.Write("What would you like to name your table? (If it already exists, it will be removed) ");
string table = Console.ReadLine();
// display example db
// ReadSample(cs);
// get user input for column names, id's auto generated
Console.WriteLine(@"Column names are how data is identified. They have data types, which specify
what data is going to be stored. Some data types include
INTEGER - Whole numbers like 1, 2, 999, or even 234
REAL - A number that has a decimal point
TEXT - Any data that are words, phrases or characters
NULL - Basically, this stores a 'nothing' value, but something is stored");
Console.Write(@"What data is going to be in the table? (Write the column names and data type separated by commas,
like this: name TEXT) ");
string columns = Console.ReadLine();
string[] col = columns.Split(',');
string insert = @"CREATE TABLE " + table + "(ID INTEGER PRIMARY KEY, ";
// to populate the data
// string dynamics = "@";
// how to dynamically add data to create table
// add to a string and set @data+toSTring(i)
for (int i = 0; i < col.Length; i++)
{
if (i == col.Length - 1) { insert += col[i] + ")"; }
else { insert += col[i] + ", "; }
}
Console.WriteLine(insert);
cmd.CommandText = "DROP TABLE IF EXISTS " + table;
cmd.ExecuteNonQuery();
cmd.CommandText = insert;
cmd.ExecuteNonQuery();
Console.WriteLine("Table was created!");
// sample db will be
// contact (table)
// include ID for all tables
// first_name - string
// city - string
// determine that data was input and is in table
Print_New_Lines(3);
string insertStatement = InsertUserTable(table, col);
Console.WriteLine(insertStatement);
// System.Environment.Exit(0);
cmd.CommandText = insertStatement;
cmd.ExecuteNonQuery();
// Console.WriteLine("Insert was successful!!!");
Print_New_Lines(2);
string stm = "SELECT * FROM " + table;
using var cmnd = new SQLiteCommand(stm, con);
using SQLiteDataReader rdr = cmnd.ExecuteReader();
// outputs
// + num is right align, - is left align
for (int i = 0; i <= col.Length; i++)
{
Console.Write($"{rdr.GetName(i),-12}");
}
Print_New_Lines(1);
// Console.WriteLine($"{rdr.GetName(0),-3} {rdr.GetName(1),-12} {rdr.GetName(2),8} {rdr.GetName(3),12}");
while (rdr.Read())
{
Console.Write($"{rdr.GetInt16(0),-12}");
for (int i = 1; i <= col.Length; i++)
{
Console.Write($"{rdr.GetString(i),-12}");
}
Print_New_Lines(1);
}
// System.Environment.Exit(0);
rdr.Close();
}
///<summary>
///Get column names for inserting into user's new table. Will format the input and make it into
/// correct sql format
///</summary>
public static string InsertUserTable(string table, string[] array)
{
// everything up to this point works. Need to parse object into arrays
// jacob salt lake city => 'jacob', 'salt lake city'
// string insert = @"INSERT INTO " + table + " VALUES";
string insert = @"INSERT INTO " + table + '(';
Console.Write("How many rows of data are you going to insert now? ");
int rows = Int16.Parse(Console.ReadLine());
// debugging purposes
// foreach (var item in array)
// {
// Console.WriteLine($"The row is {item}.");
// }
// create var to hold col names
string[] columns = new string[array.Length];
for (int i = 0; i < array.Length; i++)
{
// have to declare a array to hold array value,
// and populate that with value from original array
string value = array[i];
string[] holder = value.Split(' ');
columns[i] = holder[0];
if (i == array.Length - 1) { insert += holder[0] + ") VALUES"; }
else { insert += holder[0] + ','; }
// Console.Write($"{columns[i]} ");
}
Console.WriteLine($"Here is the insert statement: {insert}\n");
// how do I dynamically create this to allocate arrays to hold all of these values???
// for (int i = 0 ...) {}
// need to create new array, assign them name, and assign size
// create a dictionary and store values there???
Dictionary<int, string> dict = new Dictionary<int, string>();
string[] names = new string[rows];
string[] address = new string[rows];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns.Length; j++)
{
Console.Write($"What value will go into {columns[j]}: ");
string value = Console.ReadLine();
// would not let me check if key does not exist, so I had to do a try
// and if it fails, then I add values later
try
{
// did not know where to add first open paren, so did it here
dict.Add(i, "('" + value);
}
catch (System.Exception)
{
// need to add single quotes for all string values
dict[i] += "','" + value;
}
}
// close the () for the insert and add comma to it
if (i == rows - 1) { dict[i] += "')"; }
else { dict[i] += "'), "; }
}
foreach (KeyValuePair<int, string> ele1 in dict)
{
// Console.WriteLine("{0} and {1}", ele1.Key, ele1.Value);
insert += ele1.Value;
}
// Console.WriteLine(insert);
return insert;
}
///<summary>Print multiple new lines. Laziness so I don't have to write console... multiples times :)</summary>
static void Print_New_Lines(int lines)
{
for (int i = 0; i < lines; i++)
{
Console.Write("\n");
}
}
}