diff --git a/Db-Script/database.c b/Db-Script/database.c index d27e2b7..25f1854 100644 --- a/Db-Script/database.c +++ b/Db-Script/database.c @@ -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);" - "CREATE TABLE CaptorMetadata(Id INTEGER PRIMARY KEY AUTOINCREMENT, CaptorNumber INT, TrialId INT, FOREIGN KEY (TrialId) REFERENCES Trials(Id));"; + 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); - 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); + sqlite3_close(db); +} - printf("%i\n", rc); +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); } 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,26 +55,22 @@ 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) { - 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; - } - sqlite3_close(db); - - return 0; + 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; } /** @@ -109,7 +111,8 @@ int main(void) { sqlite3_initialize(); - initializeTrailsAndCaptorMetadataTables(); - // createCaptorDataTable(1, 1); + initializeTrailsTable(); + initializeCaptorMetadataTable(); + createCaptorDataTable(12, 8); sqlite3_shutdown(); } \ No newline at end of file diff --git a/Db-Script/db b/Db-Script/db index e4aa2f9..c116f7d 100755 Binary files a/Db-Script/db and b/Db-Script/db differ diff --git a/Db-Script/robotgowest.db b/Db-Script/robotgowest.db index 01bd220..1e6be5e 100644 Binary files a/Db-Script/robotgowest.db and b/Db-Script/robotgowest.db differ