✅ Learning SQLite for C# Academy Project
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;
}
}
}Create if not existsCreate if not exists, SaveEntrySaveEntry, and Update EntryUpdate Entry functions. So far, everything but my UpdateEntry function works and I can't figure out why. I have tried various things from stack overflow and I'm not quite understanding. I have a good knowledge point on SQLite3 from Python, but it's a tad different with c#. When I put in break points in my test code, the UpdateEntry function gets the correct values from the newHabitnewHabit being passed to it, but when it tries to update, it just changes the Name from "Test" to 0. Thanks in advance.