Menu

[r40]: / trunk / WhatsappClient / Classes / SQLiteDatabase.cs  Maximize  Restore  History

Download this file

261 lines (246 with data), 9.0 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
/*
* SQL Lite Data Helper Class
* by Swen Kooij
* on 10/2/2012 (European Date Notation)
* Release under the GPL License
* See http://gpl.org for more information
*
* Mail adresses:
* ---------------------------------------------------------
* swenkooij@gmail.com
* 40004568@st.roca12.nl
* kirk@hackflag.org
* tha_kirk@icloud.com
* swen.kooij@facebook.com
* swen.kooij@apple.com
* swen.kooij@youtube.com
* swen.kooij@google.com
* ------------------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Windows.Forms;
/// <summary>
/// Vertegenwoordigd een SQL Lite Database
/// </summary>
public class SQLiteDatabase
{
String dbConnection;
/// <summary>
/// Single Param Constructor for specifying the DB file.
/// </summary>
/// <param name="inputFile">The File containing the DB</param>
public SQLiteDatabase(String inputFile)
{
dbConnection = String.Format("Data Source={0}", inputFile);
}
/// <summary>
/// Single Param Constructor for specifying advanced connection options.
/// </summary>
/// <param name="connectionOpts">A dictionary containing all desired options and their values</param>
public SQLiteDatabase(Dictionary<String, String> connectionOpts)
{
String str = "";
foreach (KeyValuePair<String, String> row in connectionOpts)
{
str += String.Format("{0}={1}; ", row.Key, row.Value);
}
str = str.Trim().Substring(0, str.Length - 1);
dbConnection = str;
}
/// <summary>
/// Allows the programmer to run a query against the Database.
/// </summary>
/// <param name="sql">The SQL to run</param>
/// <returns>A DataTable containing the result set.</returns>
public DataTable GetDataTable(string sql)
{
DataTable dt = new DataTable();
try
{
SQLiteConnection cnn = new SQLiteConnection(dbConnection);
cnn.Open();
SQLiteCommand mycommand = new SQLiteCommand(cnn);
mycommand.CommandText = sql;
SQLiteDataReader reader = mycommand.ExecuteReader();
dt.Load(reader);
reader.Close();
cnn.Close();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return dt;
}
/// <summary>
/// Allows the programmer to retrieve all contents in a table without a query
/// </summary>
/// <param name="tableName">The name of the table</param>
/// <param name="extraQuery">Add extra shizzle at the end of the SQL query.</param>
/// <returns>A DataTable containing the result</returns>
public DataTable GetTable(string tableName, string extraQuery)
{
DataTable dt = new DataTable();
try
{
SQLiteConnection cnn = new SQLiteConnection(dbConnection);
cnn.Open();
SQLiteCommand mycommand = new SQLiteCommand(cnn);
mycommand.CommandText = "SELECT * FROM " + tableName + " " + extraQuery;
SQLiteDataReader reader = mycommand.ExecuteReader();
dt.Load(reader);
reader.Close();
cnn.Close();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return dt;
}
/// <summary>
/// Retrieves all rows in a specified table
/// </summary>
/// <param name="tableName">The name of the table</param>
/// <param name="extraQuery">Add extra shizzle at the end of the SQL query.</param>
/// <returns>A DataRowCollection object</returns>
public DataRowCollection GetTableRows(string tableName, string extraQuery)
{
DataTable dt = new DataTable();
try
{
SQLiteConnection cnn = new SQLiteConnection(dbConnection);
cnn.Open();
SQLiteCommand mycommand = new SQLiteCommand(cnn);
mycommand.CommandText = "SELECT * FROM " + tableName + " " + extraQuery;
SQLiteDataReader reader = mycommand.ExecuteReader();
dt.Load(reader);
reader.Close();
cnn.Close();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return dt.Rows;
}
/// <summary>
/// Dumps the contents of a table in a string, used for debugging
/// </summary>
/// <param name="TableName">The name of the table</param>
/// <returns>The string</returns>
public string TableDump(string Query)
{
DataTable dt = new DataTable();
try
{
SQLiteConnection cnn = new SQLiteConnection(dbConnection);
cnn.Open();
SQLiteCommand mycommand = new SQLiteCommand(cnn);
mycommand.CommandText = Query;
SQLiteDataReader reader = mycommand.ExecuteReader();
dt.Load(reader);
reader.Close();
cnn.Close();
}
catch (Exception e)
{
string Excp = e.ToString();
Excp = Excp.Replace("System.Data.SQLite.SQLiteException: ", "");
Excp = Excp.Split(new[] { "at System.Data.SQLite.SQLite3.Prepare" }, StringSplitOptions.None)[0];
return Excp;
}
string Dump = "Table: " + dt.TableName + Environment.NewLine;
Dump += "Rows: " + dt.Rows.Count.ToString() + Environment.NewLine + Environment.NewLine;
DataColumnCollection Columns = dt.Columns;
for (int i = 0; i < dt.Rows.Count; i++)
{
string Column = Columns[i].ToString() + ": ";
DataRow Row = dt.Rows[i];
Dump += "Row " + i.ToString() + Environment.NewLine;
Dump += "------------------------------------------------------------------" + Environment.NewLine;
foreach (DataColumn Col in Columns)
{
Dump += "-- " + Col.ColumnName + " (" + Col.DataType.ToString().Replace("System.", "") + "): " + Row[Col.ColumnName] + Environment.NewLine;
}
Dump += Environment.NewLine;
}
return Dump;
}
/// <summary>
/// Allows the programmer to interact with the database for purposes other than a query.
/// </summary>
/// <param name="sql">The SQL to be run.</param>
/// <returns>An Integer containing the number of rows updated.</returns>
public string Query(string sql)
{
try
{
if (sql.StartsWith("SELECT"))
{
return TableDump(sql);
}
SQLiteConnection cnn = new SQLiteConnection(dbConnection);
cnn.Open();
SQLiteCommand mycommand = new SQLiteCommand(cnn);
mycommand.CommandText = sql;
int rowsUpdated = mycommand.ExecuteNonQuery();
cnn.Close();
return rowsUpdated.ToString();
}
catch (Exception E)
{
MessageBox.Show(E.ToString() + Environment.NewLine + Environment.NewLine + sql);
}
return "";
}
/// <summary>
/// Makes up an insert query
/// </summary>
/// <param name="Values">A dictionary of the values to insert</param>
/// <returns>The query result (Number of rows updated</returns>
public string Insert(string tableName, Dictionary<string, string> Values)
{
string Q = "INSERT INTO " + tableName + " (";
foreach (string Value in Values.Keys)
{
Q += Value + ",";
}
Q = Q.TrimEnd(',') + ") VALUES(";
foreach (string Value in Values.Values)
{
Q += "'" + Value + "',";
}
Q = Q.TrimEnd(',');
Q += ");";
return Query(Q);
}
/// <summary>
/// Retrieves all column names from a specified table.
/// </summary>
/// <param name="TableName">The table name.</param>
/// <returns>An string array of column names</returns>
public string[] GetColumns(string TableName)
{
DataTable Tb = GetTable(TableName, "");
List<string> Columns = new List<string>();
foreach (DataColumn Col in Tb.Columns)
{
Columns.Add(Col.ColumnName);
}
return Columns.ToArray();
}
/// <summary>
/// Retrieves the last added id in a table
/// </summary>
/// <param name="tableName">The name of the table you want to retrieve the latest id from</param>
/// <returns></returns>
public int RetrieveLastId(string tableName)
{
DataRow Row = GetTableRows(tableName, "ORDER BY id DESC LIMIT 0,1")[0];
return Convert.ToInt32(Row["id"]);
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.