sqlite3查询例子

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

#define P3SCAN_DB "/usr/local/conf/as.db"
sqlite3 *p3scan_db = NULL;

int main()
{
        int ret, i, n;
        char *errmsg = NULL, **result = NULL;
        int nrow = 0,ncol = 0;

        if(sqlite3_open(P3SCAN_DB, &p3scan_db))
        {
                sqlite3_free(p3scan_db);
                return -1;
        }

        ret = sqlite3_get_table(p3scan_db,"select blackserverip from blackServer;",&result,&nrow,&ncol,&errmsg);
        sqlite3_free(errmsg);
        if(ret == SQLITE_OK)
        {
                printf("nrow: %d, ncol: %d\n", nrow, ncol);
                if(nrow && result)
                {
                        for(i=1; i<=nrow; i++)
                        {
                                printf("%s\n", result[i*ncol]);
                        }
                }
                sqlite3_free_table(result);
        }
}
编译:
[root@localhost ~]# gcc -o select_blackserverip select_blackserverip.c -lsqlite3
 


posted @ 2013-06-13 21:14  爱生活,爱编程  阅读(392)  评论(0编辑  收藏  举报