Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read data from SQLite database?

Tags:

I decided to use SQLite as it allows to store database into a single file. I think I have managed to do a database with SQLite Database Browser.

How can I read that data in a C/C++ program?

like image 396
zaplec Avatar asked Oct 18 '10 07:10

zaplec


People also ask

How do I retrieve data from SQLite database in Excel?

Click and enable the “Table” and “Existing Worksheet” options in the Import Data window. Click an empty cell on the Excel spreadsheet where you want the data table from the SQLite database to appear. Click the “OK” button.

How can I retrieve single data from SQLite database in Android?

We can retrieve anything from database using an object of the Cursor class. We will call a method of this class called rawQuery and it will return a resultset with the cursor pointing to the table. We can move the cursor forward and retrieve the data. This method return the total number of columns of the table.


1 Answers

A example using sqlite read:

#include <stdio.h> #include <sqlite3.h> #include <string.h>   int main(int argc, char** argv) {     const char*          username = "satyam";     char                 q[999];     sqlite3*             db;     sqlite3_stmt*        stmt;     int                  row = 0;     int                  bytes;     const unsigned char* text;      if (2 == argc) {         username = argv[1];     }      q[sizeof q - 1] = '\0';     snprintf(         q,         sizeof q - 1,         "SELECT ipaddr FROM items WHERE username = '%s'",         username     );      if (sqlite3_open ("test.db", &db) != SQLITE_OK) {         fprintf(stderr, "Error opening database.\n");         return 2;     }      printf("Query: %s\n", q);      sqlite3_prepare(db, q, sizeof q, &stmt, NULL);      bool done = false;     while (!done) {         printf("In select while\n");         switch (sqlite3_step (stmt)) {         case SQLITE_ROW:             bytes = sqlite3_column_bytes(stmt, 0);             text  = sqlite3_column_text(stmt, 1);             printf ("count %d: %s (%d bytes)\n", row, text, bytes);             row++;             break;          case SQLITE_DONE:             done = true;             break;          default:             fprintf(stderr, "Failed.\n");             return 1;         }     }      sqlite3_finalize(stmt);      return 0; } 
like image 81
Satyam Avatar answered Oct 20 '22 18:10

Satyam