Skip to content

Instantly share code, notes, and snippets.

@wd5gnr
Created April 25, 2021 19:43
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wd5gnr/7f6e49426838ebb1c75b1ade1d8fc2ea to your computer and use it in GitHub Desktop.
Save wd5gnr/7f6e49426838ebb1c75b1ade1d8fc2ea to your computer and use it in GitHub Desktop.
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