电子词典的相关子函数db.c程序
整个电子词典是分块做的:包含的Dic_Server.c,Dic_Client.c,db.c,query.c,xprtcl.c,dict.h,xprtcl.h,dict.txt(单词文件)
下面是db.c代码:主要用创建子进程实现服务器端的并发操作
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#include "dict.h"
#include "xprtcl.h"
#define DATABASE "dict.db"
#define T_USERS "users" /* table name for login users */
#define T_HISTORY "history" /* table name for query history */
#define MAXLEN_SQL 256
sqlite3 *g_db = NULL;
const char *login_name;
const char *login_passwd;
void db_close()
{
if (sqlite3_close(g_db) != SQLITE_OK) {
fprintf(stderr,
"error: close database: %s\n",
sqlite3_errmsg(g_db));
}
}
static int cb_tbl_isexist(
void* arg,
int count,
char** column_value,
char** column_name)
{
dprintf("cb_tbl_isexist was called once\n");
*((int*)arg) = 1; /* set existence flag as 1 */
return 0;
}
static int tbl_isexist(sqlite3 *db, const char *table_name)
{
char sqlstr[MAXLEN_SQL];
int exist;
char *errmsg;
if (snprintf(sqlstr, MAXLEN_SQL,
"select name from sqlite_master "
"where type='table' and name='%s'", table_name) < 0) {
dprintf("snprintf SQL error");
return 0;
}
dprintf("SQL: %s\n", sqlstr);
/*
* Note: this flag "exist" will be passed into the callback.
* If the callback occurred, it will be modified by
* the callback internally.
*/
exist = 0;
if (SQLITE_OK != sqlite3_exec(
db,
sqlstr,
cb_tbl_isexist,
&exist,
&errmsg)) {
dprintf("sqlite3_exec error (table[%s] existence): %s\n",
table_name, errmsg);
sqlite3_free(errmsg);
}
return exist;
}
static int tbl_create_usrs(sqlite3 *db) /*在数据库中创建需要的表格:usrs*/
{
char sqlstr[MAXLEN_SQL];
char *errmsg;
if (1 == tbl_isexist(db, T_USERS)) /*如果创建的表存在,退出*/
{
dprintf("table users already exists\n");
return 0;
}
if (snprintf(
sqlstr,
MAXLEN_SQL,
"create table users(usrname text, passwd text, state text);"
) < 0) {
perror("snprintf error");
return -1;
}
dprintf("SQL: %s\n", sqlstr);
if (SQLITE_OK != sqlite3_exec(
db,
sqlstr,
NULL,
NULL,
&errmsg)) {
fprintf(stderr, "error: create users: %s\n", errmsg);
sqlite3_free(errmsg);
return -1;
}
return 0;
}
static int tbl_create_history(sqlite3 *db) /*在数据库中创建需要的表格:usrs*/
{
char sqlstr[MAXLEN_SQL];
char *errmsg;
if (1 == tbl_isexist(db, T_HISTORY)) /*如果创建的表存在,退出*/
{
dprintf("table history already exists\n");
return 0;
}
if (snprintf(
sqlstr,
MAXLEN_SQL,
"create table history(usrname text, word text, time text);"
) < 0) {
perror("snprintf error");
return -1;
}
dprintf("SQL: %s\n", sqlstr);
if (SQLITE_OK != sqlite3_exec(
db,
sqlstr,
NULL,
NULL,
&errmsg)) {
fprintf(stderr, "error: create users: %s\n", errmsg);
sqlite3_free(errmsg);
return -1;
}
return 0;
}
int db_init()
{
int result;
result = sqlite3_open(DATABASE, &g_db);
if (result != SQLITE_OK)
{
if (NULL != g_db)
{
fprintf(stderr, "error: open database: %s\n",
sqlite3_errmsg(g_db));
}
else
{
fprintf(stderr, "error: failed to allocate memory!\n");
}
return -1;
}
if (tbl_create_usrs(g_db) < 0) {
goto _error_exit;
}
if (tbl_create_history(g_db) < 0) {
goto _error_exit;
}
return 0;
_error_exit:
db_close();
return -1;
}
static int cb_user_isexist(
void* arg,
int count,
char** column_value,
char** column_name)
{
dprintf("cb_user_isexist was called once\n");
*((int*)arg) = 1; /* set existence flag as 1 */
return 0;
}
static int user_isexist(sqlite3 *db, const char *user_name,const char *user_passwd)
{
char sqlstr[MAXLEN_SQL];
int exist;
char *errmsg;
if (snprintf(sqlstr, MAXLEN_SQL,
"select * from %s "
"where usrname='%s'", T_USERS, user_name) < 0) {
dprintf("snprintf SQL error");
return 0;
}
dprintf("SQL: %s\n", sqlstr);
/*
* Note: this flag "exist" will be passed into the callback.
* If the callback occurred, it will be modified by
* the callback internally.
*/
exist = 0;
if (SQLITE_OK != sqlite3_exec(
db,
sqlstr,
cb_user_isexist,
&exist,
&errmsg)) {
dprintf("sqlite3_exec error (user[%s] existence): %s\n",
user_name, errmsg);
sqlite3_free(errmsg);
}
return exist;
}
/*注册的账号*/
int tbl_insert_users(const char *name, const char *passwd)
{
char sqlstr[MAXLEN_SQL];
char *errmsg;
/* validating syntax of username and passwd ...... */
/* checking if user exists with the input name */
if (user_isexist(g_db, name, passwd)) /*账号已存在,退出*/
{
return -1;
}
/* in case passed checking */ /*账号不存在,向数据库中录入账号和密码*/
if (snprintf(sqlstr, MAXLEN_SQL, "insert into users values('%s', '%s', '0')",
name, passwd)< 0)
{
perror("snprintf error");
return -1;
}
dprintf("SQL: %s\n", sqlstr);
if (SQLITE_OK != sqlite3_exec(g_db, sqlstr, NULL, NULL, &errmsg))
{
fprintf(stderr, "error: insert users: %s\n", errmsg);
sqlite3_free(errmsg);
return -1;
}
return 0;
}
/*登录验证的回调函数*/
static int login_user_isexist(
void* arg,
int count,
char** column_value,
char** column_name)
{
//int i=0;
char sqlstr[MAXLEN_SQL];
char *errmsg;
dprintf("cb_user_isexist was called once\n");
if((*column_value[0] == *login_name) && (*column_value[1] == *login_passwd))
{
if((*column_value[2])== '0') /*验证账号是否已登录*/
{
//printf("entry this function\n");
if (snprintf(sqlstr, MAXLEN_SQL,
"update users set state = 1 where usrname = '%s'",
column_value[0])< 0)
{
perror("snprintf error");
return -1;
}
dprintf("SQL: %s\n", sqlstr);
if (SQLITE_OK != sqlite3_exec(g_db, sqlstr, NULL, NULL, &errmsg))
{
fprintf(stderr, "error: insert users: %s\n", errmsg);
sqlite3_free(errmsg);
return -1;
}
*((int*)arg) = 1;/*set existence flag as 1 */
}
if((*column_value[2])== '1')
{
*((int*)arg) = 2;
}
}
return 0;
}
static int login_user_true(sqlite3 *db, const char *user_name,const char *user_passwd)
{
char sqlstr[MAXLEN_SQL];
int exist;
char *errmsg;
if (snprintf(sqlstr, MAXLEN_SQL,
"select * from %s "
"where usrname='%s'", T_USERS, user_name) < 0) {
dprintf("snprintf SQL error");
return 0;
}
dprintf("SQL: %s\n", sqlstr);
/*
* Note: this flag "exist" will be passed into the callback.
* If the callback occurred, it will be modified by
* the callback internally.
*/
exist = 0;
if (SQLITE_OK != sqlite3_exec(
db,
sqlstr,
login_user_isexist,
&exist,
&errmsg)) {
dprintf("sqlite3_exec error (user[%s] existence): %s\n",
user_name, errmsg);
sqlite3_free(errmsg);
}
return exist;
}
/*验证登陆账号是否注册*/
int login_users(const char *name, const char *passwd)
{
//char sqlstr[MAXLEN_SQL];
//char *errmsg;
int login_flag;
login_name = name;
login_passwd = passwd;
/* validating syntax of username and passwd ...... */
/* checking if user exists with the input name */
login_flag = login_user_true(g_db, name, passwd);
if (login_flag==1) /*账号是否已注册和登录*/
{
printf("login successfuly\n");
return 0;
}
if(login_flag==2) /*账号已登录*/
{
printf("Accounts have been online\n");
return 1;
}
return -1;
}
电子词典的相关子函数db.c程序的更多相关文章
- Python实现电子词典(图形界面)
Python实现电子词典(图形界面) 终端电子词典:https://www.cnblogs.com/noonjuan/p/11341375.html 文件一览: .├── client.py├── d ...
- Python实现电子词典
代码一览: dictionary/├── code│ ├── client.py│ ├── func.py│ ├── server.py│ └── settings.py├── dat ...
- SQLServer学习笔记<>相关子查询及复杂查询
二.查询缺少值的查询 在这里我们加入要查询2008年每一天的订单有多少?首先我们可以查询下订单表的订单日期在2008年的所有订单信息. 1 select distinct orderdate,coun ...
- [SQL SERVER系列]之嵌套子查询和相关子查询
子查询有两种类型,一种是只返回一个单值的子查询,这时它可以用在一个单值可以使用的地方,这时子查询可以看作是一个拥有返回值的函数:另外一种是返回一列值的子查询,这时子查询可以看作是一个在内存中临时存在的 ...
- C++第15周(春)项目3 - OOP版电子词典(一)
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 [项目3-OOP版电子词典](本程序中须要的相 ...
- 第14周 项目三-OOP版电子词典
做一个简单的电子词典.在文件dictionary.txt中,保存的是英汉对比的一个词典,词汇量近8000个,英文.中文释义与词性间用'\t'隔开. (1)编程序,由用户输入英文词.显示词性和中文释义. ...
- 使用Android简单实现有道电子词典
前言: 毕业设计的内容,仅仅有Java基础.没学过Android. 本着用到什么学什么.花费了10多个晚上完毕毕业设计. 当然,仅仅是简单的实线了电子词典功能,自始至终没有考虑过性能等问题. 本电子词 ...
- wxWidgets+wxSmith版电子词典
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目3-OOP版电子词典](本程序须要的相关 ...
- C++第15周(春)项目3 - OOP版电子词典(二)
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目3-OOP版电子词典](本程序须要的相关 ...
随机推荐
- android 布局优化常用技巧
android对多个模块都要是要的UI逻辑的致辞除了fragment之外,没有别的东西可以支持了, include,merge,viewstub只能支持公用的ui,但是这个通用支持不能包含逻辑(jav ...
- Python多进程编程
转自:Python多进程编程 阅读目录 1. Process 2. Lock 3. Semaphore 4. Event 5. Queue 6. Pipe 7. Pool 序. multiproces ...
- Zabbix安装图解教程
说明: 操作系统:CentOS IP地址:192.168.21.127 Web环境:Nginx+MySQL+PHP zabbix版本:Zabbix 2.2 LTS 备注:Linux下安装zabbix需 ...
- 通过HWND获得CWnd指针
cwnd 又为计算机网络中拥塞窗口(congestion window)的简写.拥塞窗口的大小取决于网络的拥塞程度,并且动态地在变化.发送方让自己的发送窗口还可能小于拥塞窗口. CWnd是MFC窗口类 ...
- HTTP协议的头信息详解
转载地址:http://blog.csdn.net/guoguo1980/article/details/2649658 HTTP(HyperTextTransferProtocol)是超文本传输协议 ...
- Postman 安装及使用入门教程
安装 本文只是基于 Chrome 浏览器的扩展插件来进行的安装,并非单独应用程序. 首先,你要台电脑,其次,安装有 Chrome 浏览器,那你接着往下看吧. 1. 官网安装(别看) 打开官网,http ...
- Jqplot 使用总结之一(线条及节点颜色)
好不容易抽出时间将Jqplot做下最后的总结,下面通过四个例子来学习Jqplot的一些常见技巧:示例1. 设置线条颜色(包括背景色及线条颜色的批量赋值) <!DOCTYPE html> & ...
- 【ros】rplidar Hector Slam
想用rplidar跑一下hector slam,在网上发现了几个教程写的都不错,但是亲测发现都有点不足,综合了一下,进行补充. 1. 安装ros 和 创建工作空间 http://blog.csdn.n ...
- python爬虫(1)
了解python的基本语法后就可以开始了,边学边巩固. 学爬虫那什么是网络爬虫,以下是百度百科的定义:网络爬虫(又被称为网页蜘蛛,网络机器人, 在FOAF社区中间,更经常的称为网页追逐者),是一种按照 ...
- python的类和对象——类成员番外篇
学完了面向对象的三大特性,已经get了所有屌丝技能的我们也当一回文艺小青年,来看看类的成员和成员修饰符. 今天‘三’这个数字好亲和~~~类成员可以分为三类:字段.方法和属性 一.字段 首先我们来看看字 ...