private void RefreshData() { // Clear the textboxes achTextBox.Clear(); datesTextBox.Clear(); // Initialize GuardTime. GuardTime guard = new GuardTime(); // Issue a new SELECT query for the Achievements table. SQLize refreshAch = new SQLize("SELECT * FROM (SELECT * FROM Achievements ORDER BY ach_id DESC LIMIT 17) ORDER BY ach_id"); // Call Read_Ach to read the data. refreshAch.Read_Ach(); // Set the output of SQLize's Get_Out method to the Achievements textbox.text achTextBox.Text = refreshAch.Get_Out(); // Issue a new SELECT query for the Dates table. SQLize refreshDates = new SQLize("SELECT datetime FROM (SELECT * FROM Dates ORDER BY date_id DESC LIMIT 17) ORDER BY date_id"); // Call Read_Dates to read the data. refreshDates.Read_Dates(); // Set the output of SQLize's Get_Out method to the Dates textbox.text datesTextBox.Text = refreshDates.Get_Out(); }
// Refresh the goal date label. private void RefreshGoalDate() { GuardTime guard = new GuardTime(); // Tell GuardTime to set the config. guard.setConfig(); // Initialize SQLize class and pass it the command. SQLize calc = new SQLize("SELECT * FROM Dates ORDER BY datetime DESC LIMIT 1"); // Tell SQLize to return OneDate and set it equal to a string. string aDate = calc.Read_OneDate(); // If the date is null, we don't want to be doing operations on it. // This could happen when Guard starts for the first time. if (aDate != null) { // Parse the string into a 64 bit integer. Int64 n; Int64.TryParse(aDate, out n); // Assign our original date -> necessary for the counting UP process. eventDateInt = n; // Tell guard to calculate the release date and set the goalDate string. goalDate = guard.CalcReleaseDate(n); // set our int value goalDateInt = guard.CalcReleaseDateInt(n); // Display the goal date without GMT information nextGoalLabel.Text = guard.RemoveGMT(goalDate); } }
// Refresh the table by updating the gregorian table in sqlite and then reading it into the table. private void RefreshTable() { // Initialize Guardtime GuardTime guard = new GuardTime(); // Populate the Gregorian Table SQLize GenerateTimes = new SQLize("SELECT datetime FROM (SELECT * FROM Dates ORDER BY date_id DESC) ORDER BY date_id"); // Call PopulateTimestamps to update the Gregorian table GenerateTimes.PopulateTimestamps(); // Create a connection to the DB. SQLiteConnection m_dbConnection; m_dbConnection = new SQLiteConnection("Data Source=GuardDB.sqlite;Version=3;"); m_dbConnection.Open(); // Create our adapter var adapter = new SQLiteDataAdapter("SELECT day_of_week, month, day, year, timestamp FROM Gregorian", m_dbConnection); // Fill the dataset object adapter.Fill(ds); // Close the connection to the DB m_dbConnection.Close(); // Show this in the Data Grid View guardDatesDGV.DataSource = ds.Tables[0].DefaultView; }
// Refreshes Some Stats private void RefreshStats() { // Issue a COUNT query for the Dates table. SQLize countDates = new SQLize("SELECT COUNT(date_id) FROM Dates"); // Run that command countDates.Count_Query(); // Set the output of SQLize's Get_Out method to the numEntries Label.text numEntriesLabel.Text = countDates.Get_Out(); }
// MAIN CODE BODY BEGIN // Guard is a rough, prototype application for tracking addiction or other habits // that a user might wish to be rid of. It is a simple program that tracks dates // on which a user might "cave in" to a craving and then assists by allowing them // a date on which they are allowed to partake in a craving. The time spans // gradually move farther apart based on user settings - allowing for an easier time // of quitting. private void MainWindow_Load(object sender, EventArgs e) { // CREATE & CONNECT TO DB // // First we need to create our tables if they don't exist. // We use the SQLize class and create some objects first. SQLize datesTable = new SQLize("CREATE TABLE IF NOT EXISTS Dates (date_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, datetime INTEGER NOT NULL)"); SQLize achTable = new SQLize("CREATE TABLE IF NOT EXISTS Achievements (ach_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, best_time INTEGER NOT NULL)"); SQLize gregTable = new SQLize("CREATE TABLE IF NOT EXISTS Gregorian (greg_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, month STRING NOT NULL," + " day STRING NOT NULL, day_of_week STRING NOT NULL, year STRING NOT NULL, timestamp STRING NOT NULL)"); // Next we use the Run_Command for the SQLize class to run the commands. datesTable.Run_Command(); achTable.Run_Command(); gregTable.Run_Command(); // Call RefreshData function to refresh the data RefreshData(); // RUN THE TIMERS RefreshGoalDate(); RefreshCountdown(); RefreshElapsed(); }
// ADD NOW BUTTON // // When a user clicks the 'Add Now' button, the current date and // time are entered into the Dates table as a unix timestamp. private void addNowButton_Click(object sender, EventArgs e) { // Delcare some variables we need Int64 unixTimeThen = 0; Int64 compare = 0; Int64 highest = 0; // When button is clicked Int64 unixTimeNow = DateTimeOffset.Now.ToUnixTimeSeconds(); // Create a new SQLize instance to get the last date SQLize retrieveDate = new SQLize("SELECT * FROM Dates ORDER BY datetime DESC LIMIT 1"); // Store this data briefly with the Read_OneDateInt function of SQLizer unixTimeThen = retrieveDate.Read_OneDateInt(); // Check the difference between unixTimeNow and unixTimeThen with GuardTime // But ONLY if the value of unixTimeThen is not 0 which would indicate // that there are no records. We need at least one date entry in the dates // table to perform calculations! if (unixTimeThen != 0) { GuardTime guard = new GuardTime(); compare = guard.CalcUnixTimeSpan(unixTimeThen, unixTimeNow); // Create one more SQLize instance to grab the last time span from the achievements table SQLize retrieveBest = new SQLize("SELECT * FROM Achievements ORDER BY best_time DESC LIMIT 1"); highest = retrieveBest.Read_OneDateInt(); // If the the highest time is 0, then there are no records which means we should add one! if (highest == 0) { // Create a new SQLize instance SQLize add = new SQLize("INSERT INTO Achievements (best_time) VALUES(" + compare + ")"); // Run that command add.Run_Command(); } // If the highest time is not 0, then determine if we should insert a new best time. if (compare > highest && highest != 0) { // Create a new SQLize instance SQLize add = new SQLize("INSERT INTO Achievements (best_time) VALUES(" + compare + ")"); // Run that command add.Run_Command(); } } // Issue an Insert statement to the Dates table. SQLize insert = new SQLize("INSERT INTO Dates (datetime) VALUES (" + unixTimeNow + ")"); // Tell SQLize to run the command. insert.Run_Command(); // Call Refresh Data RefreshData(); // Refresh our timers RefreshGoalDate(); RefreshCountdown(); RefreshElapsed(); }
// ADD BUTTON // // When a user clicks this button a new record containing a custom date and time // is inserted into the database and the database is refreshed. private void addButton_Click(object sender, EventArgs e) { // Declare some variables we need Int64 unixTimeThen = 0; Int64 compare = 0; Int64 highest = 0; // Begin by creating a new SQLize instance to get the last date SQLize retrieveDate = new SQLize("SELECT * FROM Dates ORDER BY datetime DESC LIMIT 1"); // Store this data briefly with the Read_OneDateInt function of SQLizer unixTimeThen = retrieveDate.Read_OneDateInt(); // Grab our values from the text box // Day string day = dayComboBox.Text; // Month string month = monthComboBox.Text; // Year string year = yearPicker.Text; // Time string time = timePicker.Text; // Parse these values into Guards date format. string dateStringRaw = day + " " + month + " " + year + " " + time; DateTime inputDate = DateTime.Parse(dateStringRaw); // Get ourselves a datetime offset. DateTimeOffset dto = new DateTimeOffset(inputDate); // Turn this into a unix timestamp! Int64 unixTimeNow = dto.ToUnixTimeSeconds(); // Check the difference between unixTimeNow and unixTimeThen with GuardTime // But ONLY if the value of unixTimeThen is not 0 which would indicate // that there are no records. We need at least one date entry in the dates // table to perform calculations! if (unixTimeThen != 0) { // Check the difference between unixTimeNow and unixTimeThen with GuardTime GuardTime guard = new GuardTime(); compare = guard.CalcUnixTimeSpan(unixTimeThen, unixTimeNow); // Create one more SQLize instance to grab the last time span from the achievements table SQLize retrieveBest = new SQLize("SELECT * FROM Achievements ORDER BY best_time DESC LIMIT 1"); highest = retrieveBest.Read_OneDateInt(); // If the highest time is 0, then there are no records whcih means we should add one! if (highest == 0) { // Create a new SQLize instance SQLize add = new SQLize("INSERT INTO Achievements (best_time) VALUES(" + compare + ")"); // Run that command add.Run_Command(); } // Determine if we should insert a new best time // If the highest time is not 0, then determine if we should insert a new best time! if (compare > highest && highest != 0) { // Create a new SQLize instance SQLize add = new SQLize("INSERT INTO Achievements (best_time) VALUES(" + compare + ")"); // Run that command add.Run_Command(); } } // Create our date //string dateStringNew = "\'" + unixTimeNow.ToString() + "\'"; // Issue an insert statement to the Dates table SQLize insert = new SQLize("INSERT INTO Dates (datetime) VALUES (" + unixTimeNow + ")"); // Tell SQLize to run the command. insert.Run_Command(); }