2023-06-01 17:22:38 +02:00
|
|
|
#include "./database.h"
|
|
|
|
|
2023-06-02 00:03:16 +02:00
|
|
|
int initializeTrailsTable()
|
2023-06-01 17:22:38 +02:00
|
|
|
{
|
|
|
|
sqlite3 *db;
|
2023-06-01 23:26:13 +02:00
|
|
|
char *err_msg;
|
2023-06-01 17:22:38 +02:00
|
|
|
int rc;
|
2023-06-01 23:26:13 +02:00
|
|
|
sqlite3_stmt *stmt;
|
2023-06-01 17:22:38 +02:00
|
|
|
|
2023-06-01 23:26:13 +02:00
|
|
|
rc = sqlite3_open("robotgowest.db", &db);
|
2023-06-01 17:22:38 +02:00
|
|
|
|
2023-06-01 23:26:13 +02:00
|
|
|
// char *sql = "CREATE TABLE Test(Id INT)";
|
2023-06-01 17:22:38 +02:00
|
|
|
|
2023-06-02 00:03:16 +02:00
|
|
|
char *sql =
|
|
|
|
"CREATE TABLE Trials(Id INTEGER PRIMARY KEY AUTOINCREMENT, Position TEXT, Start INT, Frequency NUMBER, Duration INT);";
|
2023-06-01 17:22:38 +02:00
|
|
|
|
2023-06-01 23:26:13 +02:00
|
|
|
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
|
|
|
|
rc = sqlite3_step(stmt);
|
|
|
|
rc = sqlite3_finalize(stmt);
|
2023-06-02 00:03:16 +02:00
|
|
|
sqlite3_close(db);
|
|
|
|
}
|
2023-06-01 23:26:13 +02:00
|
|
|
|
2023-06-02 00:03:16 +02:00
|
|
|
int initializeCaptorMetadataTable()
|
|
|
|
{
|
|
|
|
sqlite3 *db;
|
|
|
|
char *err_msg;
|
|
|
|
int rc;
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
|
|
|
|
rc = sqlite3_open("robotgowest.db", &db);
|
|
|
|
|
|
|
|
// char *sql = "CREATE TABLE Test(Id INT)";
|
|
|
|
|
|
|
|
char *sql =
|
|
|
|
"CREATE TABLE CaptorMetadata(Id INTEGER PRIMARY KEY AUTOINCREMENT, CaptorNumber INT, TrialId INT, FOREIGN KEY (TrialId) REFERENCES Trials(Id));";
|
|
|
|
|
|
|
|
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
|
|
|
|
rc = sqlite3_step(stmt);
|
|
|
|
rc = sqlite3_finalize(stmt);
|
2023-06-01 23:26:13 +02:00
|
|
|
sqlite3_close(db);
|
2023-06-01 17:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int createCaptorDataTable(int trialId, int captorNumber)
|
|
|
|
{
|
|
|
|
sqlite3 *db;
|
2023-06-02 00:03:16 +02:00
|
|
|
char *err_msg;
|
2023-06-01 17:22:38 +02:00
|
|
|
int rc;
|
2023-06-02 00:03:16 +02:00
|
|
|
sqlite3_stmt *stmt;
|
2023-06-01 17:22:38 +02:00
|
|
|
|
|
|
|
// Create Table Name
|
|
|
|
char tableName[8];
|
|
|
|
strcpy(tableName, "T");
|
|
|
|
strcat(tableName, convertIntegerToChar(trialId));
|
|
|
|
strcat(tableName, "C");
|
|
|
|
strcat(tableName, convertIntegerToChar(captorNumber));
|
|
|
|
strcat(tableName, "Data");
|
|
|
|
|
2023-06-02 00:03:16 +02:00
|
|
|
rc = sqlite3_open("robotgowest.db", &db);
|
|
|
|
if (rc != SQLITE_OK)
|
2023-06-01 17:22:38 +02:00
|
|
|
{
|
2023-06-02 00:03:16 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2023-06-01 17:22:38 +02:00
|
|
|
|
2023-06-02 00:03:16 +02:00
|
|
|
char sql[101] = "CREATE TABLE ";
|
|
|
|
strcat(sql, tableName);
|
|
|
|
strcat(sql, " (Id INTEGER PRIMARY KEY AUTOINCREMENT, Lux NUMBER, Mean NUMBER , Power NUMBER);");
|
2023-06-01 17:22:38 +02:00
|
|
|
|
2023-06-02 00:03:16 +02:00
|
|
|
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
|
|
|
|
rc = sqlite3_step(stmt);
|
|
|
|
rc = sqlite3_finalize(stmt);
|
|
|
|
sqlite3_close(db);
|
2023-06-01 17:22:38 +02:00
|
|
|
|
2023-06-02 00:03:16 +02:00
|
|
|
return 0;
|
2023-06-01 17:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief convert an interger N into a char*
|
|
|
|
*
|
|
|
|
* @param N
|
|
|
|
* @return char*
|
|
|
|
*/
|
|
|
|
char *convertIntegerToChar(int N)
|
|
|
|
{
|
|
|
|
// Count digits in number N
|
|
|
|
int m = N;
|
|
|
|
int digit = 0;
|
|
|
|
while (m)
|
|
|
|
{
|
|
|
|
digit++;
|
|
|
|
m /= 10;
|
|
|
|
}
|
|
|
|
char *arr;
|
|
|
|
char arr1[digit];
|
|
|
|
arr = (char *)malloc(digit * sizeof(char));
|
|
|
|
int index = 0;
|
|
|
|
while (N)
|
|
|
|
{
|
|
|
|
arr1[++index] = N % 10 + '0';
|
|
|
|
N /= 10;
|
|
|
|
}
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < index; i++)
|
|
|
|
{
|
|
|
|
arr[i] = arr1[index - i];
|
|
|
|
}
|
|
|
|
arr[i] = '\0';
|
|
|
|
return (char *)arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2023-06-01 23:26:13 +02:00
|
|
|
sqlite3_initialize();
|
|
|
|
|
2023-06-02 00:03:16 +02:00
|
|
|
initializeTrailsTable();
|
|
|
|
initializeCaptorMetadataTable();
|
|
|
|
createCaptorDataTable(12, 8);
|
2023-06-01 23:26:13 +02:00
|
|
|
sqlite3_shutdown();
|
2023-06-01 17:22:38 +02:00
|
|
|
}
|