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

Binary file not shown.

Binary file not shown.