SQLite C example: select data

This is a simple example of selecting a column from an SQLite database using the C interface.

The database has one table, called "t", with one column, called "xyz". See SQLite C example: insert data for creating the table and inserting data, as well as the macros in mysqlite.h.

The main program selects whatever is in the table "xyz" and then prints out each row:

#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
#include "mysqlite.h"

int main ()
{
    sqlite3 * db;
    char * sql;
    sqlite3_stmt * stmt;
    int nrecs;
    char * errmsg;
    int i;
    int row = 0;

    CALL_SQLITE (open ("test.db", & db));
    sql = "SELECT * FROM t";
    CALL_SQLITE (prepare_v2 (db, sql, strlen (sql) + 1, & stmt, NULL));

    while (1) {
        int s;

        s = sqlite3_step (stmt);
        if (s == SQLITE_ROW) {
            int bytes;
            const unsigned char * text;
            bytes = sqlite3_column_bytes (stmt, 0);
            text  = sqlite3_column_text (stmt, 0);
            printf ("%d: %s\n", row, text);
            row++;
        }
        else if (s == SQLITE_DONE) {
            break;
        }
        else {
            fprintf (stderr, "Failed.\n");
            exit (1);
        }
    }
    return 0;
}

(download)


Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer