On 10/26/05, Alfredo Cole <[EMAIL PROTECTED]> wrote:
> I have searched, but have not found samples of actual uses of sqlite_compile,
> sqlite_step and sqlite_finalize. Could anybody point the way to where I can
> find samples with source code to use these functions?

It's c++ but here's some:

    sqlite3*      dbInput = NULL;
    sqlite3*      dbOutput = NULL;

        // get database names
        string dbNameInput = argv[1];
        string dbNameOutput = dbNameInput + ".backup";

        sqlite3_stmt *pStmt;
        int           rc;
        bool          Loop;

        // a list of tables
        vector<table> tables;

        //--------------------------------------------------
        // Get a list of every table in the source database
        //--------------------------------------------------
        string sql = "SELECT name, sql"
                     " FROM sqlite_master"
                     " WHERE type = 'table'";

        // connect to database
        rc = sqlite3_open( dbNameInput.c_str(), &dbInput );
        if ( rc )
          throw "Can't open source database";

        if ( sqlite3_prepare( dbInput, sql.c_str(), sql.size(),
&pStmt, NULL ) != SQLITE_OK )
          throw "Cannot prepare sql";

        Loop = true;
        while ( Loop )
          switch ( sqlite3_step( pStmt ) )
            {
              case SQLITE_ROW:
                {
                  char*         p;
                  string        Name;

                  // get results ( 0 based index!!! )
                  p = (char *) sqlite3_column_text( pStmt, 0 );
                  if ( ! p )
                    throw "blank table names in source database";
                  Name = p;

                  p = (char *) sqlite3_column_text( pStmt, 1 );
                  if ( ! p )
                    throw "blank sql in source database";
                  sql = p;

                  // copy table to list
                  tables.push_back( table( Name, sql ) );
                }
                break;
              case SQLITE_DONE:
                Loop = false;
                break;
              default:
                throw "Cannot execute sql";
                break;
            }

        // clean up when finished
        sqlite3_finalize( pStmt );
        sqlite3_close( dbInput );
        dbInput = NULL;

Reply via email to