Skip to content

Instantly share code, notes, and snippets.

@wd5gnr

wd5gnr/main.c

Created Apr 25, 2021
Embed
What would you like to do?
Simple SQLite example
#include <sqlite3.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
sqlite3 *db;
sqlite3_stmt *sql;
int rv;
rv=sqlite3_open("parts.db",&db);
if (rv!=SQLITE_OK)
{
fprintf(stderr,"Can't open database: %s!",sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
rv=sqlite3_prepare_v2(db, "SELECT * from full", -1, &sql, NULL);
if (rv!=SQLITE_OK)
{
fprintf(stderr,"Prepare failed: %s\n",sqlite3_errmsg(db));
sqlite3_close(db);
return 2;
}
do
{
rv=sqlite3_step(sql);
if (rv==SQLITE_ROW)
{
printf("%s,",sqlite3_column_text(sql,0));
printf("%s\n",sqlite3_column_text(sql,2));
}
}
while (rv==SQLITE_ROW);
sqlite3_finalize(sql);
sqlite3_close(db);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment