

Set fetchValue=true (default false) in the Start function when the existing database->ExecSQL is called to prevent multiple WriteCount calls in the DatabaseExecSQLResult function.Begin a new transaction in the TaskReadWriteCount::Start function.Add class IPreparedStatement * stmtHistoryInsert as parameter to the constructor and set it inside the constructor, as we need the stmtHistoryInsert pointer of the NewApp1 class.Add a bool member fetchValue, as we need to modify the state machine.Add a member class IPreparedStatement * stmtHistoryInsert.Add overloads for DatabaseBeginTransactionResult and DatabaseEndTransactionResult to the TaskReadWriteCount class.Use of the prepared statements inside the already existing class TaskReadWriteCount Void NewApp1::DatabaseShutdown(IDatabase * const database, db_error_t reason) If(!this->stmtHistoryInsert) this->stmtHistoryInsert = statement Void NewApp1::DatabasePrepareStatementResult(IDatabase * const database, class IPreparedStatement * statement) This->stmtHistoryRead = nullptr // tutorial changeĭatabase->PrepareStatement(this, "INSERT INTO history (user_id,value) VALUES ((SELECT id FROM users WHERE sip=%s),%llu)") // tutorial changeĭatabase->PrepareStatement(this, "SELECT u.sip, h.timestamp, h.value FROM history h " \ This->stmtHistoryInsert = nullptr // tutorial change This->database->Connect(args->dbHost, args->dbName, args->dbUser, args->dbPassword) This->database = service->databaseProvider->CreateDatabase(iomux, this, this) This->webserverPlugin = service->webserverPluginProvider->CreateWebserverPlugin(iomux, service->localSocketProvider, this, args->webserver, args->webserverPath, this) NewApp1::NewApp1(IIoMux * const iomux, class NewApp1Service * service, AppInstanceArgs * args) : AppInstance(service, args), AppUpdates(iomux), ConfigContext(nullptr, this) Call the Start function of the initHistory task inside the TaskComplete function of the class TaskDatabaseInit to start the task which creates the table and columns.Ĭlass TaskDatabaseInit in NewApp1_tasks.h after these changesĬlass TaskDatabaseInit : public ITask, public UTask Ĭlass IWebserverPlugin * webserverPlugin Ĭlass AppUpdatesFilters badgeCountFilters Ĭlass IPreparedStatement * stmtHistoryInsert // tutorial changeĬlass IPreparedStatement * stmtHistoryRead // tutorial changeĬlass NewApp1 in NewApp1.cpp after these changes (just changed functions).Add the neccessary columns with a foreign key and index for this key to the users table inside the constructor of the class TaskDatabaseInit.Initialize this class member after the constructor definition in the cpp file.

Add a new class member class TaskPostgreSQLInitTable initHistory into the class TaskDatabaseInit.Tables and columns are always initialized on each start of the App instance, so you can add tables and columns later without issues.Header file NewApp1_tasks.h, cpp file NewApp1_tasks.cpp inside the NewApp1 folder. Your filenames might be different according to your settings.

The used file and class names in this example are based on a newly created App with the name NewApp1 and the company name innovaphone. The history is to be displayed inside a scrolling area below the counter and queried once after opening the app.Use prepared statemens for better query performance.Use a transaction, as multiple writing queries are executed which belong to the same logical action.Create a history table where every change of the counter is tracked with a timestamp and the sip of the logged in user.We will also make use of the javascript object and the UTaskTemplate of the task classes. The tutorial is based on a newly created innovaphone App with the Visual Studio plugin.
#IDATABASE TUTORIAL HOW TO#
The scope of this tutorial is to learn, how to add new tables, prepared statements etc. Innovaphone App database tutorial Tutorial: database
