简体   繁体   中英

C, how to transfer sqlite3 database handle

I'm trying to make some utils for using with sqlite databases. For that I create sqlite3_ut.h and sqlite3_ut.c files

sqlite_ut.h

#ifndef sqlite3_ut_h
#define sqlite3_ut_h

#include <stdio.h>
#include "sqlite3.h"

int drop_table(sqlite3 handle);

#endif

sqlite_ut.c

int drop_table(sqlite3 handle)
{
int dropped = 0;
printf("Begin Drop\n");

sqlite3_exec(handle, "BEGIN;", NULL, NULL, NULL);
sqlite3_stmt *droptab;
if (sqlite3_prepare_v2(handle, "DROP TABLE mytable;", -1, &droptab, 0) != SQLITE_OK)
    printf("db error: %s\n", sqlite3_errmsg(handle));

if(droptab)
{
    sqlite3_step(droptab);
    dropped = 1;
}
else
    printf("Error: drop_table");

sqlite3_finalize(droptab);
sqlite3_exec(handle, "COMMIT;", NULL, NULL, NULL);

printf("End Drop\n");
return dropped;
}

sqlite_ut.h is included in main file.

sqlite3 *db;

int rc = sqlite3_open("m_test.db", &db);
if (rc)...

//error here
int dropped = drop_table(db);

Obviously, I can't transfer handle of opened database properly to drop_table function which is of sqlite3 type.

How to do that with suggested program cofiguration?

SQLite3 handles are of type sqlite3 * , not sqlite3 . Redefine drop_table as follows:

int drop_table(sqlite3 *handle) { … }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM