分类: 数据库开发技术
2009-05-08 09:17:58
|
|
|
|
|
|
#include <stdio.h>
#include "sqlite3.h"
int main(){
sqlite3 *db;
char *zErrMsg = NULL;
int rv;
char szSql[128] = { 0 };
sqlite3_stmt *stmt;
rv = sqlite3_open("test.db", &db);
if( rv ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
else
fprintf(stderr, "sqlite3_open OK\n");
//这里查询时,假定数据库中存在表test,并且有一列为COL1(text类型)
strcpy(szSql, "SELECT COL1 FROM test where COL1=?");
rv = sqlite3_prepare(db, szSql, 128, &stmt, NULL);
if( rv != SQLITE_OK ) {
fprintf(stderr, "sqlite3_prepare(%d): %s\n", rv, sqlite3_errmsg(db));
return -1;
}
rv = sqlite3_bind_text(stmt, 1, "VALUE1", strlen("VALUE1"), SQLITE_STATIC);
if( rv != SQLITE_OK ) {
fprintf(stderr, "sqlite3_bind_text(%d): %s\n", rv, sqlite3_errmsg(db));
return -1;
}
rv = sqlite3_step(stmt);
if( (rv != SQLITE_OK) && (rv != SQLITE_DONE) && (rv != SQLITE_ROW) ) {
fprintf(stderr, "sqlite3_step(%d): %s\n", rv, sqlite3_errmsg(db));
return -1;
}
while( rv == SQLITE_ROW ){
fprintf(stderr, "result: %s\n", sqlite3_column_text(stmt,0));
rv = sqlite3_step(stmt);
}
rv = sqlite3_finalize(stmt);
if( rv != SQLITE_OK ) {
fprintf(stderr, "sqlite3_finalize(%d): %s\n", rv, sqlite3_errmsg(db));
return -1;
}
sqlite3_close(db);
return 0;
}