I'm having trouble writing records to my log table that has an FK from the client table. The error is in the insert_log () function
SQL to create tables
sql_create_client = "CREATE TABLE cliente (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, user_id TEXT NOT NULL, birthday TEXT NOT NULL, link TEXT NOT NULL, name TEXT NOT NULL, location TEXT NOT NULL, gender TEXT NOT NULL, email TEXT NOT NULL);";
sql_create_log = "CREATE TABLE log (id INTEGER NOT NULL PRIMARY KEY, created_at TEXT NOT NULL, fk_id INTEGER NOT NULL, FOREIGN KEY (fk_id) REFERENCES cliente(id));";
Inserting Log
int insert_log(char user_id[30]) {
int fk_id = user_exists(user_id);
const char *created_at;
sql_insert_log = "PRAGMA foreign_keys = 1; INSERT INTO log VALUES (?,?,?);";
sql_query_log_datatime = "SELECT datetime('now', 'localtime');";
if (connect_db() == 0) {
sqlite3_prepare_v2(conn, sql_query_log_datatime, -1, &stmt, 0);
if (sqlite3_step(stmt) == SQLITE_ROW) {
created_at = sqlite3_column_text(stmt, 0);
fprintf(stdout, "Result datetime: %s\n",created_at);
sqlite3_prepare_v2(conn, sql_insert_log, -1, &stmt, NULL);
sqlite3_bind_null(stmt,1);
sqlite3_bind_text(stmt, 2, created_at, -1, SQLITE_STATIC);
sqlite3_bind_int(stmt, 3, fk_id);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
close_db();
return 0;
} else return 1;
} else {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(conn));
close_db();
return 1;
}
}
Details: I'm using Ubuntu and gcc. All client table queries work perfectly.
Part of the error was resolved, I had a segmentation fault, and I took the part that caused it:
Falha de segmentação (imagem do núcleo gravada)
O que causou: sqlite3_reset(stmt);