On Nov 29, 2007 12:03 PM, Wilson, Ron <[EMAIL PROTECTED]> wrote:
>
> http://www.sqlite.org/quickstart.html
>
> This page still shows the old callback method for usign sqlite3_exec().
> I actually haven't used SQlite in quite a long time, but judging from
> recent list topics, this is no longer the preferred method. So where do
> I point my friend for using sqlite3_prepare_v2() etc.? I've seen a few
> posts (long since deleted from my inbox) that had basic outlines. Could
> someone please post a basic code snipit for open/query/close using the
> newer interface, i.e. avoiding the callback usage? Perhaps the
> quickstart guide (above) could use an update as well?
>
> Ron,
I believe the following code is equivalent to the existing quickstart code,
but it uses the newer APIs. This simple program will execute arbitrary SQL
on a database file. If the SQL is a query it will display all the results
returned.
#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]);
}else{
// open database
rc = sqlite3_open(argv[1], &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
}else{
// prepare statement
rc = sqlite3_prepare_v2(db, argv[2], -1, &stmt, 0);
if( rc ){
fprintf(stderr, "SQL error: %d : %s\n", rc, sqlite3_errmsg(db));
}else{
cols = sqlite3_column_count(stmt);
// execute the statement
do{
rc = sqlite3_step(stmt);
switch( rc ){
case SQLITE_DONE:
break;
case SQLITE_ROW:
// print results for this row
for( col=0; col<cols; col++){
const char *txt = (const char*)sqlite3_column_text(stmt,
col);
printf("%s = %s\n", sqlite3_column_name(stmt, col), txt ?
txt : "NULL");
}
break;
default:
fprintf(stderr, "Error: %d : %s\n", rc, sqlite3_errmsg(db));
break;
}
}while( rc==SQLITE_ROW );
sqlite3_finalize(stmt);
}
sqlite3_close(db);
}
}
return rc!=SQLITE_DONE;
}
HTH
Dennis Cote