Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an sqlite3 table in c++

Tags:

c++

sqlite

I'm experimenting with C++, having recently moved across from python; currently writing a function that builds a table in an sqlite3 database.

I seem to be hitting some newbie errors:

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 from ‘const char*’ to ‘int’ [-fpermissive]
    sql = "CREATE TABLE WORDS("  \
        ^
main.cpp:38:29: error: invalid conversion from ‘int’ 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 hit even more errors. Any idea how to get this thing going?

like image 330
ES-AKF Avatar asked Jan 25 '15 03:01

ES-AKF


People also ask

Is SQLite written in C?

SQLite is written in ANSI-C. The C programming language is used because it is the universal assembly language - it can run on just about any hardware and on just about any operating system. No matter what programming language is used for the application code, it can usually interface easily with a library written in C.

Is sqlite3 faster than MySQL?

SQLite 2.7. 6 is often faster (sometimes more than twice as fast) than MySQL 3.23. 41 for most common operations. SQLite does not execute CREATE INDEX or DROP TABLE as fast as the other databases.

Is sqlite3 better than MySQL?

SQLite3 is much faster than MySQL database. It's because file database is always faster than unix socket.


2 Answers

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

/* 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[];

The type const char sql[] is appropriate for constant string literals.

Edit:

For completeness, Hot Licks also hints that your call to sqlite3_exec must be:

rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
like image 81
Drew Dormann Avatar answered Oct 06 '22 00:10

Drew Dormann


If you're going to code in C++ you'd better very quickly learn what a pointer is, and what a "C string" is.

sqlite3_exec expects a C string as the second argument, which is a sequence of 8-bit char values terminated with a byte of zeros. It is passed via a pointer-to-char -- char *.

So your declaration needs to be char * sql;.

Also, if you look at the documentation you will see that sqlite3_exec has several more parameters -- they cannot be omitted (though they can be passed as NULL).

like image 43
Hot Licks Avatar answered Oct 06 '22 00:10

Hot Licks