Skip to content

Instantly share code, notes, and snippets.

@tnewman
Last active December 16, 2015 14:28
Show Gist options
  • Save tnewman/5448476 to your computer and use it in GitHub Desktop.
Save tnewman/5448476 to your computer and use it in GitHub Desktop.
Basic SELECT of an SQLite database using prepared statements.
#include <stdio.h>
#include "sqlite3.h"
int main()
{
const char filename[] = "Atm.db";
sqlite3 *dbHandle;
const char sql[] = "SELECT * FROM User;";
sqlite3_stmt *ppStmt;
if(sqlite3_open_v2(filename, &dbHandle, SQLITE_OPEN_READWRITE, 0) == SQLITE_OK)
{
if(sqlite3_prepare_v2(dbHandle, sql, sizeof(sql), &ppStmt, 0) == SQLITE_OK)
{
while(sqlite3_step(ppStmt) == SQLITE_ROW)
{
printf("%d\n", sqlite3_column_int(ppStmt, 0));
}
sqlite3_finalize(ppStmt);
}
else
{
printf("Failed to prepare statement.\n");
}
sqlite3_close_v2(dbHandle);
}
else
{
printf("Failed to open DB.\n");
}
printf("Press enter to continue...");
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment