|
|
@@ -0,0 +1,91 @@
|
|
|
+
|
|
|
+/**
|
|
|
+ * Opens the database connection.
|
|
|
+ *
|
|
|
+ * @param[out] db Database object.
|
|
|
+ * @param[in] path Database path, relative to the execution path. Optional.
|
|
|
+ * @return SUCCESS or an error code.
|
|
|
+ */
|
|
|
+int db_open(sqlite3 **db, char *path){
|
|
|
+ printf("IN_DB_OPEN\n");
|
|
|
+
|
|
|
+ // Get the path, relative to the program
|
|
|
+ char db_location[512];
|
|
|
+ strcpy(db_location, executable_location);
|
|
|
+
|
|
|
+ // If db specified as argument, use it, else use the default location.
|
|
|
+ if (path == NULL){
|
|
|
+ strcat(db_location, DEFAULT_DB_PATH);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ strcat(db_location, path);
|
|
|
+ }
|
|
|
+ if (
|
|
|
+ SUCCESS != sqlite3_open_v2(db_location, db, SQLITE_OPEN_READWRITE, NULL)
|
|
|
+ ){
|
|
|
+ fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(*db));
|
|
|
+ sqlite3_close(*db);
|
|
|
+ return ERROR_DB_CANT_OPEN;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return SUCCESS;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Finalizes a database connection.
|
|
|
+ *
|
|
|
+ * @param[in|out] db Connection to close.
|
|
|
+ * @return SUCCESS or an error code.
|
|
|
+ */
|
|
|
+//int db_close(sqlite3 *db){
|
|
|
+// sqlite3_close_v2(db);
|
|
|
+//}
|
|
|
+
|
|
|
+int db_query(sqlite3 **db, sqlite3_stmt **stmt, char query[], char *parameters[]){
|
|
|
+
|
|
|
+
|
|
|
+ // If the connection has not been initialized, do it now
|
|
|
+ if (NULL != *db){
|
|
|
+ db_open(*db, NULL);
|
|
|
+ }
|
|
|
+
|
|
|
+ sqlite3_stmt *stmt2;
|
|
|
+
|
|
|
+ if (SQLITE_OK != sqlite3_prepare_v2(*db, query, -1, stmt, 0)) {
|
|
|
+ fprintf(
|
|
|
+ stderr,
|
|
|
+ "Failed IN QUERY: %s\n",
|
|
|
+ sqlite3_errmsg(db)
|
|
|
+ );
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ sqlite3_close(*db);
|
|
|
+ return ERROR_DB_DELETE_UNITS_TEAMS;
|
|
|
+ }
|
|
|
+ char total_parameters = sqlite3_bind_parameter_count(*stmt);
|
|
|
+ for (int i = 0; i < total_parameters; i ++){
|
|
|
+ sqlite3_bind_text(*stmt, 1, parameters[i], strlen(parameters[i]), NULL);
|
|
|
+ }
|
|
|
+ printf("END QUERY\n");
|
|
|
+ return 0;
|
|
|
+ /*int step = sqlite3_step(stmt);
|
|
|
+ if (step != SQLITE_ROW) {
|
|
|
+ fprintf(stderr, "Unit not found: %s\n", sqlite3_errmsg(db));
|
|
|
+ //sqlite3_finalize(stmt);
|
|
|
+ //sqlite3_close(db);
|
|
|
+ //return ERROR_DB_UNIT_NOF_FOUND;
|
|
|
+ }
|
|
|
+ printf(" QUERY team_name: %s\n", sqlite3_column_text(stmt, 0));*/
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //sqlite3_bind_text(stmt_verify, 1, argv[0], strlen(argv[0]), NULL);
|
|
|
+ //sqlite3_bind_text(stmt_verify, 2, argv[1], strlen(argv[1]), NULL);
|
|
|
+
|
|
|
+
|
|
|
+}
|