转自:http://www.jb51.cc/article/p-dtdaakpd-xq.html
感谢作者。。。
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的数据库常用操作:创建数据库、创建表、增、删、改、查等。
- /************************************************************************
- *名称:contact_using_sqlite3.c
- *功能:一个简单的通讯录程序,主要是为了练习sqlite3的使用
- *描述:使用sqlite3,建立数据库contact,其中有表一张
- info(namevarchar(10),agesmallint,relationvarchar(10),phonevarchar(11))
- 包括如下功能:
- 1.查看通讯录
- 2.增加联系人
- 3.删除联系人
- 4.修改联系人信息
- 5.查找联系人
- *作者:JarvisChu
- *时间:2011-8-22
- *修订:2011-8-24,完成修改和删除;
- ************************************************************************/
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<sqlite3.h>
- sqlite3*db=NULL;//thedatabase
- //showthemenuonthescreen,andreturnuser'schoice
- intshowMenu(){
- intchoice=0;
- while(1){
- printf("*****************************************************\n");
- printf("Welcome\n");
- printf("1.DisplayAll2.AddContact\n");
- printf("3.DeleteContact4.AlterContact\n");
- printf("5.FindContact\n");
- printf("Yourchoiceis:");
- scanf("%d",&choice);
- if(choice==1||choice==2||\
- choice==3||choice==4||\
- choice==5){
- returnchoice;
- }
- else{
- printf("Wrong!Again!\n");
- }
- return0;
- //showallrecordsindb
- voiddisplayAll(){
- inti=0;
- intj=0;
- intindex=0;
- intret=0;
- introw=0;
- intcolumn=0;
- char*sql=NULL;
- char**resultSet=NULL;//storethequeryresult
- sql=(char*)malloc(sizeof(char)*20);
- strcpy(sql,"select*frominfo;");
- ret=sqlite3_get_table(db,sql,&resultSet,&row,&column,0);
- if(ret!=sqlITE_OK){
- fprintf(stderr,"selectrecordserr\n");
- printf("Thereare%dContact:\n",row);
- index=0;
- for(i=0;i<=row;i++){
- for(j=0;j<column;j++){
- printf("%-11s",resultSet[index++]);
- printf("\n");
- sqlite3_free_table(resultSet);
- free(sql);
- //addcontact
- voidaddContact(){
- char*name=NULL;
- intage=0;
- char*relation=NULL;
- char*phone=NULL;
- name=(char)*10);
- relation=(char)*10);
- phone=(char)*12);
- char)*64);
- printf("input(nameagerelationphone):");
- scanf("%s%d%s%s",name,&age,relation,phone);
- //printf("%s,%d,%s,%s\n",age,phone);
- sprintf(sql,"insertintoinfovalues('%s',%d,'%s','%s');",0); background-color:inherit">//printf("%s\n",sql);
- ret=sqlite3_exec(db,108); list-style:decimal-leading-zero outside; color:inherit; line-height:17.600000381469727px; margin:0px!important; padding:0px 3px 0px 10px!important"> printf("Failed!\n");
- else{
- printf("ok!\n");
- free(name);
- free(relation);
- free(phone);
- free(sql);
- //findContact
- voidfindContact(){
- inti,j,index;
- char*sql=NULL;
- char**resultSet=NULL;
- printf("Inputthenameyouwanttofind:");
- scanf("%s",name);
- sql=(char)*64);
- sprintf(sql,"select*frominfowherename='%s'",name);
- ret=sqlite3_get_table(db,0);
- if(ret!=sqlITE_OK){
- fprintf(stderr,"selecterr:%s\n",sqlite3_errmsg(db));
- return;
- if(row>0){
- for(i=0;i<=row;i++){
- for(j=0;j<column;j++){
- printf("%-11s",resultSet[index++]);
- printf("\n");
- printf("nosuchperson!\n");
- //alertContact()
- voidalterContact(){
- //first,findthecontactinfotobealtered.
- intret=0;
- introw=0;
- intcolumn=0;
- char*relation=NULL;
- char*phone=NULL;
- printf("Inputthenameyouwanttoalter:");
- char)*128);
- //exist,thenalter
- printf("inputthenewdata(agerelationphone):");
- scanf("%d%s%s",phone);
- //printf("%d,%s,%s\n",phone);
- "updateinfosetage=%d,relation='%s',phone='%s'wherename='%s';",phone,sql);
- ret=sqlite3_exec(db,248); line-height:17.600000381469727px; margin:0px!important; padding:0px 3px 0px 10px!important"> printf("Failed!\n");
- printf("ok!\n");
- //deleteContact
- voiddeleteContact(){
- name=( printf("Inputthenameofcontactyouwanttodelete:");
- scanf("%s","deletefrominfowherename='%s';",0); background-color:inherit">//tobesimple,therewillbenowarningifthecontactdoesnotexist
- printf("deleteerr:%s",sqlite3_errmsg(db));
- printf("ok!");
- free(name);
- intmain()
- {
- intch=0;
- char*errmsg=NULL;
- //openthedbifexistorcreateit
- ret=sqlite3_open("contact.db",&db);
- if(ret){
- "Cannotopendatabase:%s\n",108); list-style:decimal-leading-zero outside; color:inherit; line-height:17.600000381469727px; margin:0px!important; padding:0px 3px 0px 10px!important"> sqlite3_close(db);
- exit(1);
- printf("Opendatabasesuccessfully...\n");
- //createthetableinfoifnotexists
- char)*128);//
- //strcpy(sql,);
- //printf("Copysqlsuccessfully\n");
- ifnotexistsinfo(\
- namevarchar(10)primarykey,\
- agesmallint,\
- relationvarchar(10),248); line-height:17.600000381469727px; margin:0px!important; padding:0px 3px 0px 10px!important"> phonevarchar(11));",&errmsg);
- //printf("Createtableerror\n");
- "Createtableerr:%s\n",0); background-color:inherit">//printf("Createtablesuccessfully\n");
- //insertsomeinitialrecords,
- //itwillcauseaerrifnotthefristtime,butthatdoesnotmatter
- "insertintoinfovalues('zhu',22,'本人','15109217871');");
- "Insertrecorderr:%s\n",0); background-color:inherit">//printf("Insertrecordsuccessfully\n");
- //mainmenu
- choice=showMenu();//showthemenuandgettheuser'schoose
- switch(choice){
- case1:
- displayAll();break;
- case2:
- addContact();
- break;
- case3:
- deleteContact();
- case4:
- alterContact();
- case5:
- findContact();
- default:
- //ifbacktomainmenuornot
- printf("\nBacktoMenu1(yes)/0(no)?");
- scanf("%d",&ch);
- if(ch==0){
- //printf("\33[2J");
- system("clear");
- }