create script ok

This commit is contained in:
Quentin Perret 2023-06-02 00:03:16 +02:00
parent b1a5b8b7bf
commit 4672159f50
3 changed files with 41 additions and 38 deletions

View file

@ -1,6 +1,6 @@
#include "./database.h" #include "./database.h"
int initializeTrailsAndCaptorMetadataTables() int initializeTrailsTable()
{ {
sqlite3 *db; sqlite3 *db;
char *err_msg; char *err_msg;
@ -8,38 +8,44 @@ int initializeTrailsAndCaptorMetadataTables()
sqlite3_stmt *stmt; sqlite3_stmt *stmt;
rc = sqlite3_open("robotgowest.db", &db); rc = sqlite3_open("robotgowest.db", &db);
printf("%i\n", rc);
// char *sql = "CREATE TABLE Test(Id INT)"; // char *sql = "CREATE TABLE Test(Id INT)";
char *sql = "DROP TABLE IF EXISTS Trials;" char *sql =
"DROP TABLE IF EXISTS CaptorMetadata;" "CREATE TABLE Trials(Id INTEGER PRIMARY KEY AUTOINCREMENT, Position TEXT, Start INT, Frequency NUMBER, Duration INT);";
"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));"; "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_prepare_v2(db, sql, -1, &stmt, NULL);
printf("%i\n", rc);
if (rc != SQLITE_OK)
{
return 1;
}
rc = sqlite3_step(stmt); rc = sqlite3_step(stmt);
printf("%i\n", rc);
if (rc != SQLITE_DONE)
{
return 1;
}
rc = sqlite3_finalize(stmt); rc = sqlite3_finalize(stmt);
printf("%i\n", rc);
sqlite3_close(db); sqlite3_close(db);
} }
int createCaptorDataTable(int trialId, int captorNumber) int createCaptorDataTable(int trialId, int captorNumber)
{ {
sqlite3 *db; sqlite3 *db;
char *err_msg = 0; char *err_msg;
int rc; int rc;
sqlite3_stmt *stmt;
// Create Table Name // Create Table Name
char tableName[8]; char tableName[8];
@ -49,27 +55,23 @@ int createCaptorDataTable(int trialId, int captorNumber)
strcat(tableName, convertIntegerToChar(captorNumber)); strcat(tableName, convertIntegerToChar(captorNumber));
strcat(tableName, "Data"); strcat(tableName, "Data");
if (sqlite3_open("robotgowest.db", &db) == SQLITE_OK) rc = sqlite3_open("robotgowest.db", &db);
if (rc != SQLITE_OK)
{ {
return 1;
}
char sql[101] = "CREATE TABLE "; char sql[101] = "CREATE TABLE ";
strcat(sql, tableName); strcat(sql, tableName);
strcat(sql, " (Id INTEGER PRIMARY KEY AUTOINCREMENT, Lux NUMBER, Mean NUMBER , Power NUMBER);"); strcat(sql, " (Id INTEGER PRIMARY KEY AUTOINCREMENT, Lux NUMBER, Mean NUMBER , Power NUMBER);");
if (rc != SQLITE_OK) rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
{ rc = sqlite3_step(stmt);
printf("Error code : %i", rc); rc = sqlite3_finalize(stmt);
fprintf(stderr, "SQL error: %s\n", err_msg);
sqlite3_free(err_msg);
sqlite3_close(db);
return 1;
}
sqlite3_close(db); sqlite3_close(db);
return 0; return 0;
} }
}
/** /**
* @brief convert an interger N into a char* * @brief convert an interger N into a char*
@ -109,7 +111,8 @@ int main(void)
{ {
sqlite3_initialize(); sqlite3_initialize();
initializeTrailsAndCaptorMetadataTables(); initializeTrailsTable();
// createCaptorDataTable(1, 1); initializeCaptorMetadataTable();
createCaptorDataTable(12, 8);
sqlite3_shutdown(); sqlite3_shutdown();
} }

Binary file not shown.

Binary file not shown.