Created
April 25, 2021 19:43
-
-
Save wd5gnr/7f6e49426838ebb1c75b1ade1d8fc2ea to your computer and use it in GitHub Desktop.
Simple SQLite example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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