Demo C program on how to use sqlite3
This commit is contained in:
parent
d750f55e87
commit
cc5b27a3ab
1 changed files with 46 additions and 0 deletions
46
sqlite.c
Normal file
46
sqlite.c
Normal file
|
@ -0,0 +1,46 @@
|
|||
#include <sqlite3.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
* See: https://zetcode.com/db/sqlitec/
|
||||
* Build with:
|
||||
* gcc -o version version.c -lsqlite3 -std=c99
|
||||
*/
|
||||
|
||||
int main(void) {
|
||||
printf("%s\n", sqlite3_libversion());
|
||||
|
||||
sqlite3 *db;
|
||||
sqlite3_stmt *res;
|
||||
|
||||
int rc = sqlite3_open(":memory:", &db);
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
|
||||
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
|
||||
sqlite3_close(db);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
rc = sqlite3_prepare_v2(db, "SELECT SQLITE_VERSION()", -1, &res, 0);
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
|
||||
fprintf(stderr, "Failed to fetch data: %s\n", sqlite3_errmsg(db));
|
||||
sqlite3_close(db);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
rc = sqlite3_step(res);
|
||||
|
||||
if (rc == SQLITE_ROW) {
|
||||
printf("%s\n", sqlite3_column_text(res, 0));
|
||||
}
|
||||
|
||||
sqlite3_finalize(res);
|
||||
sqlite3_close(db);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Reference in a new issue