add database script in c for creation and init

This commit is contained in:
QuentinPerret 2023-06-02 15:53:17 +02:00
parent 2bd028910f
commit 2443e553c8
10 changed files with 134 additions and 81 deletions

View file

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.6)
cmake_minimum_required(VERSION 2.8)
project(Traitement-signal-plantes C)
include(CTest)
@ -10,16 +10,21 @@ file(MAKE_DIRECTORY Executable)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY Executable) #Set Executable directory as default exectutable file location
add_executable(exect fileGestion.c getArray.c average.c growthRate.c power.c queue.c simulateFlux.c main.c)
add_executable(exect fileGestion.c getArray.c average.c growthRate.c power.c queue.c simulateFlux.c database.c main.c)
# add_executable(exect main.c simulateFlux.c queue.c power.c growthRate.c average.c getArray.c fileGestion.c)
find_package (SQLite3)
include_directories(${SQLite3_INCLUDE_DIRS})
target_link_libraries (exect ${SQLite3_LIBRARIES} m)
find_package(Threads)
target_link_libraries(exect ${CMAKE_THREAD_LIBS_INIT} m)
set(CTEST_MEMORYCHECK_TYPE "AddressSanitizer")
set(CTEST_MEMORYCHECK_SANITIZER_OPTIONS "verbosity=1:symbolize=1:abort_on_error=1:detect_leaks=1")
add_executable(ctest fileGestion.c getArray.c average.c growthRate.c queue.c simulateFlux.c ctest.c)
add_executable(ctest fileGestion.c getArray.c average.c growthRate.c queue.c simulateFlux.c database.c ctest.c)
target_link_libraries(ctest ${CMAKE_THREAD_LIBS_INIT} m)
add_test(test_queueCreateEmpty ./ctest queueCreateEmpty)

11
Code-C/Include/database.h Normal file
View file

@ -0,0 +1,11 @@
#include <sqlite3.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <math.h>
int createDb();
int initiaizeNewTrial(char *position, double frequency, bool *captorOneHot);
int initiaizeNewTrial(char *position, double frequency, bool *captorOneHot);

View file

@ -0,0 +1,37 @@
# Copyright (C) 2007-2009 LuaDist.
# Created by Peter Kapec <kapecp@gmail.com>
# Redistribution and use of this file is allowed according to the terms of the MIT license.
# For details see the COPYRIGHT file distributed with LuaDist.
# Note:
# Searching headers and libraries is very simple and is NOT as powerful as scripts
# distributed with CMake, because LuaDist defines directories to search for.
# Everyone is encouraged to contact the author with improvements. Maybe this file
# becomes part of CMake distribution sometimes.
# - Find sqlite3
# Find the native SQLITE3 headers and libraries.
#
# SQLITE3_INCLUDE_DIRS - where to find sqlite3.h, etc.
# SQLITE3_LIBRARIES - List of libraries when using sqlite.
# SQLITE3_FOUND - True if sqlite found.
# Look for the header file.
FIND_PATH(SQLITE3_INCLUDE_DIR NAMES sqlite3.h)
# Look for the library.
FIND_LIBRARY(SQLITE3_LIBRARY NAMES sqlite)
# Handle the QUIETLY and REQUIRED arguments and set SQLITE3_FOUND to TRUE if all listed variables are TRUE.
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SQLITE3 DEFAULT_MSG SQLITE3_LIBRARY SQLITE3_INCLUDE_DIR)
# Copy the results to the output variables.
IF(SQLITE3_FOUND)
SET(SQLITE3_LIBRARIES ${SQLITE3_LIBRARY})
SET(SQLITE3_INCLUDE_DIRS ${SQLITE3_INCLUDE_DIR})
ELSE(SQLITE3_FOUND)
SET(SQLITE3_LIBRARIES)
SET(SQLITE3_INCLUDE_DIRS)
ENDIF(SQLITE3_FOUND)
MARK_AS_ADVANCED(SQLITE3_INCLUDE_DIRS SQLITE3_LIBRARIES)

View file

@ -1,9 +1,9 @@
#include "./database.h"
#include "./Include/simulateFlux.h"
#include "./Include/database.h"
int initializeTrailsTable()
int initializeTrialsTable()
{
sqlite3 *db;
char *err_msg;
int rc;
sqlite3_stmt *stmt;
@ -18,12 +18,12 @@ int initializeTrailsTable()
rc = sqlite3_step(stmt);
rc = sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
int initializeCaptorMetadataTable()
{
sqlite3 *db;
char *err_msg;
int rc;
sqlite3_stmt *stmt;
@ -38,6 +38,7 @@ int initializeCaptorMetadataTable()
rc = sqlite3_step(stmt);
rc = sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
char *generateDataTableName(int trialId, int captorNumber)
@ -56,7 +57,6 @@ char *generateDataTableName(int trialId, int captorNumber)
int createCaptorDataTable(int trialId, int captorNumber)
{
sqlite3 *db;
char *err_msg;
int rc;
sqlite3_stmt *stmt;
@ -81,49 +81,13 @@ int createCaptorDataTable(int trialId, int captorNumber)
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)
{
sqlite3 *db;
sqlite3_stmt *stmt;
char *err_msg;
int rc;
int idx;
rc = sqlite3_open("robotgowest.db", &db);
@ -140,6 +104,7 @@ int insertTrailElement(char *position, int start, double frequency)
rc = sqlite3_step(stmt);
rc = sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
int updateTrailElement(int id, char *position, int start, double frequency)
@ -148,9 +113,7 @@ int updateTrailElement(int id, char *position, int start, double frequency)
sqlite3 *db;
sqlite3_stmt *stmt;
char *err_msg;
int rc;
int idx;
rc = sqlite3_open("robotgowest.db", &db);
@ -169,6 +132,7 @@ int updateTrailElement(int id, char *position, int start, double frequency)
rc = sqlite3_step(stmt);
rc = sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
int insertCaptorMetadataElement(int captorNumber, int trialId)
@ -177,9 +141,7 @@ int insertCaptorMetadataElement(int captorNumber, int trialId)
sqlite3 *db;
sqlite3_stmt *stmt;
char *err_msg;
int rc;
int idx;
rc = sqlite3_open("robotgowest.db", &db);
@ -193,7 +155,9 @@ int insertCaptorMetadataElement(int captorNumber, int trialId)
rc = sqlite3_step(stmt);
rc = sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
int insertCaptorDataElement(int trialId, int captorNumber, double lux, double mean, double power)
@ -204,16 +168,13 @@ int insertCaptorDataElement(int trialId, int captorNumber, double lux, double me
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)
@ -226,19 +187,62 @@ int insertCaptorDataElement(int trialId, int captorNumber, double lux, double me
rc = sqlite3_step(stmt);
rc = sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
int main(void)
int createDb()
{
sqlite3_initialize();
initializeTrailsTable();
// initializeCaptorMetadataTable();
// createCaptorDataTable(12, 8);
// insertTrailElement("test", 1, 3.0);
// insertCaptorMetadataElement(1, 2);
// insertCaptorDataElement(12, 8, 10, 121, 1513);
updateTrailElement(1, "test2", 122, 22);
sqlite3_shutdown();
char *dbPosition = "./robotgowest.db";
if (access(dbPosition, F_OK))
{
initializeTrialsTable();
initializeCaptorMetadataTable();
return 0;
}
else
{
printf("Database already initialized");
return 1;
}
}
int getMaxTrialIdRow()
{
int rc;
sqlite3 *db;
sqlite3_stmt *stmt;
int data = -1;
rc = sqlite3_open("robotgowest.db", &db);
if (rc != SQLITE_OK)
{
return -1;
}
rc = sqlite3_prepare_v2(db, "SELECT MAX(Id) FROM Trials", -1, &stmt, NULL);
if (sqlite3_step(stmt) == SQLITE_ROW)
{
data = sqlite3_column_int(stmt, 0);
}
rc = sqlite3_finalize(stmt);
sqlite3_close(db);
return data;
}
int initiaizeNewTrial(char *position, double frequency, bool *captorOneHot)
{
insertTrailElement(position, -1, frequency);
int trialId = getMaxTrialIdRow();
for (int i = 0; i < 8; i++)
{
if (captorOneHot[i])
{
insertCaptorMetadataElement(i, trialId);
createCaptorDataTable(trialId, i + 1);
}
}
return 0;
}

View file

@ -8,6 +8,7 @@
#include "./Include/average.h"
#include "./Include/growthRate.h"
#include "./Include/getArray.h"
#include "./Include/database.h"
bool rawDataWriteFlag;
int nRowRawData = 5000;
@ -160,6 +161,11 @@ int main(int argc, char **argv)
invTimeBandWidth = 1 / (nRowRawData * period);
firstRawDataQueue = queueCreateEmpty(); // change this for create empty
// launch DB related function
sqlite3_initialize();
createDb(); // Created the Db if not exist
initiaizeNewTrial("test", freqEch, selectionCaptors); // Initialize Trials data and tables
pthread_t rawData;
if (pthread_create(&rawData, NULL, threadSimulateFlux, "threadSimulflux") != 0)
{
@ -182,4 +188,6 @@ int main(int argc, char **argv)
// }
pthread_exit(NULL);
sqlite3_shutdown();
}

View file

@ -152,11 +152,11 @@ bool writeOneRawData(FILE *rawDataFile)
{
if (selectionCaptors[i - 1])
{
quartet value;
value.octet1 = buff[3 * i + 1];
value.octet2 = buff[3 * i + 2];
value.octet3 = buff[3 * i + 3];
value.octet4 = 0;
// quartet value;
// value.octet1 = buff[3 * i + 1];
// value.octet2 = buff[3 * i + 2];
// value.octet3 = buff[3 * i + 3];
// value.octet4 = 0;
valbin[i] = buff[3 * i + 1] * 256 * 256 * 256 + buff[3 * i + 2] * 256 * 256 + buff[3 * i + 3] * 256;
memcpy(&values[i], &valbin[i], sizeof(uint32_t));

View file

@ -1,14 +0,0 @@
#include <sqlite3.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Duplication from simulFlux.c
/**
* @brief convert an interger N into a char*
*
* @param N
* @return char*
*/
char *convertIntegerToChar(int N);

View file

@ -1 +1,3 @@
##This mist include all fetch function and also init and insert function for nn training annd testing result
import numpy

Binary file not shown.

Binary file not shown.