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 =
|
2023-06-02 13:02:38 +02:00
|
|
|
"CREATE TABLE Trials(Id INTEGER PRIMARY KEY AUTOINCREMENT, Position TEXT, Start INT, Frequency NUMBER);";
|
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
|
|
|
}
|
|
|
|
|
2023-06-02 12:48:29 +02:00
|
|
|
char *generateDataTableName(int trialId, int captorNumber)
|
|
|
|
{
|
|
|
|
|
|
|
|
char *tableName;
|
|
|
|
tableName = (char *)malloc(8 * sizeof(char));
|
|
|
|
strcpy(tableName, "T");
|
|
|
|
strcat(tableName, convertIntegerToChar(trialId));
|
|
|
|
strcat(tableName, "C");
|
|
|
|
strcat(tableName, convertIntegerToChar(captorNumber));
|
|
|
|
strcat(tableName, "Data");
|
|
|
|
return tableName;
|
|
|
|
}
|
|
|
|
|
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
|
2023-06-02 12:48:29 +02:00
|
|
|
char *tableName = generateDataTableName(trialId, captorNumber);
|
2023-06-01 17:22:38 +02:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-06-02 13:02:38 +02:00
|
|
|
int insertTrailElement(char *position, int start, double frequency)
|
2023-06-02 12:48:29 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
sqlite3 *db;
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
|
|
|
|
char *err_msg;
|
|
|
|
int rc;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
rc = sqlite3_open("robotgowest.db", &db);
|
|
|
|
|
2023-06-02 13:02:38 +02:00
|
|
|
rc = sqlite3_prepare_v2(db, "INSERT INTO Trials(Position,Start,Frequency) VALUES ( :position , :start , :frequency )", -1, &stmt, NULL);
|
2023-06-02 12:48:29 +02:00
|
|
|
if (rc != SQLITE_OK)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
sqlite3_bind_text(stmt, 1, position, -1, SQLITE_STATIC);
|
|
|
|
|
|
|
|
sqlite3_bind_int(stmt, 2, start);
|
|
|
|
|
|
|
|
sqlite3_bind_double(stmt, 3, frequency);
|
|
|
|
|
|
|
|
rc = sqlite3_step(stmt);
|
|
|
|
rc = sqlite3_finalize(stmt);
|
|
|
|
sqlite3_close(db);
|
|
|
|
}
|
|
|
|
|
|
|
|
int insertCaptorMetadataElement(int captorNumber, int trialId)
|
|
|
|
{
|
|
|
|
|
|
|
|
sqlite3 *db;
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
|
|
|
|
char *err_msg;
|
|
|
|
int rc;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
rc = sqlite3_open("robotgowest.db", &db);
|
|
|
|
|
|
|
|
rc = sqlite3_prepare_v2(db, "INSERT INTO CaptorMetadata(CaptorNumber,TrialId) VALUES ( :captorNumber , :trialId)", -1, &stmt, NULL);
|
|
|
|
if (rc != SQLITE_OK)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
sqlite3_bind_int(stmt, 1, captorNumber);
|
|
|
|
|
|
|
|
sqlite3_bind_int(stmt, 2, trialId);
|
|
|
|
|
|
|
|
rc = sqlite3_step(stmt);
|
|
|
|
rc = sqlite3_finalize(stmt);
|
|
|
|
sqlite3_close(db);
|
|
|
|
}
|
|
|
|
|
|
|
|
int insertCaptorDataElement(int trialId, int captorNumber, double lux, double mean, double power)
|
|
|
|
{
|
|
|
|
|
|
|
|
char *tableName = generateDataTableName(trialId, captorNumber);
|
|
|
|
|
|
|
|
sqlite3 *db;
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
|
|
|
|
char *err_msg;
|
|
|
|
int rc;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
rc = sqlite3_open("robotgowest.db", &db);
|
|
|
|
|
|
|
|
char sql[70] = "INSERT INTO ";
|
|
|
|
strcat(sql, tableName);
|
|
|
|
strcat(sql, "(Lux,Mean,Power) VALUES ( :lux , :mean , :power)");
|
|
|
|
printf(sql);
|
|
|
|
|
|
|
|
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
|
|
|
|
if (rc != SQLITE_OK)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
sqlite3_bind_double(stmt, 1, lux);
|
|
|
|
sqlite3_bind_double(stmt, 2, mean);
|
|
|
|
sqlite3_bind_double(stmt, 3, power);
|
|
|
|
|
|
|
|
rc = sqlite3_step(stmt);
|
|
|
|
rc = sqlite3_finalize(stmt);
|
|
|
|
sqlite3_close(db);
|
|
|
|
}
|
|
|
|
|
2023-06-01 17:22:38 +02:00
|
|
|
int main(void)
|
|
|
|
{
|
2023-06-01 23:26:13 +02:00
|
|
|
sqlite3_initialize();
|
|
|
|
|
2023-06-02 12:48:29 +02:00
|
|
|
// initializeTrailsTable();
|
|
|
|
// initializeCaptorMetadataTable();
|
|
|
|
// createCaptorDataTable(12, 8);
|
|
|
|
// insertTrailElement("test", 1, 3.0, 9);
|
|
|
|
// insertCaptorMetadataElement(1, 2);
|
|
|
|
// insertCaptorDataElement(12, 8, 10, 121, 1513);
|
|
|
|
|
2023-06-01 23:26:13 +02:00
|
|
|
sqlite3_shutdown();
|
2023-06-01 17:22:38 +02:00
|
|
|
}
|