电子词典的相关子函数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版电子词典](本程序须要的相关 ...
随机推荐
- 2015弱校联盟(1) - C. Censor
C. Censor Time Limit: 2000ms Memory Limit: 65536KB frog is now a editor to censor so-called sensitiv ...
- SUDTOJ 3323园艺问题 (线段树)
园艺问题 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 本巨养了一盆双色茉莉.这种花有一种特点:第i朵花在第Di天盛开,刚开时是紫色的 ...
- [Machine-Learning] 熟悉 Numpy
Numpy 是 Python 中的一个模块,主要用于处理数学和计算相关的问题,这里是一个入门的介绍. 导入 习惯上可以这样导入: import numpy as np 在 machine learni ...
- 查看占用cpu和内存最多的进程
linux下获取占用CPU资源最多的10个进程,可以使用如下命令组合: ps aux|head -;ps aux|grep -v PID|sort -rn -k +|head linux下获取占用内存 ...
- 内核编译选配(VMware篇)
出现这个错误的原因是相应的驱动程序没有编译进内核,所以在内核启动时,不认识分区. 一.磁盘驱动没编译进内核 VMware5.5.3 的磁盘有两种,一种是IDE的,一种是SCSI的:VMware 你在新 ...
- 【转】稍改进过的ListView,很好用哈
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using ...
- VS2012解决方案的设置
用VS开发项目时,一个解决方案可以包含多个项目,在此我记录一下: 1.首先我新建一个Win32Demo的解决方案: 2.勾选"空项目": 3.新建完之后,会默认生成一个Win32D ...
- PHP操作MySQL的常用函数
某些情况下(如html中),调用php的变量时,要给变量加{},若要使字符串变量加上引号,则还需要在{}外加引号 如: $sql="select * from admin where use ...
- jsp表格数据导出到Execl
1.关于“下载” 需要设置页面header的一个属性为:Content-Disposition: attachment; filename=下载的文件.txt 如: <a href=" ...
- ubunt tmux X Error of failed request
X Error of failed request: BadName (named color or font does not exist) Major opcode of failed req ...