C语言_网络编程_SQLite3数据库项目 _ 在线词典

一、项目分析

1、在线词典一般的饿运行过程:例如

                  

 —  服务器端将用户信息和历史记录保存在数据库中。客户端输入用户和密码,服务器端在数据库中查找、匹配,返回结果;

2、项目的流程

—  定义数据库中表的结构;

—  定义消息结构体;

—  分析服务器端和客户端 流程;

—  编码实现;

name text primary key : 表格中,name相同的 的字符串只能出现一次;

3、客户端设计流程图:

4、服务器 流程图:

— 总结: 在做任何的项目之前,都应该首先根据 项目需求 画出 流程图,根据流程图,编程时将更加有目的性;

二、项目编码

1、服务端源码

#include "onlin_dic.h"
#include <sys/types.h>
#include <sqlite3.h>

#define DATABACK "my.db"
void cli_data_handle(int sockefd,sqlite3* db);
int do_searchword(sqlite3* db,_MSG *msg ,char word[]);//在文件中直接查找dict.txt

int do_register(int newfd , sqlite3* db , _MSG* msg)
{
	char *errmsg;
	char sql[128];

	sprintf(sql,"insert into usr values('%s','%s');",msg->name,msg->data);
	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
	{
		printf("insert faile !!");
		strcpy(msg->data,"usr name already exist.");
	}else
	{
		printf("client register ok !\n");
		strcpy(msg->data,"ok");
	}
	if(send(newfd,msg,sizeof(_MSG),0) < 0)
	{
		printf("faile to faile");
		return -1;
	}
	return 1;
}

int do_history(int newfd , sqlite3* db , _MSG* msg)
{
	char sql[128];
	char history_word[255]= {0};
	int nrow;
	int ncloumn;
	char** resultp;
	char* errmsg;
	int index;
	int i,j;
	int index2;

	sprintf(sql,"select * from record where name = '%s';",msg->name);
	if(sqlite3_get_table(db,sql,&resultp,&nrow,&ncloumn,&errmsg) != SQLITE_OK )
	{
		printf("sqlite3_get_table faile !!");
	}else printf("sqlite3_get_table ok\n");//根据用户名以及密码,查找返回查找过得相应的记录

	index = 0;//列数
	for(i = 0;i<=nrow;i++)//行数
	{
		for(j = 0;j<=ncloumn;j++)
		{
			index2 = index;
			printf("%s  ",resul
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值