C语言实现sqlite3数据库查询的基本方法

前端之家收集整理的这篇文章主要介绍了C语言实现sqlite3数据库查询的基本方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

sqlite回传函数相关说了3种基本方法:callback,gettable和预处理stmt方法

下面给出测试代码和测试用数据库代码如下

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include"sqlite3.h"
  5. //
  6. //typedefint(*sqlite3_callback)(
  7. //void*data,/*Dataprovidedinthe4thargumentofsqlite3_exec()*/
  8. //intncols,/*Thenumberofcolumnsinrow*/
  9. //char**values,/*Anarrayofstringsrepresentingfieldsintherow*/
  10. //char**headers/*Anarrayofstringsrepresentingcolumnnames*/
  11. //);
  12. intcallback(void*data,intncols,87); background-color:inherit; font-weight:bold">char**values,87); background-color:inherit; font-weight:bold">char**headers)
  13. {
  14. inti;
  15. intlen=0;
  16. intll=0;
  17. for(i=0;i<ncols;i++)
  18. {
  19. if(strlen(headers[i])>len)
  20. len=strlen(headers[i]);
  21. }
  22. ll=len-strlen(headers[i]);
  23. while(ll)
  24. fprintf(stdout,"");
  25. --ll;
  26. }
  27. fprintf(stdout,"%s:%s\n",headers[i],values[i]);
  28. "\n");
  29. return0;
  30. intsearch_by_callback(constchar*db_name,char*sql_cmd)
  31. inti=0;
  32. intj=0;
  33. intnrow=0,ncolumn=0;
  34. char**azResult;//二维数组存放结果
  35. sqlite3*db=NULL;
  36. char*zErrMsg=0;
  37. intrc;
  38. if(access(db_name,0)==-1)
  39. fprintf(stderr,"%snotfound\n",db_name);
  40. return-1;
  41. rc=sqlite3_open(db_name,&db);
  42. if(rc!=sqlITE_OK)
  43. "%sopenFailed:%s\n",db_name,sqlite3_errmsg(db));
  44. sqlite3_close(db);
  45. return-1;
  46. //查询数据
  47. rc=sqlite3_exec(db,sql_cmd,callback,NULL,&zErrMsg);
  48. if(rc!=sqlITE_OK)
  49. fprintf(stderr,"%s%s:%s\n",sqlite3_errmsg(db));
  50. if(zErrMsg)
  51. "ErrMsg=%s\n",zErrMsg);
  52. sqlite3_free(zErrMsg);
  53. if(zErrMsg)
  54. //关闭数据库
  55. intsearch_by_table(char*sql_cmd)
  56. inti=0;
  57. intj=0;
  58. //二维数组存放结果
  59. sqlite3*db=NULL;
  60. char*zErrMsg=0;
  61. intrc;
  62. intlen=0;
  63. rc=sqlite3_open(db_name,&db);
  64. sqlite3_close(db);
  65. //查询数据
  66. rc=sqlite3_get_table(db,&azResult,&nrow,&ncolumn,&zErrMsg);
  67. sqlite3_free_table(azResult);
  68. for(j=0;j<ncolumn;j++)
  69. if(strlen(azResult[j])>len)
  70. len=strlen(azResult[j]);
  71. //从第0索引到第nColumn-1索引都是字段名称
  72. //从第nColumn索引开始,后面都是字段值
  73. for(i=0;i<nrow;i++)
  74. intll=(len-strlen(azResult[j]));
  75. printf("");
  76. printf("%s:%s\n",azResult[j],azResult[(i+1)*ncolumn+j]);
  77. printf("\n");
  78. #if0
  79. printf("%s",azResult[j]);
  80. for(i=ncolumn;i<(nrow+1)*ncolumn;i++)
  81. if(((i-ncolumn+1)%ncolumn)==0)
  82. printf("%s\n",azResult[i]);
  83. else
  84. printf("%s",248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; list-style-position:outside!important"> #endif
  85. //与sqlite3_get_table对应,释放掉azResult的内存空间
  86. intsearch_by_stmt( sqlite3_stmt*stmt=0;
  87. intncolumn=0;
  88. char*column_name;
  89. intvtype,i;
  90. rc=sqlite3_prepare_v2(db,-1,&stmt,0);
  91. ncolumn=sqlite3_column_count(stmt);
  92. while(sqlite3_step(stmt)==sqlITE_ROW)
  93. for(i=0;i<ncolumn;i++)
  94. vtype=sqlite3_column_type(stmt,i);
  95. column_name=sqlite3_column_name(stmt,i);
  96. switch(vtype)
  97. casesqlITE_NULL:
  98. "%s:null\n",column_name);
  99. break;
  100. casesqlITE_INTEGER:
  101. "%s:%d\n",column_name,sqlite3_column_int(stmt,i));
  102. break;
  103. casesqlITE_FLOAT:
  104. "%s:%f\n",sqlite3_column_double(stmt,i));
  105. casesqlITE_BLOB:/*arguablyfallthrough...*/
  106. "%s:BLOB\n",column_name);
  107. casesqlITE_TEXT:
  108. default:
  109. "%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);
  110. //关闭数据库
  111. return0;
  112. intmain(intargc,87); background-color:inherit; font-weight:bold">char*argv[])
  113. if(argc!=4)
  114. "usage:%s[table/callback/stmt]<db_name><sql_cmd>\r\n",argv[0]);
  115. if((strcmp(argv[1],"table")==0))
  116. search_by_table(argv[2],argv[3]);
  117. else"callback")==0))
  118. 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

猜你在找的Sqlite相关文章