Creating sqlite3 table in C ++

I am experimenting with C ++, recently switched from python; is currently writing a function that creates a table in the sqlite3 database.

It seems that I hit some newbie bugs:

int db_build()
{
   sqlite3 *db;
   int rc; // This line
   int sql; // This line
   rc = sqlite3_open("test.db", &db);

   /* Create SQL statement */
   sql = "CREATE TABLE WORDS("  \
         "ID INT PRIMARY        KEY      NOT NULL," \
         "CURRENT_WORD          TEXT     NOT NULL," \
         "BEFORE_WORD           TEXT     NOT NULL," \
         "AFTER_WORD            TEXT     NOT NULL," \
         "OCCURANCES            INT      NOT NULL);";

   /* Execute SQL statement */
   rc = sqlite3_exec(db, sql);
   sqlite3_close(db);
   return 0;
}

My terminal returns the following:

akf@akf-v5 ~/c/HelloWorld $ g++ main.cpp -l sqlite3
main.cpp: In function ‘int db_build()’:
main.cpp:30:8: error: invalid conversion fromconst char*’ to ‘int’ [-fpermissive]
    sql = "CREATE TABLE WORDS("  \
        ^
main.cpp:38:29: error: invalid conversion fromint’ to ‘const char*’ [-fpermissive]
    rc = sqlite3_exec(db, sql);
                             ^
main.cpp:38:29: error: too few arguments to function ‘int sqlite3_exec(sqlite3*, const char*, int (*)(void*, int, char**, char**), void*, char**)’
In file included from main.cpp:4:0:
/usr/include/sqlite3.h:379:16: note: declared here
 SQLITE_API int sqlite3_exec(
                ^

If I change 'int sql' to 'char sql', I push even more errors. Any idea how to do this?

+4
source share
2 answers

You have one syntax error. Get rid of the back\

   /* Create SQL statement */
   sql = "CREATE TABLE WORDS("  
         "ID INT PRIMARY        KEY      NOT NULL," 
         "CURRENT_WORD          TEXT     NOT NULL," 
         "BEFORE_WORD           TEXT     NOT NULL," 
         "AFTER_WORD            TEXT     NOT NULL," 
         "OCCURANCES            INT      NOT NULL);";

And one python-y error. Change int sql;to:

const char* sql;

A type const char*(usually read as a "pointer to const char") is suitable for pointing to string literals.

Edit:

, Hot Licks , sqlite3_exec :

rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
+6

++, , , "C-".

sqlite3_exec , C , 8- char, . char - char *.

, char * sql;.

, , , sqlite3_exec - ( NULL).

+2

All Articles