Small. Fast. Reliable.
Choose any three.

This information is obsolete. You are looking at the CVSTrac source management system display for SQLite that was replaced by Fossil on 2009-08-11. The information shown here has not been updated since that cut-over. These pages are retained for historical reference only.

This sample code was contributed by Dennis Cote in the sqlite-users mailing list on 2009-03-25 00:23:23

   /** This code is equivalent to the very old callback style  **/
   /** code shown at    **/
   /**                                                         **/
   /** It should provide the same results using the newer      **/
   /** prepare/step/finalize set of calls that are             **/
   /** discussed at         **/
   /**                                                         **/
   /** Hopefully it provides a complete, if somewhat basic,    **/
   /** intro to the use of the preferred C API functions.      **/

   #include <stdio.h>
   #include <sqlite3.h>

   int main(int argc, const char *argv[]){
     sqlite3 *db;
     sqlite3_stmt *stmt;
     int rc = 0;
     int col, cols;

     if( argc!=3 ){
       fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]);
       // open the database file
       rc = sqlite3_open(argv[1], &db);
       if( rc ){
         fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
         // prepare the SQL statement from the command line
         rc = sqlite3_prepare_v2(db, argv[2], -1, &stmt, 0);
         if( rc ){
           fprintf(stderr, "SQL error: %d : %s\n", rc, sqlite3_errmsg(db));
           cols = sqlite3_column_count(stmt);
           // execute the statement
             rc = sqlite3_step(stmt);
             switch( rc ){
               case SQLITE_DONE:
               case SQLITE_ROW:
                 // print results for this row
                 for( col=0; col<cols; col++){
                   const char *txt = (const char*)sqlite3_column_text(stmt,
                   printf("%s = %s\n", sqlite3_column_name(stmt, col), txt
   ? txt : "NULL");
                 fprintf(stderr, "Error: %d : %s\n",  rc, sqlite3_errmsg(db));
           }while( rc==SQLITE_ROW );
           // finalize the statement to release resources
         // close the database file
     return rc!=SQLITE_DONE;