I'm trying to write some code that initializes an sql database and table with the name of the database & table set through preprocessor commands and if not set, defaulting to the values given below.
I'm using a macro to stringify whatever name is given. The sqlite3_open correctly opens / creates the database with the given name or the default
sqlite3_open(TO_STRING(DB_NAME), &db);
For the creation of the table I'm relying on sprintf.
sprintf(str,"CREATE TABLE %s;", TO_STRING(TABLE_NAME));
sql = str;
When inspecting my code in debugging, pointer sql seems to be having the correct value of "CREATE TABLE SensorData;". However when I try to execute my code I get the error "SQL error: near ";": syntax error". Upon inspection it is clear that the error code 110 is returned by the function, I can't seem to find any information on this code however.
I can't seem to figure out what the exact problem is in this case, as the string resulting in the array str seems to be exactly the same as when I create the table in a static way.
This is all my code relevant to the problem.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#define REAL_TO_STRING(s) #s
#define TO_STRING(s) REAL_TO_STRING(s)
#ifndef DB_NAME
#define DB_NAME Sensor.db
#endif
#ifndef TABLE_NAME
#define TABLE_NAME SensorData
#endif
#define DBCONN sqlite3
int main()
{
char *err_msg = 0;
DBCONN * db;
int ok = sqlite3_open(TO_STRING(DB_NAME), &db);
if(ok != SQLITE_OK)
{
fprintf(stderr,"Cannot open database: %s\n",sqlite3_errmsg(db));
sqlite3_close(db);
return 0;
}
char * sql;
char str[100];
//clear array
memset(&str[0],0,100);
sprintf(str,"CREATE TABLE %s;", TO_STRING(TABLE_NAME));
sql = str;
ok = sqlite3_exec(db, sql, 0, 0, &err_msg);
if (ok != SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", err_msg);
sqlite3_free(err_msg);
sqlite3_close(db);
return 0;
}
sqlite3_close(db);
return 0;
}