sqlite回传函数相关说了3种基本方法:callback,gettable和预处理stmt方法
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include"sqlite3.h"
- //
- //typedefint(*sqlite3_callback)(
- //void*data,/*Dataprovidedinthe4thargumentofsqlite3_exec()*/
- //intncols,/*Thenumberofcolumnsinrow*/
- //char**values,/*Anarrayofstringsrepresentingfieldsintherow*/
- //char**headers/*Anarrayofstringsrepresentingcolumnnames*/
- //);
- intcallback(void*data,intncols,87); background-color:inherit; font-weight:bold">char**values,87); background-color:inherit; font-weight:bold">char**headers)
- {
- inti;
- intlen=0;
- intll=0;
- for(i=0;i<ncols;i++)
- {
- if(strlen(headers[i])>len)
- len=strlen(headers[i]);
- }
- ll=len-strlen(headers[i]);
- while(ll)
- fprintf(stdout,"");
- --ll;
- }
- fprintf(stdout,"%s:%s\n",headers[i],values[i]);
- "\n");
- return0;
- intsearch_by_callback(constchar*db_name,char*sql_cmd)
- inti=0;
- intj=0;
- intnrow=0,ncolumn=0;
- char**azResult;//二维数组存放结果
- sqlite3*db=NULL;
- char*zErrMsg=0;
- intrc;
- if(access(db_name,0)==-1)
- fprintf(stderr,"%snotfound\n",db_name);
- return-1;
- rc=sqlite3_open(db_name,&db);
- if(rc!=sqlITE_OK)
- "%sopenFailed:%s\n",db_name,sqlite3_errmsg(db));
- sqlite3_close(db);
- return-1;
- //查询数据
- rc=sqlite3_exec(db,sql_cmd,callback,NULL,&zErrMsg);
- if(rc!=sqlITE_OK)
- fprintf(stderr,"%s%s:%s\n",sqlite3_errmsg(db));
- if(zErrMsg)
- "ErrMsg=%s\n",zErrMsg);
- sqlite3_free(zErrMsg);
- if(zErrMsg)
- //关闭数据库
- intsearch_by_table(char*sql_cmd)
- inti=0;
- intj=0;
- //二维数组存放结果
- sqlite3*db=NULL;
- char*zErrMsg=0;
- intrc;
- intlen=0;
- rc=sqlite3_open(db_name,&db);
- sqlite3_close(db);
- //查询数据
- rc=sqlite3_get_table(db,&azResult,&nrow,&ncolumn,&zErrMsg);
- sqlite3_free_table(azResult);
- for(j=0;j<ncolumn;j++)
- if(strlen(azResult[j])>len)
- len=strlen(azResult[j]);
- //从第0索引到第nColumn-1索引都是字段名称
- //从第nColumn索引开始,后面都是字段值
- for(i=0;i<nrow;i++)
- intll=(len-strlen(azResult[j]));
- printf("");
- printf("%s:%s\n",azResult[j],azResult[(i+1)*ncolumn+j]);
- printf("\n");
- #if0
- printf("%s",azResult[j]);
- for(i=ncolumn;i<(nrow+1)*ncolumn;i++)
- if(((i-ncolumn+1)%ncolumn)==0)
- printf("%s\n",azResult[i]);
- else
- printf("%s",248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; list-style-position:outside!important"> #endif
- //与sqlite3_get_table对应,释放掉azResult的内存空间
- intsearch_by_stmt( sqlite3_stmt*stmt=0;
- intncolumn=0;
- char*column_name;
- intvtype,i;
- rc=sqlite3_prepare_v2(db,-1,&stmt,0);
- ncolumn=sqlite3_column_count(stmt);
- while(sqlite3_step(stmt)==sqlITE_ROW)
- for(i=0;i<ncolumn;i++)
- vtype=sqlite3_column_type(stmt,i);
- column_name=sqlite3_column_name(stmt,i);
- switch(vtype)
- casesqlITE_NULL:
- "%s:null\n",column_name);
- break;
- casesqlITE_INTEGER:
- "%s:%d\n",column_name,sqlite3_column_int(stmt,i));
- break;
- casesqlITE_FLOAT:
- "%s:%f\n",sqlite3_column_double(stmt,i));
- casesqlITE_BLOB:/*arguablyfallthrough...*/
- "%s:BLOB\n",column_name);
- casesqlITE_TEXT:
- default:
- "%s:ERROR[%s]\n",248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; list-style-position:outside!important"> sqlite3_finalize(stmt);
- //关闭数据库
- return0;
- intmain(intargc,87); background-color:inherit; font-weight:bold">char*argv[])
- if(argc!=4)
- "usage:%s[table/callback/stmt]<db_name><sql_cmd>\r\n",argv[0]);
- if((strcmp(argv[1],"table")==0))
- search_by_table(argv[2],argv[3]);
- else"callback")==0))
- search_by_callback(argv[2],248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; list-style-position:outside!important"> search_by_stmt(argv[2],248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; list-style-position:outside!important"> }
编译 gcc sqlite3_search.c sqlite3.c -o sqlite3_search -lpthread -ldl
原文链接:https://www.f2er.com/sqlite/198278.html