SQLite函数应用(二)

callback函数

sqlite3* db;

int rc;

char* zErr;

rc = sqlite3_open("test.db, &db);

if(rc) cout<<"ERROR"<<endl;

char * data = "CallBack";

char * sql = "select * from test";

rc = sqlite3_exec( db, sql, callback, data, &zErr );

if( rc != SQLITE_OK) cout<<"ERROR"<<endl;

sqlite3_close(db);


int callback(void* data, int ncols, char**values, char** headers)

{

      for( int i=0; i<ncols; i++)

            cout << headers[i] <<" --> "<< values[i] <<endl;

}

对sqlite3_exec执行结果中查询到的每条记录应用callback函数

每条记录的相应字段值存放于values数组,表头存放于headers数组,可以完成相应数据处理

sqlite3_get_table查询

char ** result;

int nrows, ncols;

char * zErr;

char * sql = "select * from test;";

int rc = sqlite3_get_table( db, sql, &result, &nrows, &ncols,  &zErr );

cout<<"行数: "<< nrows<<endl;

cout<<"列数: "<< ncols<<endl;

for( int i = 0; i <= nrows; i++ )

{

      for( int j = 0; j < ncols; j++ )

            cout<< result[i*ncols+j] <<"\t";

      cout<<endl;

}

sqlite3_free_table(result);

sqlite3_get_table 将查询得到的结果全部存入result数组,并可得到行数和列数

注意,第一行是表头


预处理查询

   int rc;

   sqlite3 *db;

   sqlite3_stmt *stmt;

   char *sql = "select * from episodes;";

   const char *tail;

   rc = sqlite3_open("test.db", &db);

   rc = sqlite3_prepare(db, sql, (int)strlen(sql), &stmt, &tail);

   rc = sqlite3_step(stmt);

   int  ncols = sqlite3_column_count(stmt);

   while(rc == SQLITE_ROW)       //sqlite3_step() has another row ready   #define SQLITE_ROW  100

   {

        for( int i=0; i < ncols; i++ ) 

              cout <<sqlite3_column_text(stmt, i);       

        cout << endl ;

        rc =sqlite3_step(stmt); 

    }

   sqlite3_finalize(stmt);

   sqlite3_close(db);


sqlite3_prepare()也能接受一个包括多个SQL语句的字符串,但是只处理第一个SQL

若想处理多个,可应用tail参数,如下   

 while( sqlite3_complete(sql) )

 {

      rc = sqlite3_prepare(db, sql, strlen(sql), &stmt, &tail);

      sql = tail;

      ...........

 } 

取字段信息

sqlite3_stmt * : statement handle

int iConl :  列号

const char *sqlite3_column_name( sqlite3_stmt*, int iCol );        //获取字段名称

const char *sqlite3_database_name( sqlite3_stmt*, int iCol );     //获取数据库名称

const char *sqlite3_table_name( sqlite3_stmt*, int iCol );           //获取表的名称


intsqlite3_column_type( sqlite3_stmt*, int iCol );          //SQLite本身的类型,或称存储类

返回值说明 

1:SQLITE_INTEGER  

2:SQLITE_FLOAT  

3:SQLITE_TEXT  

4:SQLITE_BLOB  

5:SQLITE_NULL


const char *sqlite3_column_decltype( sqlite3_stmt*, int iCol );   //字段声明时的类型

如果结果集中的一列不是来自一个实际的字段(如来自于表达式、函数或聚合的结果),这个函数将返回NULL


sqlite3_column_xxx()函数取当前记录中每个字段的值

xxx表示你希望得到的数据类型,包括以下函数:

int sqlite3_column_int(sqlite3_stmt*, intiCol);

double sqlite3_column_double(sqlite3_stmt*,int iCol);

long long int sqlite3_column_int64(sqlite3_stmt*,int iCol);

const void*sqlite3_column_blob(sqlite3_stmt*, int iCol);

const unsigned char*sqlite3_column_text(sqlite3_stmt*, int iCol);

const void*sqlite3_column_text16(sqlite3_stmt*, int iCol);


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值