29 filepath(path), db(nullptr), stmt(nullptr), database_open(true)
32 std::filesystem::remove(filepath);
35 int error_code = sqlite3_open(filepath.c_str(), &db);
36 if (error_code != SQLITE_OK)
38 ARMARX_ERROR_S <<
"Error opening database: " << sqlite3_errmsg(db);
41 database_open =
false;
46 char* zErrMsg =
nullptr;
47 std::string sql_create_table =
"CREATE TABLE TopicData(" \
48 "ID INTEGER PRIMARY KEY," \
51 "OPERATIONNAME TEXT," \
53 error_code = sqlite3_exec(db, sql_create_table.c_str(),
nullptr,
nullptr, &zErrMsg);
54 if (error_code != SQLITE_OK)
60 sqlite3_free(zErrMsg);
62 database_open =
false;
65 sqlite3_free(zErrMsg);
69 sql_create_table =
"CREATE TABLE Topics(TOPIC TEXT);";
70 error_code = sqlite3_exec(db, sql_create_table.c_str(),
nullptr,
nullptr, &zErrMsg);
71 if (error_code != SQLITE_OK)
77 sqlite3_free(zErrMsg);
79 database_open =
false;
82 sqlite3_free(zErrMsg);
85 std::string sql_insert_into =
"INSERT INTO TopicData (ID, TIME, TOPIC, OPERATIONNAME, DATA) VALUES (?, ?, ?, ?, ?);";
86 error_code = sqlite3_prepare_v2(db, sql_insert_into.c_str(), -1, &stmt,
nullptr);
87 if (error_code != SQLITE_OK)
89 ARMARX_ERROR_S <<
"Can not prepare sql statement: " << sqlite3_errmsg(db);
91 sqlite3_finalize(stmt);
93 database_open =
false;
99 error_code = sqlite3_exec(db,
"BEGIN TRANSACTION",
nullptr,
nullptr, &zErrMsg);
100 if (error_code != SQLITE_OK)
106 sqlite3_free(zErrMsg);
108 database_open =
false;
111 sqlite3_free(zErrMsg);
121 char* zErrMsg =
nullptr;
122 int error_code = sqlite3_exec(db,
"END TRANSACTION",
nullptr,
nullptr, &zErrMsg);
123 if (error_code != SQLITE_OK)
130 sqlite3_free(zErrMsg);
133 sqlite3_finalize(stmt);
157 char* zErrMsg =
nullptr;
161 std::string sql_insert_table =
"INSERT INTO Topics VALUES ('" + topicData.
topicName +
"');";
162 int error_code = sqlite3_exec(db, sql_insert_table.c_str(),
nullptr,
nullptr, &zErrMsg);
163 if (error_code != SQLITE_OK)
169 sqlite3_free(zErrMsg);
170 sqlite3_finalize(stmt);
172 database_open =
false;
175 sqlite3_free(zErrMsg);
184 int error_code_ID = sqlite3_bind_null(stmt, 1);
185 int error_code_time = sqlite3_bind_int64(stmt, 2, (sqlite3_int64) topicData.
timestamp.toMicroSeconds());
186 int error_code_topic = sqlite3_bind_text(stmt, 3, topicData.
topicName.c_str(), -1, SQLITE_STATIC);
187 int error_code_operation = sqlite3_bind_text(stmt, 4, topicData.
operationName.c_str(), -1, SQLITE_STATIC);
188 int error_code_data = sqlite3_bind_blob(stmt, 5, topicData.
inParams.data(), topicData.
inParams.size(), SQLITE_STATIC);
190 if (error_code_ID != SQLITE_OK || error_code_time != SQLITE_OK || error_code_topic != SQLITE_OK || error_code_operation != SQLITE_OK || error_code_data != SQLITE_OK)
193 sqlite3_finalize(stmt);
195 database_open =
false;
200 int error_code_step = sqlite3_step(stmt);
201 if (error_code_step != SQLITE_DONE)
203 ARMARX_ERROR_S <<
"Can not execute sql statement: " << sqlite3_errmsg(db);
204 sqlite3_finalize(stmt);
206 database_open =
false;
214 sqlite3_clear_bindings(stmt);