Created
February 18, 2019 09:51
-
-
Save thebitbrine/6520c71e0444663c6691e311be411714 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IDatabase | |
{ | |
public DataTable ReadTable(SQLiteConnection Connection, string TableName) | |
{ | |
List<string> Data = new List<string>(); | |
string Command = $"SELECT * FROM {TableName}"; | |
SQLiteCommand SqliteCommand = new SQLiteCommand(Command, Connection); | |
SQLiteDataAdapter da = new SQLiteDataAdapter(SqliteCommand); | |
DataTable dt = new DataTable(); | |
da.Fill(dt); | |
return dt; | |
} | |
public SQLiteConnection ConnectToDatabase(string FullPath) | |
{ | |
SQLiteConnection Connection = new SQLiteConnection("Data Source=" + FullPath + ";Version=3;"); | |
Connection = Connection.OpenAndReturn(); | |
return Connection; | |
} | |
public int SendCommand(SQLiteConnection Connection, string Command) | |
{ | |
SQLiteCommand SqliteCommand = new SQLiteCommand(Command, Connection); | |
return SqliteCommand.ExecuteNonQuery(); | |
} | |
public bool DataExists(SQLiteConnection Connection, string TableName, string ColumnName, string Value) | |
{ | |
string Command = "SELECT count(*) FROM " + TableName + " WHERE word=" + Value; | |
SQLiteCommand SqliteCommand = new SQLiteCommand(Command, Connection); | |
object Result = SqliteCommand.ExecuteScalar(); | |
if (Result == null) | |
return false; | |
else | |
return true; | |
} | |
public List<string> ReadData(SQLiteConnection Connection, string Selection, string TableName, string Field) | |
{ | |
List<string> Data = new List<string>(); | |
string Command = $"SELECT {Selection} FROM {TableName}"; | |
SQLiteCommand SqliteCommand = new SQLiteCommand(Command, Connection); | |
var Reader = SqliteCommand.ExecuteReader(); | |
while (Reader.Read()) | |
{ | |
Data.Add(Reader[Field].ToString()); | |
} | |
return Data; | |
} | |
public List<KeyValuePair<string, string>> ReadDataJoinedKVP(SQLiteConnection Connection, string Selection, string TableName, string Field_1, string Field_2) | |
{ | |
var Data = new List<KeyValuePair<string, string>>(); | |
string Command = $"SELECT {Selection} FROM {TableName}"; | |
SQLiteCommand SqliteCommand = new SQLiteCommand(Command, Connection); | |
var Reader = SqliteCommand.ExecuteReader(); | |
while (Reader.Read()) | |
{ | |
Data.Add(new KeyValuePair<string, string>(Reader[Field_1].ToString(), Reader[Field_2].ToString())); | |
} | |
return Data; | |
} | |
public List<string> ReadDataJoined(SQLiteConnection Connection, string Selection, string TableName, string Field_1, string Field_2) | |
{ | |
var Data = new List<string>(); | |
string Command = $"SELECT {Selection} FROM {TableName}"; | |
SQLiteCommand SqliteCommand = new SQLiteCommand(Command, Connection); | |
var Reader = SqliteCommand.ExecuteReader(); | |
while (Reader.Read()) | |
{ | |
Data.Add($"{Reader[Field_1].ToString()} {Reader[Field_2].ToString()}"); | |
} | |
return Data; | |
} | |
public int InsertData(SQLiteConnection Connection, string TableName, string ColumnNames, string Values) | |
{ | |
Values = Values.Replace(", ", ","); | |
string[] ValueArray = Values.Split(','); | |
Values = ""; | |
for (int i = 0; i <= ValueArray.Length - 1; i++) | |
{ | |
int IntValue = 0; | |
if (int.TryParse(ValueArray[i], out IntValue) == false) | |
{ | |
if (i != ValueArray.Length - 1) | |
Values += "\"" + ValueArray[i] + "\", "; | |
else | |
Values += "\"" + ValueArray[i] + "\""; | |
} | |
else | |
{ | |
if (i != ValueArray.Length - 1) | |
Values += ValueArray[i] + ", "; | |
else | |
Values += ValueArray[i]; | |
} | |
} | |
string Command = "insert into " + TableName + " (" + TableName + ") values (" + Values + ")"; | |
SQLiteCommand SqliteCommand = new SQLiteCommand(Command, Connection); | |
return SqliteCommand.ExecuteNonQuery(); | |
} | |
public int CreateTable(SQLiteConnection Connection, string TableName, string Columns) | |
{ | |
string Command = "CREATE TABLE " + TableName + " (" + Columns + ")"; | |
SQLiteCommand SqliteCommand = new SQLiteCommand(Command, Connection); | |
return SqliteCommand.ExecuteNonQuery(); | |
} | |
public bool CreateDatabase(string Path, string FileName) | |
{ | |
if (FileName.EndsWith(".sqlite") == false) | |
FileName += ".sqlite"; | |
if (Path.EndsWith("\\") == false) | |
Path += "\\"; | |
SQLiteConnection.CreateFile(System.IO.Path.Combine(Path, FileName)); | |
if (System.IO.File.Exists(System.IO.Path.Combine(Path, FileName)) == true) | |
return true; | |
else | |
return false; | |
} | |
public bool CheckDatabase(string Path) | |
{ | |
if (System.IO.File.Exists(Path) == true) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment