织梦CMS - 轻松建站从此开始!

罗索

用c语言动态操作sqlite3数据库

jackyhwei 发布于 2011-09-22 15:46 点击:次 
落鹤生:用c语言动态操作sqilite3数据库,目前已经封装实现了以下功能:1、动态创建表2、动态实现对表的基本操作(增、删、改、查)3、清空表数据、删除表(附加功能)。对一些只需要简易数据库的应用程序来说,这将是一段非常有用的代码。推荐大家收藏一下。
TAG:

/* 项目名称:用c语言动态操作sqilite3数据库
 * 项目成员:张双喜
 * 编译环境:gcc
 * 项目功能:
 *     1、动态创建表
 *     2、动态实现对表的基本操作(增、删、改、查)
 *     3、清空表数据、删除表(附加功能)
 * 项目总结:
 *     1、涉及的技术:
 *         1、int sqlite3_exec(sqlite3*, const char *sql, sqlite_callback, void*, char**);
 *          欲实现动态操作数据库,必须动态构造sql语句,构造sql语句可以用strcat函数,逐步拼接,直至完整构造出sql语句。
 *         2、欲实现简单菜单选项效果,须分离出实现每个子功能的函数,在switch中,选择的调用
 *         3、
 *     2、意外收获:
 *         1、想输入一条带有空格的字符串,不能用scanf函数,应为scanf函数遇到空格、换行、回车自动结束输入,所以scanf不能达到预期效果
 *          ,解决该问题可以使用gets函数,但是gets是个危险函数,c语言不提倡使用,在使用gets函数前,一定要清空缓冲区,否则。会出现
 *          意想不到的输入结果,这就是先前没有清空缓冲区造成的。
 *         2、linux c 清空缓冲区的问题:
 *             个人见解:
 *                 经测试,linux c 使用fflush(stdin)无法清空缓冲区,使用fflush(stdin)后,再使用gets函数,gets还是会从缓冲区中读取东西,
 *                 因为缓冲区经fflush(stdin)没有被清空;
 *                 经过在网上找资料,得知setbuf(stdin, NULL);这样一个函数,经测试,清理缓冲区成功。
 *             简言之:linux c 清空缓冲区的函数:setbuf(stdin, NULL);
 *     3、编程中问题及其解决方法:
 *         1、更新数据库update正确使用方法:
 *             update table_name set name = 'new_name', sex = 'new_sex' where id = id_val;        //是正确的
 *             上句中的,不能换成and,
 *             即:
 *             update table_name set name = 'new_name' and sex = 'new_sex' where id = id_val;        //是错误的
 *        2、字符串的问题:
 *            char * p = "hello world!";    //该字符串是只读的不能修改字符串,不能改写,不能strcat(p, "jack");
 *            char str[] = "hello world!"    //这样可以通过指针或不通过指针对字符串进行任意操作
 *            char *p;
 *            p = str;
 *        3、字符串清理零:
 *            char str[10];
 *            memset(str, 0,sizeof(str));
 *     张双喜
 *     v1.0
 *     2010.9.29    14:39
 * update 1
 *     1、增加了一个测试表test_table,解决了,每次对操作数据库必须首先建立一个表的bug
 *     2、增加了确认退出功能
 *
 *     张双喜
 *     v2.0
 *     2010.9.30    9:40
 *        
 * */

  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include <string.h> 
  4. #include "sqlite3.h" 
  5.  
  6. /*该结构体是方便对表内数据操作*/ 
  7.  
  8. typedef struct table_column 
  9.     char column_name[100];        //列名 
  10.     char column_type[200];        //列的数据类型 
  11. }COLUMN; 
  12.  
  13. void db_create(sqlite3 * db, char * pTable_name, int column_num
  14. , COLUMN column[], int * create_flag); 
  15. int rscallback(void * p, int column, char ** column_val
  16. ,char ** column_name); 
  17. void db_show(sqlite3 * db, char * pTable_name); 
  18. void db_insert(sqlite3 * db, char * pTable_name, int column_num
  19. , COLUMN column[], int * test_flag, int * create_flag); 
  20. void db_update(sqlite3 * db, char * pTable_name, int column_Num
  21. , COLUMN column[], int * test_flag, int * create_flag); 
  22. void db_delete(sqlite3 * db, char * pTable_name); 
  23. void db_empty(sqlite3 * db, char * pTable_name); 
  24. void db_drop(sqlite3 * db, char *pTable_name, int * test_flag
  25. int * create_flag); 
  26. void db_test(sqlite3 * db, char * pTable_name, int ** test_flag); 
  27. void db_flag(sqlite3 * db, int * create_flag, int * test_flag
  28. char * pTable_name); 
  29. void db_reset_table_name(sqlite3 * db, char * pTable_name); 
  30.  
  31. int main(int argc, char* argv[]) 
  32.     sqlite3 * db; 
  33.     int empty = 1; 
  34.     int ret = 0; 
  35.     int create_flag = 1; 
  36.     int test_flag = 1; 
  37.     int test_flag_create = 1; 
  38.     char c = 'n'
  39.     char * errmsg = NULL; 
  40.     int choice = -1; 
  41.     char table_name[20]; 
  42.     char * pTable_name; 
  43.     int column_num; 
  44.     int i; 
  45.     COLUMN column[10]; 
  46.     pTable_name = table_name; 
  47.     strcpy(pTable_name, "test_table"); 
  48.  
  49.     ret = sqlite3_open("student.db", &db); 
  50.     if(ret != SQLITE_OK) 
  51.     { 
  52.         perror("slqite3_open"); 
  53.         exit(1); 
  54.     } 
  55.  
  56.     while(choice != 0) 
  57.     { 
  58. printf("please input your choise:/n"); 
  59. printf("-------------------------------------/n"); 
  60. printf("|0.exit|1.create|2.show|3.insert|4.update|5.delete|6.empty|7.drop|/n"); 
  61. printf("-------------------------------------/n"); 
  62.         scanf("%d", &choice); 
  63.         
  64.         switch(choice) 
  65.         { 
  66.             case 0: 
  67. printf("you choise leave, y or n?/n"); 
  68.                     setbuf(stdin, NULL); 
  69.                     scanf("%c", &c); 
  70.                     setbuf(stdin, NULL); 
  71.                     if(c == 'y'
  72.                     { 
  73.                         if(test_flag == 0) 
  74.                         { 
  75. db_drop(db, "test_table", &test_flag, &create_flag); 
  76.                         } 
  77.                         
  78.                         printf("goodbye!/n"); 
  79.  
  80.                         sqlite3_close(db); 
  81.  
  82.                         return 0; 
  83.                     } 
  84.                     else 
  85.                     { 
  86.                         choice = -1; 
  87.                     } 
  88.  
  89.                     break
  90.                     
  91.             case 1: 
  92. printf("we will create a table for you, please input the name of your table:/n"); 
  93. scanf("%s",pTable_name); 
  94. printf("please input the number of column:/n"); 
  95. scanf("%d", &column_num); 
  96. printf("please input column_name column_type:/n"); 
  97.  
  98. for(i = 0; i < column_num; i++)
  99. scanf("%s %s", column[i].column_name, column[i].column_type);                        
  100.  
  101. db_create(db, table_name, column_num, column, &create_flag); 
  102.                     break
  103.             case 2: 
  104. db_flag(db, &create_flag, &test_flag, table_name); 
  105. db_show(db, table_name); 
  106.                     break
  107.                    
  108.             case 3: 
  109. db_flag(db, &create_flag, &test_flag, table_name); 
  110. db_insert(db, table_name, column_num, column, &test_flag, &create_flag); 
  111.                     break
  112.             case 4: 
  113. db_flag(db, &create_flag, &test_flag, table_name); 
  114. db_update(db, table_name, column_num, column, &test_flag, &create_flag); 
  115.                     break
  116.             case 5: 
  117.                     db_flag(db, &create_flag, &test_flag, table_name); 
  118.                     db_delete(db, table_name); 
  119.                     break
  120.             case 6: 
  121.                     db_flag(db, &create_flag, &test_flag, table_name); 
  122.                     db_empty(db, table_name); 
  123.                     break
  124.             case 7: 
  125.                     db_flag(db, &create_flag, &test_flag, table_name); 
  126.                     db_drop(db, table_name, &test_flag, &create_flag); 
  127.                     break
  128.             default
  129.                     printf("your choice is not exist!/n"); 
  130.                     break
  131.     
  132.         } 
  133.         
  134.     } 
  135.  
  136.     sqlite3_close(db); 
  137.  
  138.     return 0; 
  139.  
  140. void db_flag(sqlite3 * db, int * create_flag, int * test_flag
  141. char * pTable_name) 
  142.     if(create_flag) 
  143.     { 
  144.         if(*test_flag) 
  145.         { 
  146.             db_test(db, pTable_name, &test_flag); 
  147.         } 
  148.         
  149.     } 
  150.  
  151.     return
  152.  
  153. void db_test(sqlite3 * db, char * pTable_name, int ** test_flag) 
  154.     int ret; 
  155.     char * errmsg; 
  156.  
  157.     **test_flag = 0; 
  158.  
  159.     printf("because you have not create a table,so we create \
  160. a test_table table for you!/n"); 
  161.     ret = sqlite3_exec(db, "create table test_table (id integer primary key \
  162. autoincrement, name text, sex text, age integer);", NULL, NULL, &errmsg); 
  163.     if(ret != SQLITE_OK) 
  164.     { 
  165.         printf("error:%d:%s/n", ret, errmsg); 
  166.         exit(1); 
  167.     } 
  168.     ret = sqlite3_exec(db, "insert into test_table (name, sex, age) values \
  169. ('zsx', 'm', 23);",NULL ,NULL, &errmsg); 
  170.     if(ret != SQLITE_OK) 
  171.     { 
  172.         printf("error:%d:%s/n", ret, errmsg); 
  173.         exit(1); 
  174.     } 
  175.  
  176.     return
  177.  
  178. void db_create(sqlite3 * db, char * pTable_name, int column_num
  179. , COLUMN column[], int * create_flag) 
  180.     int ret, i; 
  181.     char * errmsg = NULL; 
  182.     char sql[200]; 
  183.     char * pSql; 
  184.  
  185.     *create_flag = 0; 
  186.     
  187.     pSql = sql; 
  188.  
  189.     strcpy(pSql, "create table "); 
  190.     
  191.     strcat(pSql, pTable_name); 
  192.     
  193.     strcat(pSql, "("); 
  194.  
  195.     strcat(pSql, column[0].column_name); 
  196.     strcat(pSql, " "); 
  197.     strcat(pSql, column[0].column_type); 
  198.     strcat(pSql, " primary key autoincrement"); 
  199.     strcat(pSql, ", "); 
  200.  
  201.     for(i = 1; i < column_num-1; i++) 
  202.     { 
  203.         strcat(pSql, column[i].column_name); 
  204.         strcat(pSql, " "); 
  205.         strcat(pSql, column[i].column_type); 
  206.         strcat(pSql, ", "); 
  207.     } 
  208.  
  209.     strcat(pSql, column[column_num-1].column_name); 
  210.     strcat(pSql, " "); 
  211.     strcat(pSql, column[column_num-1].column_type); 
  212.     strcat(pSql, ");"); 
  213.  
  214.     printf("/nsqlite > %s/n/n",pSql); 
  215.  
  216.     ret = sqlite3_exec(db, pSql, NULL, NULL, &errmsg); 
  217.     if(ret != SQLITE_OK) 
  218.     { 
  219.          printf("error:%d:%s/n", ret, errmsg); 
  220.          exit(1); 
  221.     } 
  222.     
  223.     return
  224.  
  225. int rscallback(void * p, int column, char ** column_val,char ** column_name) 
  226.     int i; 
  227.     *(int *)p = 0; 
  228.  
  229.     for(i = 0; i < column; i++) 
  230.     { 
  231.         printf("%s > %s/t/t",column_name[i], column_val[i]); 
  232.     } 
  233.     printf("/n"); 
  234.     return 0; 
  235.  
  236. void db_show(sqlite3 * db, char * pTable_name) 
  237.     int ret = 0; 
  238.     char * errmsg = NULL; 
  239.     int empty = 1; 
  240.     char sql[200]; 
  241.     char * pSql; 
  242.  
  243.     pSql = sql; 
  244.  
  245.     strcpy(pSql, "select * from "); 
  246.     strcat(pSql, pTable_name); 
  247.     strcat(pSql, ";"); 
  248.     printf("/nsqlite > %s/n/n", pSql); 
  249.  
  250.     ret = sqlite3_exec(db, pSql, rscallback, &empty, &errmsg); 
  251.     if(ret != SQLITE_OK) 
  252.     { 
  253.         printf("error:%d:%s/n", ret, errmsg); 
  254.         exit(1); 
  255.     } 
  256.  
  257.     if(empty) 
  258.     { 
  259.         printf("the table is empty!/n"); 
  260.     } 
  261.  
  262.     printf("/n"); 
  263.     return
  264.  
  265. void db_insert(sqlite3 * db, char * pTable_name, int column_num
  266. , COLUMN column[]int * test_flag, int * create_flag) 
  267.     int ret = 0; 
  268.     char * errmsg = NULL; 
  269.     char sql[200]; 
  270.     char * pSql; 
  271.     char tmp[20]; 
  272.     int i; 
  273.     char test_val[100]; 
  274.  
  275.     pSql = sql; 
  276.     strcpy(pSql, "insert into "); 
  277.     strcat(pSql, pTable_name); 
  278.     strcat(pSql, "("); 
  279.  
  280.     if((*test_flag == 0) && (*create_flag)) 
  281.     { 
  282.         strcat(pSql, " name, "); 
  283.         strcat(pSql, "sex, "); 
  284.         strcat(pSql, "age ) "); 
  285.         strcat(pSql, "values ("); 
  286.  
  287.         printf("please input name:/n"); 
  288.         scanf("%s", test_val); 
  289.         strcat(pSql, " /'"); 
  290.         strcat(pSql, test_val); 
  291.         strcat(pSql, "/', "); 
  292.         printf("please input sex:/n"); 
  293.         scanf("%s", test_val); 
  294.         strcat(pSql, "/'"); 
  295.         strcat(pSql, test_val); 
  296.         strcat(pSql,"/', "); 
  297.         printf("please input age:/n"); 
  298.         scanf("%s",test_val); 
  299.         strcat(pSql,test_val); 
  300.         strcat(pSql, ");"); 
  301.     } 
  302.     if(*create_flag == 0) 
  303.     { 
  304.         for(i = 1; i < column_num-1; i++) 
  305.         { 
  306.             strcat(pSql, column[i].column_name); 
  307.             strcat(pSql, ", "); 
  308.         } 
  309.  
  310.         strcat(pSql, column[column_num-1].column_name); 
  311.         strcat(pSql, ") "); 
  312.         strcat(pSql, "values("); 
  313.         for(i = 1; i < column_num-1; i++) 
  314.         { 
  315.             printf("please input %s/n",column[i].column_name); 
  316.             if(strcmp(column[i].column_type,"text") == 0) 
  317.             { 
  318.                 strcat(pSql, "/'"); 
  319.                 memset(tmp, 0, sizeof(tmp)); 
  320.                 scanf("%s",tmp); 
  321.                 strcat(pSql, tmp); 
  322.                 strcat(pSql,"/'"); 
  323.                 strcat(pSql, ","); 
  324.             } 
  325.             else 
  326.             { 
  327.                 memset(tmp,0,sizeof(tmp)); 
  328.                 scanf("%s",tmp); 
  329.                 strcat(pSql, tmp); 
  330.                 strcat(pSql,","); 
  331.                 strcat(pSql," "); 
  332.             } 
  333.         } 
  334.         printf("please input %s/n",column[column_num-1].column_name); 
  335.         if(strcmp(column[column_num-1].column_type, "text") == 0) 
  336.         { 
  337.             scanf("%s",tmp); 
  338.             strcat(pSql, "/'"); 
  339.             strcat(pSql, tmp); 
  340.             strcat(pSql, "/'"); 
  341.             strcat(pSql, ");"); 
  342.         } 
  343.         else 
  344.         { 
  345.             scanf("%s",tmp); 
  346.             strcat(pSql, tmp); 
  347.             strcat(pSql, ");"); 
  348.         } 
  349.     } 
  350.     printf("/nsqlite > %s/n/n", pSql); 
  351.   
  352.     ret = sqlite3_exec(db, pSql, NULL, NULL, &errmsg); 
  353.     if(ret != SQLITE_OK) 
  354.     { 
  355.         printf("error:%d:%s/n", ret, errmsg); 
  356.         exit(1); 
  357.     } 
  358.  
  359.     return
  360.  
  361. void db_update(sqlite3 * db, char * pTable_name, int column_num
  362. , COLUMN column[], int * test_flag, int * create_flag) 
  363.     int ret = 0; 
  364.     char * errmsg = NULL; 
  365.     char sql[200]; 
  366.     char * pSql; 
  367.     char new_val[20]; 
  368.     char  new_id[3]; 
  369.     int i; 
  370.     char test_val[100]; 
  371.     
  372.  
  373.     pSql = sql; 
  374.     strcpy(pSql, "update "); 
  375.     strcat(pSql, pTable_name); 
  376.     strcat(pSql, " set "); 
  377.  
  378.     if((*test_flag == 0) && (*create_flag)) 
  379.     { 
  380.         strcat(pSql, "name = "); 
  381.         printf("please input a new name:/n"); 
  382.         scanf("%s",test_val); 
  383.         strcat(pSql,"/'"); 
  384.         strcat(pSql,test_val); 
  385.         strcat(pSql, "/', sex = "); 
  386.         printf("please input a new sex: /n"); 
  387.         scanf("%s", test_val); 
  388.         strcat(pSql, "/'"); 
  389.         strcat(pSql, test_val); 
  390.         strcat(pSql, "/', age = "); 
  391.         printf("please input a new age:/n"); 
  392.         scanf("%s",test_val); 
  393.         strcat(pSql,test_val); 
  394.         strcat(pSql, " where id = "); 
  395.         printf("please input a id that you want to change:/n"); 
  396.         scanf("%s",test_val); 
  397.         strcat(pSql, test_val); 
  398.         strcat(pSql, ";");    
  399.     } 
  400.     if(*create_flag == 0) 
  401.     { 
  402.         for(i = 1; i < column_num-1; i++) 
  403.         { 
  404.             strcat(pSql, column[i].column_name); 
  405.             strcat(pSql, " = "); 
  406.             if(strcmp(column[i].column_type, "text") ==0) 
  407.             { 
  408.                 strcat(pSql, "/'"); 
  409.                 memset(new_val, 0, sizeof(new_val)); 
  410.                 printf("please input a new %s/n", column[i].column_name); 
  411.                 scanf("%s",new_val); 
  412.                 strcat(pSql, new_val); 
  413.                 strcat(pSql, "/'"); 
  414.                 strcat(pSql, " , "); 
  415.             } 
  416.             else 
  417.             { 
  418.                 memset(new_val, 0, sizeof(new_val)); 
  419.                 printf("please input a new %s/n", column[i].column_name); 
  420.                 scanf("%s",new_val); 
  421.                 strcat(pSql, new_val); 
  422.                 strcat(pSql, " , "); 
  423.             } 
  424.         } 
  425.  
  426.         strcat(pSql, column[i].column_name); 
  427.         strcat(pSql, " = "); 
  428.  
  429.         if(strcmp(column[column_num-1].column_type, "text") ==0) 
  430.         { 
  431.             strcat(pSql, "/'"); 
  432.             memset(new_val, 0, sizeof(new_val)); 
  433.             printf("please input a new %s/n", column[i].column_name); 
  434.             scanf("%s",new_val); 
  435.             strcat(pSql, new_val); 
  436.             strcat(pSql, "/'"); 
  437.             strcat(pSql, " "); 
  438.         } 
  439.         else 
  440.         { 
  441.             memset(new_val, 0, sizeof(new_val)); 
  442.             printf("please input a new %s/n", column[i].column_name); 
  443.             scanf("%s",new_val); 
  444.             strcat(pSql, new_val); 
  445.             strcat(pSql, " "); 
  446.         } 
  447.  
  448.         strcat(pSql, "where id = "); 
  449.         printf("please input the id that you want to change its value:/n"); 
  450.         scanf("%s",new_id); 
  451.         strcat(pSql, new_id); 
  452.         strcat(pSql, ";"); 
  453.     } 
  454.     printf("sqlite > %s/n/n",pSql); 
  455.       
  456.     ret = sqlite3_exec(db, pSql, NULL, NULL, &errmsg); 
  457.     if(ret != SQLITE_OK) 
  458.     { 
  459.         printf("error:%d:%s/n", ret, errmsg); 
  460.         exit(1); 
  461.     } 
  462.  
  463.     return
  464.     
  465.  
  466. void db_delete(sqlite3 * db, char * pTable_name) 
  467.     int ret = 0; 
  468.     char * errmsg = NULL; 
  469.     char sql[200]; 
  470.     char * pSql; 
  471.     char tmp_name[20]; 
  472.     char tmp_id[3]; 
  473.     
  474.  
  475.     pSql = sql; 
  476.     strcpy(pSql, "delete from "); 
  477.     strcat(pSql, pTable_name); 
  478.     strcat(pSql, " where id = "); 
  479.     printf("please input a id that you want to delete:/n"); 
  480.     scanf("%s",tmp_id); 
  481.     strcat(pSql, tmp_id); 
  482.     strcat(pSql, ";"); 
  483.  
  484.     printf("/nsqlite > %s/n/n",pSql); 
  485.       
  486.     ret = sqlite3_exec(db, pSql, NULL, NULL, &errmsg); 
  487.     if(ret != SQLITE_OK) 
  488.     { 
  489.         printf("error:%d:%s/n", ret, errmsg); 
  490.         exit(1); 
  491.     } 
  492.  
  493.     return
  494.  
  495. void db_empty(sqlite3 * db, char * pTable_name) 
  496.     int ret = 0; 
  497.     char * errmsg = NULL; 
  498.     char sql[200]; 
  499.     char * pSql; 
  500.  
  501.  
  502.     pSql = sql; 
  503.     strcpy(pSql, "delete from "); 
  504.     strcat(pSql, pTable_name); 
  505.     strcat(pSql,";"); 
  506.  
  507.     printf("/nsqlite > %s/n/n",pSql); 
  508.  
  509.     ret = sqlite3_exec(db, pSql, NULL, NULL, &errmsg); 
  510.     if(ret != SQLITE_OK) 
  511.     { 
  512.         printf("error:%d:%s/n", ret, errmsg); 
  513.         exit(1); 
  514.     } 
  515.  
  516.     return
  517.     
  518. void db_drop(sqlite3 * db, char * pTable_name, int * test_flag
  519. int * create_flag) 
  520.     int ret = 0; 
  521.     char * errmsg = NULL; 
  522.     char sql[200]; 
  523.     char * pSql; 
  524.  
  525.  
  526.     pSql = sql; 
  527.     strcpy(pSql, "drop table "); 
  528.     strcat(pSql, pTable_name); 
  529.     strcat(pSql,";"); 
  530.  
  531.     if(*test_flag) 
  532.     { 
  533.         printf("/nsqlite > %s/n/n",pSql); 
  534.     } 
  535.  
  536.     if(*create_flag == 0) 
  537.     { 
  538.         printf("/nsqlite > %s/n/n",pSql); 
  539.     } 
  540.  
  541.     ret = sqlite3_exec(db, pSql, NULL, NULL, &errmsg); 
  542.     if(ret != SQLITE_OK) 
  543.     { 
  544.         printf("error:%d:%s/n", ret, errmsg); 
  545.         exit(1); 
  546.     } 
  547.  
  548.     return
(张双喜)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201109/15033.html]
本文出处:blog.csdn.net/sunsea1026 作者:张双喜
顶一下
(8)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容