现在的位置: 首页 > 综合 > 正文

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

2013年12月03日 ⁄ 综合 ⁄ 共 17049字 ⁄ 字号 评论关闭

本文为转载,转载地址为:

http://www.cnblogs.com/elect-fans/archive/2012/08/04/2622467.html

/* 项目名称:用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
*
* */

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

抱歉!评论已关闭.