public class Database
{
private static readonly string dbFile = "habits.db";
public static bool CreateDatabase()
{
SQLiteConnection.CreateFile(dbFile);
var sqlite = new SQLiteConnection("Data Source=" + dbFile);
sqlite.Open();
string sql = @"
CREATE TABLE IF NOT EXISTS habits(
Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
Name TEXT,
TrackType TEXT,
Description TEXT)
";
SQLiteCommand command = new SQLiteCommand(sql, sqlite);
try
{
command.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
public static bool SaveEntry(Habit newHabit)
{
var sqlite = new SQLiteConnection("Data Source=" + dbFile);
sqlite.Open();
SQLiteCommand insertSQL = new SQLiteCommand(
"INSERT INTO habits(Name, TrackType, Description) VALUES (?,?,?)",
sqlite
);
insertSQL.Parameters.AddWithValue("Name", newHabit.Name);
insertSQL.Parameters.AddWithValue("TrackType", newHabit.TrackType);
insertSQL.Parameters.AddWithValue("Description", newHabit.Description);
try
{
insertSQL.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
public static bool UpdateEntry(Habit newHabit, int num)
{
var sqlite = new SQLiteConnection("Data Source=" + dbFile);
sqlite.Open();
SQLiteCommand updateSQL = new SQLiteCommand(
"UPDATE habits SET Name=?, TrackType=?, Description=? WHERE Id=?",
sqlite
);
updateSQL.Parameters.AddWithValue("Id", num);
updateSQL.Parameters.AddWithValue("Name", newHabit.Name);
updateSQL.Parameters.AddWithValue("TrackType", newHabit.TrackType);
updateSQL.Parameters.AddWithValue("Description", newHabit.Description);
try
{
updateSQL.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
}
public class Database
{
private static readonly string dbFile = "habits.db";
public static bool CreateDatabase()
{
SQLiteConnection.CreateFile(dbFile);
var sqlite = new SQLiteConnection("Data Source=" + dbFile);
sqlite.Open();
string sql = @"
CREATE TABLE IF NOT EXISTS habits(
Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
Name TEXT,
TrackType TEXT,
Description TEXT)
";
SQLiteCommand command = new SQLiteCommand(sql, sqlite);
try
{
command.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
public static bool SaveEntry(Habit newHabit)
{
var sqlite = new SQLiteConnection("Data Source=" + dbFile);
sqlite.Open();
SQLiteCommand insertSQL = new SQLiteCommand(
"INSERT INTO habits(Name, TrackType, Description) VALUES (?,?,?)",
sqlite
);
insertSQL.Parameters.AddWithValue("Name", newHabit.Name);
insertSQL.Parameters.AddWithValue("TrackType", newHabit.TrackType);
insertSQL.Parameters.AddWithValue("Description", newHabit.Description);
try
{
insertSQL.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
public static bool UpdateEntry(Habit newHabit, int num)
{
var sqlite = new SQLiteConnection("Data Source=" + dbFile);
sqlite.Open();
SQLiteCommand updateSQL = new SQLiteCommand(
"UPDATE habits SET Name=?, TrackType=?, Description=? WHERE Id=?",
sqlite
);
updateSQL.Parameters.AddWithValue("Id", num);
updateSQL.Parameters.AddWithValue("Name", newHabit.Name);
updateSQL.Parameters.AddWithValue("TrackType", newHabit.TrackType);
updateSQL.Parameters.AddWithValue("Description", newHabit.Description);
try
{
updateSQL.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
}