I've been trying to write a function that takes a name as parameter and checks a database to see if there exists a table with that name, but for some reason it keeps failing. Maybe someone can push me in the right direction here. Thanks!!!
Here's the functions i have:
int check_table(char tbl_name[])
{
sqlite3 *db;
char *err_msg = 0;
char sql_query[1024];
printf("checking for: %s\n", tbl_name);
int rc = sqlite3_open(db_name, &db);
if (rc != SQLITE_OK)
{
fprintf(stderr, "Cannot open database: %s\n",
sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
// assemble string
snprintf(sql_query,sizeof(sql_query), "SELECT name FROM sqlite_master WHERE type='table' AND name=\'%s\'", tbl_name);
rc = sqlite3_exec(db, sql_query, callbackcheck, 0, &err_msg);
if (rc != SQLITE_OK )
{
fprintf(stderr, "Failed to select data\n");
fprintf(stderr, "SQL error: %s\n", err_msg);
sqlite3_free(err_msg);
sqlite3_close(db);
return 1;
}
sqlite3_close(db);
// needs some work here
return 1; // table does exists
//return 0; // table does not exists
}
int callbackcheck(void *NotUsed, int argc, char **argv, char **azColName)
{
NotUsed = 0;
printf("argc: %s - argv: %s - azColName: %s", argc, argv, azColName);
for (int i = 0; i < argc; i++)
{
printf("%s\n", argv[i] ? argv[i] : "NULL");
}
return 0;
}
My problem lies in how to get a True/False returned, so ideally i would call the function like so: bool tb_is_there = check_table("some_tbl"); then I can return tb_is_there;
I hope that makes sense