Skip to content

Instantly share code, notes, and snippets.

@tonyrang
Forked from jsok/sqlite3_test.c
Created June 30, 2020 10:17
Show Gist options
  • Select an option

  • Save tonyrang/6e961c888a91b3c2b0b6f1f8b8305b8c to your computer and use it in GitHub Desktop.

Select an option

Save tonyrang/6e961c888a91b3c2b0b6f1f8b8305b8c to your computer and use it in GitHub Desktop.
sqlite3 C example
#include <stdio.h>
#include <sqlite3.h>
int main(void)
{
sqlite3 *db;
sqlite3_stmt *stmt;
sqlite3_open("expenses.db", &db);
if (db == NULL)
{
printf("Failed to open DB\n");
return 1;
}
printf("Performing query...\n");
sqlite3_prepare_v2(db, "select * from expenses", -1, &stmt, NULL);
printf("Got results:\n");
while (sqlite3_step(stmt) != SQLITE_DONE) {
int i;
int num_cols = sqlite3_column_count(stmt);
for (i = 0; i < num_cols; i++)
{
switch (sqlite3_column_type(stmt, i))
{
case (SQLITE3_TEXT):
printf("%s, ", sqlite3_column_text(stmt, i));
break;
case (SQLITE_INTEGER):
printf("%d, ", sqlite3_column_int(stmt, i));
break;
case (SQLITE_FLOAT):
printf("%g, ", sqlite3_column_double(stmt, i));
break;
default:
break;
}
}
printf("\n");
}
sqlite3_finalize(stmt);
sqlite3_close(db);
getc(stdin);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment