#include
#include
#include
#include “…/address/address.h”
#include
#include
#include
/*
创建数据库文件
创建保存联系人信息的表
*/
void InitDataBase()
{
sqlite3 *ppdb;//数据库句柄
//创建数据库文件并打开
int ret = sqlite3_open(“address.db”, &ppdb);
if(ret != SQLITE_OK)
{
printf(“sqlite3_open:%s\n”, sqlite3_errmsg(ppdb));
exit(1);
}
char sql[128]={0};//保存数据库语句
sprintf(sql,“create table if not exists address(name text,tel text);”);
ret = sqlite3_exec(ppdb, sql, NULL, NULL, NULL);
if(ret != SQLITE_OK)
{
printf(“sqlite3_exec1:%s\n”, sqlite3_errmsg(ppdb));
exit(1);
}
sqlite3_close(ppdb);
}
void add_info(Chat *c)
{
sqlite3 *ppdb;
int ret = sqlite3_open(“address.db”, &ppdb);
if(ret != SQLITE_OK)
{
printf(“sqlite3_open:%s\n”, sqlite3_errmsg(ppdb));
}
char sql[128] = {0};
sprintf(sql,“insert into address values(’%s’,’%s’);”,c->name,c->tel);
ret = sqlite3_exec(ppdb, sql, NULL, NULL, NULL);
if(ret != SQLITE_OK)
{
printf(“sqlite3_exec:%s\n”, sqlite3_errmsg(ppdb));
}
sqlite3_close(ppdb);
}
void del_info(Chat *c)
{
sqlite3 *ppdb;
int ret = sqlite3_open(“address.db”, &ppdb);
if(ret != SQLITE_OK)
{
printf(“sqlite3_open:%s\n”, sqlite3_errmsg(ppdb));
}
char sql[100];
sprintf(sql, “delete from address where name=’%s’”, c->name);
ret = sqlite3_exec(ppdb, sql, NULL, NULL, NULL);
if(ret != SQLITE_OK)
{
printf(“sqlite3_exec:%s\n”, sqlite3_errmsg(ppdb));
}
sqlite3_close(ppdb);
printf(“delete success\n”);
}
void update_info(Chat *c)
{
sqlite3 *ppdb;
char *errmsg;
int ret = sqlite3_open(“address.db”, &ppdb);
if(ret != SQLITE_OK)
{
printf(“sqlite3_open:%s\n”, sqlite3_errmsg(ppdb));
}
char sql[128];
sprintf(sql, “update address set tel=’%s’ where name=’%s’;”,c->tel, c->name);
ret = sqlite3_exec(ppdb, sql, 0, 0, &errmsg);//delete
if(ret != SQLITE_OK)
{
printf(“insert table error: %s\n”, errmsg);
exit(1);
}
}
int sendinfo(void *pare, int columnCount, char **columnVal, char **columnName)
{
Chat c;
int fd = *(int *)pare;
strcpy(c.name, columnVal[0]);
strcpy(c.tel, columnVal[1]);
int ret = send(fd, &c, sizeof©, 0);
if(-1 == ret)
{
perror(“send”);
}
return 0;
}
void search_info(Chat c,int fd)
{
sqlite3 *ppdb;
int ret = sqlite3_open(“address.db”, &ppdb);
if(ret != SQLITE_OK)
{
printf(“sqlite3_open:%s\n”, sqlite3_errmsg(ppdb));
exit(1);
}
char sql[120];
sprintf(sql,“select *from address where name=’%s’;”,c.name);
ret = sqlite3_exec(ppdb, sql, sendinfo, &fd, NULL);
if(ret != SQLITE_OK)
{
printf(“sqlite3_exec:%s\n”, sqlite3_errmsg(ppdb));
exit(1);
}
Chat d;
strcpy(d.name, “bye”);
strcpy(d.tel, “bye”);
ret = send(fd, &d, sizeof(d), 0);
if(-1 == ret)
{
perror(“send”);
}
printf("\n");
}
void show_info(int fd)
{
sqlite3 *ppdb;
int ret = sqlite3_open(“address.db”, &ppdb);
if(ret != SQLITE_OK)
{
printf(“sqlite3_open:%s\n”, sqlite3_errmsg(ppdb));
exit(1);
}
char sql[128] = “select *from address;”;
ret = sqlite3_exec(ppdb, sql, sendinfo, &fd, NULL);
if(ret != SQLITE_OK)
{
printf(“sqlite3_exec:%s\n”, sqlite3_errmsg(ppdb));
exit(1);
}
Chat c;
strcpy(c.name, “bye”);
strcpy(c.tel, “bye”);
ret = send(fd, &c, sizeof©, 0);
if(-1 == ret)
{
perror(“send”);
}
printf("\n");
}