Traitement-signal-plantes/Db-Script/database.c

216 lines
4.8 KiB
C

#include "./database.h"
int initializeTrailsTable()
{
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 Trials(Id INTEGER PRIMARY KEY AUTOINCREMENT, Position TEXT, Start INT, Frequency NUMBER, Duration INT);";
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
rc = sqlite3_step(stmt);
rc = sqlite3_finalize(stmt);
sqlite3_close(db);
}
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);
sqlite3_close(db);
}
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;
}
int createCaptorDataTable(int trialId, int captorNumber)
{
sqlite3 *db;
char *err_msg;
int rc;
sqlite3_stmt *stmt;
// Create Table Name
char *tableName = generateDataTableName(trialId, captorNumber);
rc = sqlite3_open("robotgowest.db", &db);
if (rc != SQLITE_OK)
{
return 1;
}
char sql[101] = "CREATE TABLE ";
strcat(sql, tableName);
strcat(sql, " (Id INTEGER PRIMARY KEY AUTOINCREMENT, Lux NUMBER, Mean NUMBER , Power NUMBER);");
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
rc = sqlite3_step(stmt);
rc = sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
/**
* @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 insertTrailElement(char *position, int start, double frequency, int duration)
{
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 Trials(Position,Start,Frequency,Duration) VALUES ( :position , :start , :frequency , :duration )", -1, &stmt, NULL);
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);
sqlite3_bind_int(stmt, 4, duration);
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);
}
int main(void)
{
sqlite3_initialize();
// initializeTrailsTable();
// initializeCaptorMetadataTable();
// createCaptorDataTable(12, 8);
// insertTrailElement("test", 1, 3.0, 9);
// insertCaptorMetadataElement(1, 2);
// insertCaptorDataElement(12, 8, 10, 121, 1513);
sqlite3_shutdown();
}