The next project of the C# Academy is creating a habit tracker. I'm a little confused on the SQLite aspect of it as I can't seem to get the statement right. I've watched a basic tutorial video https://www.youtube.com/watch?v=oeuTw00F1as&t=725s and I'm using various stack overflows (that make sense) to get my sql working. I have some SQLite knowledge from working with Python, it just seems to be a smidge different.
private static readonly string dbFile = "habits.db";public static void CreateDatabase(){ SQLiteConnection.CreateFile(dbFile); var sqlite = new SQLConnection("Data Source=" + dbFile); sqlite.Open(); string sql = @"CREATE TABLE IF NOT EXISTS habits( Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Name TEXT NOT NULL, TrackType TEXT NOT NULL, Description TEXT NOT NULL "; SQLiteCommand command = new SQLiteCommand(sql, sqlite); command.ExecuteNonQuery();}public static void SaveEntry(Habit newHabit){ var slite = new SQLiteConnection("Data Source=" + dbFile); sqlite.Open(); SQLiteCommand insertSQL = new SQLiteCommand( "INSERT INTO habits(Name, TrackType, Description) VALUES (?,?,?)", sqlite ); insertSQL.Parameters.Add(newHabit.Name); insertSQL.Parameters.Add(newHabit.TrackType); insertSQL.Parameters.Add(newHabit.Description); try { insertSQL.ExecuteNonQuery(); } catch (Exception ex) { throw new Exception(ex.Message);}
private static readonly string dbFile = "habits.db";public static void CreateDatabase(){ SQLiteConnection.CreateFile(dbFile); var sqlite = new SQLConnection("Data Source=" + dbFile); sqlite.Open(); string sql = @"CREATE TABLE IF NOT EXISTS habits( Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Name TEXT NOT NULL, TrackType TEXT NOT NULL, Description TEXT NOT NULL "; SQLiteCommand command = new SQLiteCommand(sql, sqlite); command.ExecuteNonQuery();}public static void SaveEntry(Habit newHabit){ var slite = new SQLiteConnection("Data Source=" + dbFile); sqlite.Open(); SQLiteCommand insertSQL = new SQLiteCommand( "INSERT INTO habits(Name, TrackType, Description) VALUES (?,?,?)", sqlite ); insertSQL.Parameters.Add(newHabit.Name); insertSQL.Parameters.Add(newHabit.TrackType); insertSQL.Parameters.Add(newHabit.Description); try { insertSQL.ExecuteNonQuery(); } catch (Exception ex) { throw new Exception(ex.Message);}