✅ Learning SQLite for C# Academy Project
The project is to build a habit tracker. I'm trying to get my Database file to be used universally throughout my code with a
Create if not exists
, SaveEntry
, and Update 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 newHabit
being passed to it, but when it tries to update, it just changes the Name from "Test" to 0. Thanks in advance.15 Replies
am I supposed to close the database somewhere, I'm missing doing that?
or do I need to find the current habit in the database, get it's ID, then update it?
you wrap the
SQLiteConnection
with a usingOverview - Microsoft.Data.Sqlite
An overview of Microsoft.Data.Sqlite
with that the connection gets disposed (and closed) automatically I assume
you can also use the
using
without braces and it gets automatically disposed at the end of scope (method)
and to make sure, how are you calling UpdateEntry
UPDATE habits SET Name=?, TrackType=?, Description=? WHERE Id=?
also the SaveEntry
has questionmarks as valueshow would I use the using statement without the braces?
this code is not permanent. I'm just trying to get the database file working properly first before I start building the actual code. This is how I'm currently calling those functions
do I have something wrong here?
the questionmarks?
is this wrong as well?
I've not done sqlite at all, but having ? as value placeholders sound odd
ok. I'll look more into it later today. I have to get to bed. Work in the AM : ( thanks for your help so far 🙂
https://pastebin.com/005wjqAF what do you think about this so far?
better, you could use instead of:
cmd.Parameters.Add(new SQLiteParameter(oldHabit.Name));
-> cmd.Parameters.AddWithValue("@CurrentName", oldHabit.Name);
UpdateEntry
has double SqlCommand
oh I see
I think you don't need the sqlite.Close();
on every method, as the using should dispose/close the connection
have you tried your solution?
as for the new style of usings:
makes it a tad cleanerhave you tried your solution?I'm actually finished with the project (in my opinion). https://github.com/mekasu0124/CodeReviews.Console.HabitTracker/tree/master/HabitTracker.mekasu0124 you're welcome to view it here. It's fully function and works as intended.
better, you could use instead of:you're not wrong here. I may make that change at a later date when I go back to this project to add other ways of tracking habits than just by count.cmd.Parameters.Add(new SQLiteParameter(oldHabit.Name));
->cmd.Parameters.AddWithValue("@CurrentName", oldHabit.Name);
as for the new style of usings:you're not wrong here. It would definitely achieve a better cleaner readability. I'll incorporate that at a later date as well. ty ❤️ I do appreciate all of your assistance 🙂 if you give my app a try, and come into any problems, you're welcome to dm me and let me know 🙂 /solved