Forum Studiologe
Forum Studiologe

Reputation: 23

c-program sqlite3 check if table exists

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

Upvotes: 1

Views: 1156

Answers (2)

Fitri Halim
Fitri Halim

Reputation: 739

Try this : https://www.sqlite.org/c3ref/table_column_metadata.html

int rc = sqlite3_table_column_metadata(db,NULL,tbl_name,NULL,NULL,NULL,NULL,NULL,NULL);

if (rc != SQLITE_OK )
{
    fprintf(stderr, "Table did not exist \n");
} 
else
{
    fprintf(stdout, "Table exist \n");
}
    

Upvotes: 0

CL.
CL.

Reputation: 180070

The callback is hard to use if you want to do anything but print out the data. In the general case, the only useful way to use sqlite3_exec() is to replace it with sqlite3_prepare_v2()/sqlite3_bind_*()/sqlite3_step()/sqlite3_finalize() calls so that you can read the data in the same place where you actually need to handle it:

sqlite3_stmt *stmt;
const char *sql = "SELECT 1 FROM sqlite_master where type='table' and name=?";

int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
    print("error: ", sqlite3_errmsg(db));
    return;
}
sqlite3_bind_text(stmt, 1, tbl_name, -1, SQLITE_TRANSIENT);

rc = sqlite3_step(stmt);
bool found;
if (rc == SQLITE_ROW)
    found = true;
else if (rc == SQLITE_DONE)
    found = false;
else {
    print("error: ", sqlite3_errmsg(db));
    sqlite3_finalize(stmt);
    return;
}

sqlite3_finalize(stmt);
return found;

Upvotes: 1

Related Questions