csqlite

How do I include SQLite into my C program?


I'm trying to connect my C program using Code Blocks to a database. None of the files that came with amalgamation zip were .lib or .a. SQLite uses libraries but all files are source code files that access those libraries by themselves. How to compile SQLite:

Everything is contained within a single code file, so it is easy to drop into the source tree of a larger C or C++ program.

They mean the sqlite3.c file. But what do they mean by "dropping into the source tree"?

#include <stdio.h>      // printf
#include <sqlite3.h>    // SQLite header (from /usr/include)

int main()
{
    sqlite3 *db;        // database connection
    int rc;             // return code
    char *errmsg;       // pointer to an error string

    /*
     * open SQLite database file test.db
     * use ":memory:" to use an in-memory database
     */
    rc = sqlite3_open(":memory:", &db);
    if (rc != SQLITE_OK) {
        printf("ERROR opening SQLite DB in memory: %s\n", sqlite3_errmsg(db));
        goto out;
    }
    printf("opened SQLite handle successfully.\n");

    /* use the database... */

out:
    /*
     * close SQLite database
     */
    sqlite3_close(db);
    printf("database closed.\n");
}

Solution

  • The amalgation expects that you put all files of the amalgation in you project folder, that you add #include "sqlite3.h" in your sources that want to access sqlite and that you make sqlite3.c a member of your project.

    Assuming a gcc command line compiler, you should do:

    gcc main.c sqlite3.c -lpthread -o foo
    

    No need for a library here, and never try to include sqlite3.c.