遍历整个表,并把查询结果输出:
int main(int argc, char **argv)
{
int rc, i, id, cid;
char *name;
char *sql;
char *zErr;
sqlite3 *db; sqlite3_stmt *stmt;
sql="select id,name,cid from episodes";
//打开数据库
sqlite3_open("test.db", &db);
//编译sql语句
sqlite3_prepare(db, sql, strlen(sql), &stmt, NULL);
//调用VM,执行VDBE程序
rc = sqlite3_step(stmt);
while(rc == SQLITE_ROW) {
id = sqlite3_column_int(stmt, 0);
name = (char *)sqlite3_column_text(stmt, 1);
cid = sqlite3_column_int(stmt, 2);
if(name != NULL){
fprintf(stderr, "Row: id=%i, cid=%i, name='%s'\n", id,cid,name);
} else {
/* Field is NULL */
fprintf(stderr, "Row: id=%i, cid=%i, name=NULL\n", id,cid);
}
rc = sqlite3_step(stmt);
}
//释放资源
sqlite3_finalize(stmt);
//关闭数据库
sqlite3_close(db);
return 0;
}