-
-
Save tonyrang/6e961c888a91b3c2b0b6f1f8b8305b8c to your computer and use it in GitHub Desktop.
sqlite3 C example
This file contains hidden or 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 <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