#include "Database_Operate.h"
#include <unistd.h>
#include <string.h>
sqlite3 * db;
void Create_History_Login_Table(sqlite3 *db,char *name)
{
int ret = 0;
char *errmsg = NULL;
char sql_history[200] = {0};
//创建历史搜索表
sprintf(sql_history,"create table if not exists %s_table(history_word text)",name);
//创建登陆表
char *sql_login = "create table if not exists login_table(name text primary_key, password text)";
ret = sqlite3_exec(db,sql_history,NULL,NULL,&errmsg);
if(ret != SQLITE_OK){
printf("history tables create fail err :%s\n",errmsg);
sqlite3_free(errmsg);//释放错误信息
exit(1);
}
ret = sqlite3_exec(db,sql_login,NULL,NULL,&errmsg);
if(ret != SQLITE_OK){
printf("Login tables create fail err :%s\n",errmsg);
sqlite3_free(errmsg);//释放错误信息
exit(1);
}
}
sqlite3 *Initialize_Database()//初始化一张用户表,用户退出从当中删除掉即可
{
int ret = 0;
//创建注册表
char *sql = "create table if not exists user_table(name text primary_key, password text)";
char *errmsg = NULL;
ret = sqlite3_open("system.db",&db);
if(ret != SQLITE_OK){
printf("sqlite3_open err : %s\n",sqlite3_errmsg(db));//打印数据库的错误
exit(1);
}
ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
if(ret != SQLITE_OK){
printf("user tables create fail err :%s\n",errmsg);
sqlite3_free(errmsg);//释放错误信息
exit(1);
}
return db;
}
int check_exist_password(sqlite3 *db,char *name,char *pass,char *list_name)
{
char check_buf[200] = {0};
int ret = 0;
char **result = NULL;//获得数据库里面的值
int row = -1;
int column = -1;
int index = 0;
int i = 0,j = 0;
char *errmsg = NULL;
sprintf(check_buf,"select name from '%s' where name = '%s' and password = '%s' ",list_name,name,pass);
ret = sqlite3_get_table(db,check_buf,&result,&row,&column,&errmsg);
if(ret != SQLITE_OK){
printf("select name fail err :%s\n",errmsg);
sqlite3_free(errmsg);//释放错误信息
return -1;
}
printf("row:%d\n",row);
return row == 1 ? 0 : -1;//0查找成功
}
int check_exist(sqlite3 *db,char *name,char *list_name)
{
char check_buf[200] = {0};
int ret = 0;
char **result = NULL;//获得数据库里面的值
int row = -1;
int column = -1;
int index = 0;
int i = 0,j = 0;
char *errmsg = NULL;
sprintf(check_buf,"select name from '%s' where name = '%s' ",list_name,name);
ret = sqlite3_get_table(db,check_buf,&result,&row,&column,&errmsg);
if(ret != SQLITE_OK){
printf("select name fail err :%s\n",errmsg);
sqlite3_free(errmsg);//释放错误信息
return -1;
}
printf("row:%d\n",row);
return row == 1 ? 0 : -1;//0查找成功
}
int Add_User(sqlite3 *db,char *UserName,char *PassWord,char *list_name)//成功返回0,失败返回-1
{
char Add_User_buf[200] = {0};
char *errmsg = NULL;
int ret = 0;
sprintf(Add_User_buf,"insert into %s values('%s','%s')",list_name,UserName,PassWord);
ret = sqlite3_exec(db,Add_User_buf,NULL,NULL,&errmsg);
if(ret != SQLITE_OK){
printf("add user fail err :%s\n",errmsg);
sqlite3_free(errmsg);//释放错误信息
return -1;
}
return 0;//
}
int Inquire_Word(sqlite3 *db,char *word,int connfd)//成功0,失败-1
{
char inquire_buf[2048] = {0};
int ret = 0;
char **result = NULL;//获得数据库里面的值
int row = -1;
int column = -1;
char *errmsg = NULL;
sprintf(inquire_buf,"select *from dict_table where word = '%s' ",word);
ret = sqlite3_get_table(db,inquire_buf,&result,&row,&column,&errmsg);
if(ret != SQLITE_OK){
printf("add user fail err :%s\n",errmsg);
sqlite3_free(errmsg);//释放错误信息
return -1;
}
if(row > 0)
write(connfd,result[3],strlen(result[3]));
else{
write(connfd,"查不到单词",20);
if(ret != SQLITE_OK){
printf("add user fail err :%s\n",errmsg);
sqlite3_free(errmsg);//释放错误信息
return -1;
}
}
return 0;
}
void Operate_Word(sqlite3* db_,char *recive_client,int connfd)
{
int cent = 0;
int i = 0;
int j = 0;
char word[50] = {0};
char name[50] = {0};
printf("%s\n",recive_client);
while(cent != 1){
++i;
if(recive_client[i] == '/')
++cent;
}
bzero(name,sizeof(name));
while(cent != 2){
name[j] = recive_client[++i];
++j;
if(recive_client[i] == '/')
++cent;
}
name[strlen(name) -1] = '\0';
j = 0;
bzero(word,sizeof(word));
while(recive_client[i] != '\0'){
word[j] = recive_client[++i];
j++;
}
printf("%s %s\n",name,word);
Query_History_Word(db_,name,word);
Inquire_Word(db_,word,connfd);
}
void Operate_History(sqlite3 *db_,char *recive_client,int connfd)
{//查看历史了记录
int cent = 0;
int i = 0;
int j = 0;
char history_buf[200] = {0};
char name[50] = {0};
int ret = 0;
char *errmsg = NULL;
char **result = NULL;//获得数据库里面的值
int row = -1;
int column = -1;
int index = -1;
while(cent != 1){
++i;
if(recive_client[i] == '/')
++cent;
}
bzero(name,sizeof(name));
while(cent != 2){
name[j] = recive_client[++i];
++j;
if(recive_client[i] == '/')
++cent;
}
name[strlen(name) -1] = '\0';
printf("name:%s\n",name);
sprintf(history_buf,"select *from %s_table",name);
ret = sqlite3_get_table(db_,history_buf,&result,&row,&column,&errmsg);
if(ret != SQLITE_OK){
printf("add user fail err :%s\n",errmsg);
sqlite3_free(errmsg);//释放错误信息
return;
}
printf("row:%d column:%d\n",row,column);
if(ret != SQLITE_OK){
printf("add user fail err :%s\n",errmsg);
sqlite3_free(errmsg);//释放错误信息
}
bzero(history_buf,sizeof(history_buf));
index = column;
for(i = 0;i < row;++i){
for(j = 0;j < column;++j)
sprintf(history_buf,"%s %s",history_buf,result[index++]);
}
printf("%s\n",history_buf);
write(connfd,history_buf,strlen(history_buf));
}
int Query_History_Word(sqlite3* db_,char *name,char *word)//成功0 失败-1
{//把单词写入历史记录吧
char history_buf[50] = {0};
int ret = 0;
char *errmsg = NULL;
sprintf(history_buf,"insert into %s_table values('%s')",name,word);
ret = sqlite3_exec(db_,history_buf,NULL,NULL,&errmsg);
if(ret != SQLITE_OK){
printf("add user fail err :%s\n",errmsg);
sqlite3_free(errmsg);//释放错误信息
return -1;
}
return 0;
}
int exit_dictionary(sqlite3 *db,char *name,char *list_name)//从登录表或者注册表中删除
{
char delete_login_buf[200] = {0};
char *errmsg = NULL;
int ret = 0;
sprintf(delete_login_buf,"delete from %s where name = '%s' ",list_name,name);
ret = sqlite3_exec(db,delete_login_buf,NULL,NULL,&errmsg);
if(ret != SQLITE_OK){
printf("delete user fail err :%s\n",errmsg);
sqlite3_free(errmsg);//释放错误信息
return -1;
}
return 0;
}