visit
If you haven't read: Building Your First App Using C++ Builder (Part 1)
This part will integrate SQLite into the application and attempt to store reminders added by users into the database.
After adding reminders to the database, we will display reminders to users when they are requested.We created a database on the desktop located in the following path:
C:\\Users\\uk\\OneDrive\\Desktop\\reminderdb.db
FDConnection1->DriverName = "SQLite";
FDConnection1->Params->Values["Database"] = "C:\\Users\\uk\\OneDrive\\Desktop\\reminderdb.db";
FDConnection1->Open();
This command goes hand in hand with the close database command, and once we have finished executing all queries, we will need to close the connection to the database using:FDConnection1->Close();
After establishing and opening the communication portal, we will execute an SQL statement to create the reminder table if it does not exist in the reminder database. We need only two fields for the table, i.e., the id field and the DateTime field, stored in string format. To create a table, we can call the ExecSQL statement to execute a SQL query. This ExecSQL function can be called whenever you want to create tables, insert or delete data, but it can't be used if data needs to be retrieved from the database. Add the following statement to create a table if it does not exist in the database.
FDConnection1->ExecSQL("CREATE TABLE IF NOT EXISTS Reminder (Id int, time TEXT NOT NULL)");
String date = dateTime.FormatString("dd/mm/yyyy HH:mm");
FDConnection1->ExecSQL("INSERT INTO Reminder (time) VALUES ('"+date+"')");
TFDQuery *query;
query = new TFDQuery(this);
query->Connection = FDConnection1;
query->SQL->Text = "SELECT * FROM Reminder";
query->Open();
To make use of TFDQuery, the following header needs to be added to your code:
#include <FireDAC.DApt.hpp>
Code for outputting retrieved dataTVarRec args[1] = {"time"};
Memo1->Lines->Add(System::UnicodeString::Format("|%25s |", args,ARRAYSIZE(args) - 1));
while (!query->Eof) {
TVarRec arguments[1] = { query->FieldByName("time")->AsString};
Memo1->Lines->Add(System::UnicodeString::Format("|%-25s |", arguments, ARRAYSIZE(arguments) - 1));
query->Next();
}
#include <FireDAC.DApt.hpp>
void __fastcall TForm2::buttonAddReminderOnClick(TObject *Sender)
{
TDateTime dateTime;
ReplaceDate(dateTime, DateTimePicker1->Date);
ReplaceTime(dateTime, TimePicker1->Time);
Memo1->Text = "";
FDConnection1->DriverName = "SQLite";
FDConnection1->Params->Values["Database"] = "C:\\Users\\k_umm\\OneDrive\\Desktop\\reminderdb.db";
try
{
FDConnection1->Open();
FDConnection1->ExecSQL("CREATE TABLE IF NOT EXISTS Reminder (id INTEGER PRIMARY KEY AUTOINCREMENT, time TEXT NOT NULL)");
String date = dateTime.FormatString("dd/mm/yyyy HH:mm");
FDConnection1->ExecSQL("INSERT INTO Reminder (time) VALUES ('"+date+"')");
FDConnection1->Close();
}
catch (Exception& E)
{
Memo1->Text = E.Message;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm2::buttonShowReminderOnClick(TObject *Sender)
{
FDConnection1->DriverName = "SQLite";
FDConnection1->Params->Values["Database"] = "C:\\Users\\k_umm\\OneDrive\\Desktop\\reminderdb.db";
try
{
FDConnection1->Open();
TFDQuery *query;
query = new TFDQuery(this);
query->Connection = FDConnection1;
query->SQL->Text = "SELECT * FROM Reminder";
query->Open();
TVarRec args[1] = {"time"};
Memo1->Lines->Add(System::UnicodeString::Format("|%25s |", args, ARRAYSIZE(args) - 1));
while (!query->Eof) {
TVarRec arguments[1] = { query->FieldByName("time")->AsString};
Memo1->Lines->Add(System::UnicodeString::Format("|%-25s |", arguments, ARRAYSIZE(arguments) - 1));
query->Next();
}
query->Close();
FDConnection1->Close();
}
catch (Exception& E)
{
Memo1->Text = E.Message;
}
}
MyNotification->FireDate = System::Dateutils::EncodeDateTime(2015, 12, 16, 17, 30, 00, 00);