基于sqlite3实现的简单通讯录程序(控制台方法实例教程



基于sqlite3实现的简单通讯录程序(控制台方法实例教程。1. sqlite3 安装
1.1. 下载sqlite3源码    www.sqlite3.org    下载 sqlite-autoconf-3070701.tar.gz
1.2. 解压    将下载的 sqlite-autoconf-3070701.tar.gz 解压,得到sqlite-autoconf-3070701 文件夹
1.3. 编译源码(参照解压文件夹下的install文件)    $ cd sqlite-autoconf-3070701 #进入文件夹    $ ./configure    $ make    $ sudo make install          #注意一定要root权限
1.4. 查看安装情况    在/usr/local/include 下:sqlite3.h sqlite3ext.h    在/usr/local/bin     下:sqlite3    在/usr/local/lib      下:pkgconfig(文件夹) libsqlite3.a  libsqlite3.la                                           libsqlite3.so  libsqlite3.so.0 libsqlite3.so.0.8.6
2. sqlite3 c编程 之 环境搭建(Codeblocks)
2.1. 新建 c project    打开codeblocks,File–> new –> project–>console application–>c
2.2. 引入库    project–>build options –> linker setting    在linker library 下点击 add, 选择 /usr/local/lib/libsqlite3.so  文件
2.3. 添加.h文件    在程序开头#include <sqlite3.h>
2.4. 至此环境搭建完成。    在程序中,就可以使用sqlite3的c语言接口了    如:sqlite3_open()

sqlite3_close()

 

3. 通讯录程序

一个简单的通讯录程序,主要是为了练手,熟悉Slqite3的使用。

数据库:contact.db,只有一张表info,保存联系人信息(姓名,年龄,关系,手机号)

程序中bug较多,但却涵盖了sqlite3的数据库常用操作:创建数据库、创建表、增、删、改、查等。

 

  1. /************************************************************************
  2. * 名  称:contact_using_sqlite3.c
  3. * 功  能:一个简单的通讯录程序,主要是为了练习sqlite3的使用
  4. * 描  述:使用sqlite3,建立数据库contact,其中有表一张
  5.           info(name varchar(10), age smallint, relation varchar(10), phone varchar(11))
  6.           包括如下功能:
  7.              1. 查看通讯录
  8.              2. 增加联系人
  9.              3. 删除联系人
  10.              4. 修改联系人信息
  11.              5. 查找联系人
  12. * 作  者:JarvisChu
  13. * 时  间:2011-8-22
  14. * 修  订:2011-8-24,完成修改和删除;
  15. ************************************************************************/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sqlite3.h>
  20. sqlite3* db = NULL;               //the database
  21. //show the menu on the screen,and return user’s choice
  22. int showMenu(){
  23.     int choice = 0;
  24.     while(1){
  25.         printf(“*****************************************************\n”);
  26.         printf(“                      Welcome                        \n”);
  27.         printf(“1. Display All          2. Add   Contact\n”);
  28.         printf(“3. Delete  Contact      4. Alter Contact\n”);
  29.         printf(“5. Find    Contact\n”);
  30.         printf(“Your choice is:”);
  31.         scanf(“%d”,&choice);
  32.         if(choice == 1 || choice == 2 ||\
  33.            choice == 3 || choice == 4 ||\
  34.            choice == 5){
  35.             return choice;
  36.         }
  37.         else{
  38.             printf(“Wrong! Again!\n”);
  39.         }
  40.     }
  41.     return 0;
  42. }
  43. //show all records in db
  44. void displayAll(){
  45.     int i = 0;
  46.     int j = 0;
  47.     int index = 0;
  48.     int ret = 0;
  49.     int row = 0;
  50.     int column = 0;
  51.     char* sql = NULL;
  52.     char** resultSet = NULL;     //store the query result
  53.     sql = (char*)malloc(sizeof(char)*20);
  54.     strcpy(sql,”select * from info;”);
  55.     ret = sqlite3_get_table(db,sql,&resultSet,&row,&column,0);
  56.     if(ret != SQLITE_OK){
  57.         fprintf(stderr,”select records err\n”);
  58.     }
  59.     printf(“There are %d Contact:\n”,row);
  60.     index = 0;
  61.     for(i=0;i<=row;i++){
  62.         for(j=0;j<column;j++){
  63.             printf(“%-11s”,resultSet[index++]);
  64.         }
  65.         printf(“\n”);
  66.     }
  67.     sqlite3_free_table(resultSet);
  68.     free(sql);
  69. }
  70. //add contact
  71. void addContact(){
  72.     int ret = 0;
  73.     char* name = NULL;
  74.     int age = 0;
  75.     char* relation = NULL;
  76.     char* phone = NULL;
  77.     char* sql = NULL;
  78.     name = (char*)malloc(sizeof(char)*10);
  79.     relation = (char*)malloc(sizeof(char)*10);
  80.     phone = (char*)malloc(sizeof(char)*12);
  81.     sql = (char*)malloc(sizeof(char)*64);
  82.     printf(“input (name age relation phone):”);
  83.     scanf(“%s %d %s %s”,name,&age,relation,phone);
  84.     //printf(“%s, %d, %s,%s\n”,name,age,relation,phone);
  85.     sprintf(sql,”insert into info values(‘%s’,%d,’%s’,'%s’);”,name,age,relation,phone);
  86.     //printf(“%s\n”,sql);
  87.     ret = sqlite3_exec(db,sql,0,0,0);
  88.     if(ret != SQLITE_OK){
  89.         printf(“failed!\n”);
  90.     }
  91.     else{
  92.         printf(“ok!\n”);
  93.     }
  94.     free(name);
  95.     free(relation);
  96.     free(phone);
  97.     free(sql);
  98. }
  99. //find Contact
  100. void findContact(){
  101.     int i,j,index;
  102.     int ret  = 0;
  103.     int row = 0;
  104.     int column = 0;
  105.     char* name = NULL;
  106.     char* sql = NULL;
  107.     char** resultSet = NULL;
  108.     name = (char*)malloc(sizeof(char)*10);
  109.     printf(“Input the name you want to find:”);
  110.     scanf(“%s”,name);
  111.     sql = (char*)malloc(sizeof(char)*64);
  112.     sprintf(sql,”select * from info where name = ‘%s’”,name);
  113.     ret = sqlite3_get_table(db,sql,&resultSet,&row,&column,0);
  114.     if(ret != SQLITE_OK){
  115.         fprintf(stderr,”select err:%s\n”,sqlite3_errmsg(db));
  116.         return;
  117.     }
  118.     index = 0;
  119.     if(row>0){
  120.         for(i=0;i<=row;i++){
  121.             for(j=0;j<column;j++){
  122.                 printf(“%-11s”,resultSet[index++]);
  123.             }
  124.             printf(“\n”);
  125.         }
  126.     }
  127.     else{
  128.         printf(“no such person!\n”);
  129.     }
  130. }
  131. //alertContact()
  132. void alterContact(){
  133.     //first,find the contact info to be altered.
  134.     int i,j,index;
  135.     int ret  = 0;
  136.     int row = 0;
  137.     int column = 0;
  138.     int age = 0;
  139.     char* name = NULL;
  140.     char* relation = NULL;
  141.     char* phone = NULL;
  142.     char* sql = NULL;
  143.     char** resultSet = NULL;
  144.     name = (char*)malloc(sizeof(char)*10);
  145.     printf(“Input the name you want to alter:”);
  146.     scanf(“%s”,name);
  147.     sql = (char*)malloc(sizeof(char)*128);
  148.     sprintf(sql,”select * from info where name = ‘%s’”,name);
  149.     ret = sqlite3_get_table(db,sql,&resultSet,&row,&column,0);
  150.     if(ret != SQLITE_OK){
  151.         fprintf(stderr,”select err:%s\n”,sqlite3_errmsg(db));
  152.         return;
  153.     }
  154.     index = 0;
  155.     if(row>0){
  156.         for(i=0;i<=row;i++){
  157.             for(j=0;j<column;j++){
  158.                 printf(“%-11s”,resultSet[index++]);
  159.             }
  160.             printf(“\n”);
  161.         }
  162.         sqlite3_free_table(resultSet);
  163.         //exist ,then alter
  164.         relation = (char*)malloc(sizeof(char)*10);
  165.         phone = (char*)malloc(sizeof(char)*12);
  166.         printf(“input the new data (age relation phone):”);
  167.         scanf(“%d %s %s”,&age,relation,phone);
  168.         //printf(” %d, %s,%s\n”,name,age,relation,phone);
  169.         sprintf(sql,”update info set age=%d,relation=’%s’,phone=’%s’ where name=’%s’;”,age,relation,phone,name);
  170.         //printf(“%s\n”,sql);
  171.         ret = sqlite3_exec(db,sql,0,0,0);
  172.         if(ret != SQLITE_OK){
  173.             printf(“failed!\n”);
  174.         }
  175.         else{
  176.             printf(“ok!\n”);
  177.         }
  178.         free(relation);
  179.         free(phone);
  180.     }
  181.     else{
  182.         printf(“no such person!\n”);
  183.     }
  184.     free(sql);
  185.     free(name);
  186. }
  187. //delete Contact
  188. void deleteContact(){
  189.     int ret = 0;
  190.     char* name = NULL;
  191.     char* sql = NULL;
  192.     name = (char*)malloc(sizeof(char)*10);
  193.     sql = (char*)malloc(sizeof(char)*64);
  194.     printf(“Input the name of contact you want to delete:”);
  195.     scanf(“%s”,name);
  196.     sprintf(sql,”delete from info where name=’%s’;”,name);
  197.     //to be simple, there will be no warning if the contact does not exist
  198.     ret = sqlite3_exec(db,sql,0,0,0);
  199.     if(ret != SQLITE_OK){
  200.         printf(“delete err:%s”,sqlite3_errmsg(db));
  201.     }
  202.     else{
  203.         printf(“ok!”);
  204.     }
  205.     free(name);
  206.     free(sql);
  207. }
  208. int main()
  209. {
  210.     int ret = 0;
  211.     int choice = 0;
  212.     int ch = 0;
  213.     char* errmsg = NULL;
  214.     char* sql = NULL;
  215.     //open the db if exist or create it
  216.     ret = sqlite3_open(“contact.db”,&db);
  217.     if(ret){
  218.         fprintf(stderr,”Cannot open database:%s\n”,sqlite3_errmsg(db));
  219.         sqlite3_close(db);
  220.         exit(1);
  221.     }
  222.     else{
  223.         printf(“Open database successfully…\n”);
  224.     }
  225.     //create the table info if not exists
  226.     sql = (char*)malloc(sizeof(char)*128);//
  227.     //strcpy(sql,);
  228.     //printf(“Copy sql successfully\n”);
  229.     ret = sqlite3_exec(db,”create table if not exists info(\
  230.                     name varchar(10) primary key, \
  231.                     age smallint, \
  232.                     relation varchar(10), \
  233.                     phone varchar(11));”,0,0,&errmsg);
  234.     if(ret != SQLITE_OK){
  235.         //printf(“Create table error\n”);
  236.         fprintf(stderr,”Create table err:%s\n”,sqlite3_errmsg(db));
  237.     }
  238.     //printf(“Create table successfully\n”);
  239.     //insert some initial records,
  240.     //it will cause a err if not the frist time,but that does not matter
  241.     strcpy(sql,”insert into info values(‘zhu’,22,’本人’,’15109217871′);”);
  242.     ret = sqlite3_exec(db,sql,0,0,&errmsg);
  243.     if(ret != SQLITE_OK){
  244.         fprintf(stderr,”Insert record err:%s\n”,sqlite3_errmsg(db));
  245.     }
  246.     free(sql);
  247.     //printf(“Insert  record successfully\n”);
  248.     //main menu
  249.     while(1){
  250.         choice = showMenu();                        //show the menu and get the user’s choose
  251.         switch(choice){
  252.             case 1:
  253.                 displayAll();                       //show all records in db
  254.                 break;
  255.             case 2:
  256.                 addContact();
  257.                 break;
  258.             case 3:
  259.                 deleteContact();
  260.                 break;
  261.             case 4:
  262.                 alterContact();
  263.                 break;
  264.             case 5:
  265.                 findContact();
  266.                 break;
  267.             default:
  268.                 break;
  269.         }
  270.         //if back to main menu or not
  271.         printf(“\nBack to Menu 1(yes) / 0(no)?”);
  272.         scanf(“%d”,&ch);
  273.         if(ch == 0){
  274.             break;
  275.         }
  276.         //printf(“\33[2J”);
  277.         system(“clear”);
  278.     }
  279.     sqlite3_close(db);
  280.     return 0;
  281. }