zoukankan      html  css  js  c++  java
  • 数据库 sqlite3_get_table,sqlite3_free_table

    sqlite入门基础(二):sqlite3_get_table,sqlite3_free_table

     http://www.cnblogs.com/likebeta/archive/2012/06/17/2552784.html
     

    上一篇介绍的sqlite3_exec 是使用回调来执行对select结果的操作。还有一个方法可以直接查询而不需要回调。但是,我个人感觉还是回调好,因为代码可以更加整齐,只不过用回调很麻烦,你得声明一个函数,如果这个函数是类成员函数,你还不得不把它声明成static的(要问为什么?这又是C++基础了。C++成员函数实际上隐藏了一个参数:this,C++调用类的成员函数的时候,隐含把类指针当成函数的第一个参数传递进去。结果,这造成跟前面说的sqlite 回调函数的参数不相符。只有当把成员函数声明成static 时,它才没有多余的隐含的this参数)。
    虽然回调显得代码整齐,但有时候你还是想要非回调的select 查询。这可以通过sqlite3_get_table 函数做到。

    int sqlite3_get_table(
      sqlite3 *db,          /* An open database */
      const char *zSql,     /* SQL to be evaluated */
      char ***pazResult,    /* Results of the query */
      int *pnRow,           /* Number of result rows written here */
      int *pnColumn,        /* Number of result columns written here */
      char **pzErrmsg       /* Error msg written here */
    );
    void sqlite3_free_table(char **result);

    第1个参数不再多说,看前面的例子。
    第2个参数是sql 语句,跟sqlite3_exec 里的sql 是一样的。是一个很普通的以结尾的char*字符串。
    第3个参数是查询结果,它依然一维数组(不要以为是二维数组,更不要以为是三维数组)。它内存布局是:字段名称,后面是紧接着是每个字段的值。下面用例子来说事。
    第4个参数是查询出多少条记录(即查出多少行,不包括字段名那行)。
    第5个参数是多少个字段(多少列)。
    第6个参数是错误信息,跟前面一样,这里不多说了。

    pazResult返回的字符串数量实际上是(*pnRow+1)*(*pnColumn),因为前(*pnColumn)个是字段名

    修改上篇的例子,使用sqlite3_get_table,来去的结果集:

    #include <iostream>
    using namespace std;
    #include "sqlite/sqlite3.h"
    int callback(void*,int,char**,char**);
    int main()
    {
        sqlite3* db;
        int nResult = sqlite3_open("test.db",&db);
        if (nResult != SQLITE_OK)
        {
            cout<<"打开数据库失败:"<<sqlite3_errmsg(db)<<endl;
            return 0;
        }
        else
        {
            cout<<"数据库打开成功"<<endl;
        }
    
        char* errmsg;
    
        nResult = sqlite3_exec(db,"create table fuck(id integer primary key autoincrement,name varchar(100))",NULL,NULL,&errmsg);
         if (nResult != SQLITE_OK)
         {
             sqlite3_close(db);
             cout<<errmsg;
             sqlite3_free(errmsg);
            return 0;
        }
        string strSql;
        strSql+="begin;
    ";
        for (int i=0;i<100;i++)
        {
            strSql+="insert into fuck values(null,'heh');
    ";
        }
        strSql+="commit;";
        //cout<<strSql<<endl;
    
        nResult = sqlite3_exec(db,strSql.c_str(),NULL,NULL,&errmsg);
    
        if (nResult != SQLITE_OK)
        {
            sqlite3_close(db);
            cout<<errmsg<<endl;
            sqlite3_free(errmsg);
            return 0;
        }
    
        strSql = "select * from fuck";
        //nResult = sqlite3_exec(db,strSql.c_str(),callback,NULL,&errmsg);
        char** pResult;
        int nRow;
        int nCol;
        nResult = sqlite3_get_table(db,strSql.c_str(),&pResult,&nRow,&nCol,&errmsg);
          if (nResult != SQLITE_OK)
        {
            sqlite3_close(db);
            cout<<errmsg<<endl;
            sqlite3_free(errmsg);
            return 0;
        }
    
        string strOut;
        int nIndex = nCol;
        for(int i=0;i<nRow;i++)
        {
            for(int j=0;j<nCol;j++)
            {
                strOut+=pResult[j];
                strOut+=":";
                strOut+=pResult[nIndex];
                strOut+="
    ";
                ++nIndex;
            }
        }
        sqlite3_free_table(pResult);
        cout<<strOut<<endl;
        sqlite3_close(db);
        return 0;
    }
    /*
    int callback(void* ,int nCount,char** pValue,char** pName)
    {
        string s;
        for(int i=0;i<nCount;i++)
        {
            s+=pName[i];
            s+=":";
            s+=pValue[i];
            s+="
    ";
        }
        cout<<s<<endl;
        return 0;
    }*/
  • 相关阅读:
    nose测试中修改nose_html_reporting插件,使生成的html报告加入显示截图功能
    python selenium中等待元素出现及等待元素消失操作
    在python pydev中使用todo标注任务
    云存储命令行工具---libs3
    关于qt creator各种假死的问题
    小端存储转大端存储 & 大端存储转小端存储
    C++判断计算式是大端存储模式,还是小端存储模式
    请教网友:#pragma pack(push) #pragma pack(pop)无效
    sizeof 计算 struct 占字节数的方法总结
    const 变量在多个文件共享,如何验证两种不同的方式下,编译器是否会在多个文件下建立多个副本
  • 原文地址:https://www.cnblogs.com/vipwtl/p/5916969.html
Copyright © 2011-2022 走看看