本程序由main.c和ops.h构成:
main.c是源代码文件,包含主要业务逻辑。
ops.h是自定义头文件,实现所有的方法。
sqlite3.dll是库文件
sq;ite3.h、sqlite3.c是官方提供的源代码文件和库文件。
sqlite.exe用来操作生成的数据库文件。
less.bat是个文本查看器
main.c代码:
#include <stdio.h>
#include <stdlib.h>
#include "ops.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void isInit() {
int init_rc = -1;
int initResultInput = 0;
printf("Whether to initialize the yoghurt system[0/1] :");
scanf("%d",&initResultInput);
printf("%d \n",initResultInput);
showDataMessage();
showMessageBar();
//判断是否要初始化数据
if(initResultInput == 1) {
printf("Signal[init_rc = -1]Failed! \n");
init_rc = initFunc();
showMessageBar();
if(init_rc != 0) {
printf("Initialization failed, please clean up the initialization garbage manually,Signal[init_rc != 0]... \n");
} else {
printf("Signal[init_rc = %d]Success! \n",init_rc);
showMessageBar();
}
} else if(initResultInput == 1) {
printf("Stating... \n");
} else {
printf("Error :( \n");
showDataMessage();
}
}
int main() {
wel();
isInit();
writeLogTime("正在初始化系统...");
/*Some Values*/
int selectRoleValue;
//显示欢迎信息
welcome();
writeLogTime("进入FirstUI界面...");
firstUI:
while(1) {
//选择需要的操作
selectRole();
printf("请输入操作代码:");
scanf("%d",&selectRoleValue);
getchar();
Sleep(200);
printf("\n");
if(selectRoleValue == 0) {
showDataMessage();
break;
}
if(selectRoleValue == 1) {
showDataMessage();
break;
}
if(selectRoleValue == 2) {
showDataMessage();
break;
}
inputError();
}
int userLoginFuncMsgRecv = 0;
while(1) {
//登录
if(selectRoleValue == 0) {
userLogin();
writeLogTime("用户登录...");
showDataMessage();
showMessageBar();
//int userLoginFuncMsgRecv = 0;
userLoginFuncMsgRecv = userLoginFunc();
if(userLoginFuncMsgRecv == 1 || userLoginFuncMsgRecv == 2) {
break;
}
}
//注册
if(selectRoleValue == 1) {
userReg();
writeLogTime("用户注册...");
showDataMessage();
showMessageBar();
userRegFunc();
showDataMessage();
//回到第一个界面
goto firstUI;
}
//退出
if(selectRoleValue == 2) {
printf("Shutdown the System... \n");
showDataMessage();
showMessageBar_warning();
writeLogTime("系统退出...");
exit(0);
}
inputError();
}
int subUser = 0;
while(1) {
if(userLoginFuncMsgRecv == 1) {
functionSelect();
}
if(userLoginFuncMsgRecv == 2) {
adminDialog();
functionSelect();
systemFunc();
}
printf("请输入代码:");
int userSelectValue;
scanf("%d",&userSelectValue);
getchar();
printf("您选择的是[%d] \n",userSelectValue);
showDataMessage();
switch(userSelectValue) {
case 0:
showDataMessage();
showMessageBar();
userKaiHu();
userKaiHuFunc();
writeLogTime("用户开户...");
break;
case 1:
showDataMessage();
showMessageBar();
userXiaoHu();
userXiaoHuFunc();
writeLogTime("用户销户...");
break;
case 2:
showDataMessage();
showMessageBar();
raiseMoney();
raiseMoneyFunc();
writeLogTime("用户存款...");
break;
case 3:
showDataMessage();
showMessageBar();
reduiceMoney();
reduiceMoneyFunc();
writeLogTime("用户提款...");
break;
case 4:
showDataMessage();
showMessageBar();
showTable();
writeLogTime("用户报表...");
showTime();
printf("\n");
printf("请输入您的用户名:");
char un[20];
scanf("%s",&un);
printf("\n");
char pw[20];
printf("请输入您的密码:");
scanf("%s",&pw);
printf("\n");
long mm;
showDataMessage();
Sleep(100);
mm = getUserMoney(un,pw);
showUserMoney(mm);
break;
case 5:
showDataMessage();
showMessageBar();
subUser = 1;
writeLogTime("进入FirstUI界面...");
break;
case 6:
showDataMessage();
showMessageBar();
systemStop();
showMessageBar_warning();
adminDeleteLog();
writeLogTime("删除日志文件...");
removeLogFile();
break;
case 7:
showDataMessage();
showMessageBar();
adminShowLog();
writeLogTime("查看日志文件...");
system("less ba.log");
break;
case 8:
showDataMessage();
showMessageBar();
systemStop();
showMessageBar_warning();
adminResetSystem();
writeLogTime("删除数据...");
removeDataFile();
removeLogFile();
//writeLogTime("");
break;
case 9:
showDataMessage();
showMessageBar();
writeLogTime("重新启动...");
exit(0);
break;
case 10:
showDataMessage();
showMessageBar();
systemStop();
writeLogTime("密码更改...");
printf("暂不支持管理员密码更改和用户权限更改,请联系柜台,操作后台数据库!\n");
break;
default:
showDataMessage();
showMessageBar();
printf("操作失败! \n");
writeLogTime("SecondUI操作失败...");
break;
}
if(subUser == 1) {
break;
}
inputError();
}
//回到第一个界面
goto firstUI;
writeLogTime("返回FirstUI...");
return 0;
}
ops.h全部代码:
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
//#include <sqlite3.h>
#include "sqlite3.h"
/*需求分析:
定义用户元数据,用户注册系统账号,注册元数据信息,保存在数据库中,但是银行卡处于冻结状态。
用户注册系统账号以后需要开户,读取银行卡ID信息和解冻银行卡状态。
用户进行增删改查的时候需要用户名和密码进行筛选。
用户注销银行卡的时候冻结银行卡ID信息。
只要是普通用户注册priValue都是0
只要是管理员注册rpiValue都是1
*/
void wel(){
Sleep(100);
printf("yyy yyy ggggggggggg hhh tttt \n");
Sleep(100);
printf(" yyy yyy ggg ggg hhh ttt \n");
Sleep(100);
printf(" yyy yyy gggg gg hhh ttt \n");
Sleep(100);
printf(" yyy yyy ggggg gg hhh rrrr ttt \n");
Sleep(100);
printf(" yyy yyy oooooo gg gg hhh uuuu uuuuu rrr ttt \n");
Sleep(100);
printf(" yyyyyyy oo oo gg gggg hhh uuuu uuuu rrr tttttttttttttttttttttt \n");
Sleep(100);
printf(" yyy oo oo gg ggg hhh uuuu uuuu rrrr rrrr ttttttttttttttttttttttt \n");
Sleep(100);
printf(" yyy oo oo ggggg gggg hhhhhhhhhhhhhhhh uuu uuu rrrr rrrrrr ttt \n");
Sleep(100);
printf(" yyy oo oo gggg hhhhhhhhhhhhhhhhhh uuuu uuuu rrrrrr ttt \n");
Sleep(100);
printf(" yyy oo oo ggg hhh hhh uuu uuuu rrrr ttt \n");
Sleep(100);
printf(" yyy oo oo gggg hhh hhh uuu uuu rrr ttt \n");
Sleep(100);
printf(" yyy oo oo ggg ggg hhh hhh uuuu uuuuu rrr ttt \n");
Sleep(100);
printf(" yyy oo oo ggg ggg hhh hhh uuuuu uuuuu rrr ttt \n");
Sleep(100);
printf(" yyy oo oo ggg ggg hhh hhh uuuuuuuuuuuuuuuuuuuuuuu rrr ttt \n");
Sleep(100);
printf(" yyy oo oo ggg ggg hhh hhh uuuu rrr ttt ttttt \n");
Sleep(100);
printf(" yyy oooooo gggg hhh hhhhhhhhh uuuuu rrr ttttttt \n");
Sleep(100);
}
//启动系统显示的界面
void welcome() {
printf("+--------------------------------------------------------------+ \n");
printf("| 欢迎使用Yoghourt银行管理程序! | \n");
printf("|--------------------------------------------------------------| \n");
printf("|开发者信息: | \n");
printf("| 组名=2,Name=许俊杰,Class=[数据库核心开发] \n");
printf("+--------------------------------------------------------------+ \n");
writeLogTime("正在启动...");
}
//显示用户进行什么操作,这是第二个界面
void selectRole() {
printf("+--------------------------------------------------------------+ \n");
printf("| 登录=[0] | \n");
printf("|--------------------------------------------------------------| \n");
printf("| 注册=[1] | \n");
printf("|--------------------------------------------------------------| \n");
printf("| 退出=[2] | \n");
printf("+--------------------------------------------------------------+ \n");
writeLogTime("FirstUI...");
}
//登录后可以进行的操作
void functionSelect() {
printf("+--------------------------------------------------------------+ \n");
printf("| 开户=[0] | \n");
printf("|--------------------------------------------------------------| \n");
printf("| 销户=[1] | \n");
printf("|--------------------------------------------------------------| \n");
printf("| 存款=[2] | \n");
printf("|--------------------------------------------------------------| \n");
printf("| 取款=[3] | \n");
printf("|--------------------------------------------------------------| \n");
printf("| 报表=[4] | \n");
printf("|--------------------------------------------------------------| \n");
printf("| 返回=[5] | \n");
printf("+--------------------------------------------------------------+ \n");
writeLogTime("UserInterface...");
}
//系统管理员额外的权限
void systemFunc() {
printf("+--------------------------------------------------------------+ \n");
printf("| 删除日志=[6] | \n");
printf("|--------------------------------------------------------------| \n");
printf("| 查看日志=[7] | \n");
printf("|--------------------------------------------------------------| \n");
printf("| 删除数据=[8] | \n");
printf("|--------------------------------------------------------------| \n");
printf("| 重新启动=[9] | \n");
printf("|--------------------------------------------------------------| \n");
printf("| 密码更改=[10] | \n");
printf("+--------------------------------------------------------------+ \n");
writeLogTime("AdminInterface...");
}
//显示普通的进度条,耗时操作
void showMessageBar() {
writeLogTime("发送消息...");
int i=0;
printf("0%%");
while(1) {
printf("=");
Sleep(100);
i++;
if(i > 55) {
break;
}
}
printf("100%%");
printf("\n");
}
//显示警告进度条
void showMessageBar_warning() {
writeLogTime("显示警告操作");
int i=0;
printf("0%%");
while(1) {
printf("#");
Sleep(100);
i++;
if(i > 55) {
break;
}
}
printf("100%%");
printf("\n");
}
//提交数据的时候显示
void showDataMessage() {
printf("+--------------------------------------------------------------+ \n");
printf("|正在提交数据... | \n");
printf("+--------------------------------------------------------------+ \n");
}
/*显示系统警告简略信息*/
void systemStop() {
printf("**************************************************************** \n");
printf("***************************系统告警***************************** \n");
printf("**************************************************************** \n");
}
//用户注册的界面
void userReg() {
printf("+--------------------------------------------------------------+ \n");
printf("| 用户注册 | \n");
printf("+--------------------------------------------------------------+ \n");
}
//用户登录的界面
void userLogin() {
printf("+--------------------------------------------------------------+ \n");
printf("| 用户登录 | \n");
printf("+--------------------------------------------------------------+ \n");
}
//用户输入有错误的时候显示
void inputError() {
printf("**************************************************************** \n");
printf("*****************************:(********************************* \n");
printf("**************************************************************** \n");
}
//用户开户的时候显示
void userKaiHu() {
printf("+--------------------------------------------------------------+ \n");
printf("| 建行开户 | \n");
printf("+--------------------------------------------------------------+ \n");
}
//用户销户的时候显示
void userXiaoHu() {
printf("+--------------------------------------------------------------+ \n");
printf("| 建行销户 | \n");
printf("+--------------------------------------------------------------+ \n");
}
//用户存钱的时候显示
void raiseMoney() {
printf("+--------------------------------------------------------------+ \n");
printf("| 便捷储蓄 | \n");
printf("+--------------------------------------------------------------+ \n");
}
//用户取款的时候显示
void reduiceMoney() {
printf("+--------------------------------------------------------------+ \n");
printf("| 便捷取款 | \n");
printf("+--------------------------------------------------------------+ \n");
}
//管理员需要打印所有信息的显示
void showTable() {
printf("+--------------------------------------------------------------+ \n");
printf("| 存取明细 | \n");
printf("+--------------------------------------------------------------+ \n");
}
//查看系统的日志记录的时候显示
void adminShowLog() {
printf("+--------------------------------------------------------------+ \n");
printf("| 日志查看 | \n");
printf("+--------------------------------------------------------------+ \n");
}
//重新初始化系统的时候显示
void adminResetSystem() {
printf("+--------------------------------------------------------------+ \n");
printf("| 重置系统 | \n");
printf("+--------------------------------------------------------------+ \n");
}
//删除日志的时候显示
void adminDeleteLog() {
printf("+--------------------------------------------------------------+ \n");
printf("| 删除日志 | \n");
printf("+--------------------------------------------------------------+ \n");
}
//管理员进行维护的时候显示
void adminDialog() {
printf("+--------------------------------------------------------------+ \n");
printf("| 管理员操作 | \n");
printf("+--------------------------------------------------------------+ \n");
}
//用户显示自己的余额的时候显示
void showUserMoney(long moneyNumber) {
printf("+--------------------------------------------------------------+ \n");
printf("| 您的余额: %d | \n",moneyNumber);
printf("+--------------------------------------------------------------+ \n");
}
struct userInfo {
//int userID;数据库主键
char username[20];
char password[20];
char cardID[40];
int userAge;
char userBirthday[10];
int isUse;
char userSex[10];
int money;
char lastDate[20];
char userRegDate[40];
int priValue;
} userInput;
/*END*/
//显示时间
void showTime() {
struct timeData {
int td_year;
int td_mouth;
int td_day;
int td_hour;
int td_min;
int td_sec;
} xtimeData;
time_t now;
time(&now);
struct tm *tm_now;
tm_now = localtime(&now);
xtimeData.td_year = tm_now->tm_year + 1900;
xtimeData.td_mouth = tm_now->tm_mon + 1;
xtimeData.td_day = tm_now->tm_mday;
xtimeData.td_hour = tm_now->tm_hour;
xtimeData.td_min = tm_now->tm_min;
xtimeData.td_sec = tm_now->tm_sec;
printf("%d",xtimeData.td_year);
printf("年");
printf("%d",xtimeData.td_mouth);
printf("月");
printf("%d",xtimeData.td_day);
printf("日");
printf(" ");
printf("%d",xtimeData.td_hour);
printf(":");
printf("%d",xtimeData.td_min);
printf(":");
printf("%d",xtimeData.td_sec);
printf(" ");
}
//创建日志文件
void touchLogFile() {
FILE *fp = fopen("ba.log","w+");
fclose(fp);
}
//删除日志文件
void removeLogFile() {
printf("正在删除日志文件...");
system("rease ba.log");
}
//删除数据库
void removeDataFile(){
printf("删除数据库... \n");
system("rease data.db");
}
//写入文件日志时间
void writeLogTime(char wmsg[1024]) {
FILE *logF = fopen("ba.log","a");
struct timeData {
int td_year;
int td_mouth;
int td_day;
int td_hour;
int td_min;
int td_sec;
} xtimeData;
time_t now;
time(&now);
struct tm *tm_now;
tm_now = localtime(&now);
xtimeData.td_year = tm_now->tm_year + 1900;
xtimeData.td_mouth = tm_now->tm_mon + 1;
xtimeData.td_day = tm_now->tm_mday;
xtimeData.td_hour = tm_now->tm_hour;
xtimeData.td_min = tm_now->tm_min;
xtimeData.td_sec = tm_now->tm_sec;
fprintf(logF,"%d年",xtimeData.td_year);
fprintf(logF,"%d月",xtimeData.td_mouth);
fprintf(logF,"%d日",xtimeData.td_day);
fprintf(logF,"%d:",xtimeData.td_hour);
fprintf(logF,"%d:",xtimeData.td_min);
fprintf(logF,"%d ",xtimeData.td_sec);
fprintf(logF," ");
fprintf(logF,"%s \n",wmsg);
fclose(logF);
}
/*
输入用户名和密码,使用SQL判断用户名是否存在
存在就存在这个用户,不存在就提示用户注册账户
*/
int userLoginFunc() {
int userLoginFuncMsg = 0;
//select
sqlite3 *db3 = NULL;
int rc3 = sqlite3_open("data.db", &db3);
if( rc3 != 0) {
printf("Open database error! \n");
writeLogTime("数据库打开失败[DB3]");
return -1;
}
printf("Open database is OK!\n");
writeLogTime("数据库打开成功[DB3]");
sqlite3_stmt* stmt3 = NULL;
char sql_3[1024];
printf("请输入您的用户名:");
scanf("%s",&userInput.username);
printf("\n");
printf("请输入您的密码:");
scanf("%s",&userInput.password);
printf("\n");
printf("making select sql... \n");
sprintf(sql_3,"select password from userinfo where username='%s'",userInput.username);
rc3 = sqlite3_prepare_v2(db3, sql_3, -1, &stmt3, NULL);
if(rc3 != SQLITE_OK) {
printf("failed to prepare: %s \n", sqlite3_errmsg(db3));
writeLogTime("SQL指令准备失败[DB3]");
return -1;
}
writeLogTime("SQL指令准备成功[DB3]");
while(1) {
rc3 = sqlite3_step(stmt3);
if(rc3 == SQLITE_ROW) {
const char* resPassword = (const char*) sqlite3_column_text(stmt3, 0);
//printf("%s",resPassword);
int cmpRes = strcmp(resPassword,userInput.password);
if(cmpRes == 0) {
printf("您已经通过登录安全检查... \n");
Sleep(200);
printf("正在登录... \n");
writeLogTime("SQL指令执行成功[DB3]");
userLoginFuncMsg = 1;
int getpriValues = getpriValue(userInput.username,userInput.password);
userLoginFuncMsg = getpriValues +1;//1 2
} else if(cmpRes > 0 || cmpRes < 0) {
printf("登录失败,您输入的用户名或密码错误或包含空格... \n");
Sleep(200);
} else {
printf("请您注册,谢谢合作... \n");
Sleep(200);
}
} else if(rc3 == SQLITE_DONE) {
printf("Finish. \n");
writeLogTime("SQL指令执行完毕[DB3]");
break;
} else {
printf("Error: %s \n", sqlite3_errmsg(db3));
writeLogTime("SQL指令执行失败[DB3]");
break;
}
}
sqlite3_finalize(stmt3);
printf("SELECT : success!\n");
Sleep(200);
sqlite3_close(db3);
printf("Closing database... \n");
writeLogTime("数据库关闭成功[DB3]");
Sleep(200);
printf("SELECT data is successfully !\n");
Sleep(200);
return userLoginFuncMsg;
}
/*
用户注册函数,用户输入元数据,向表中插入一条数据,银行卡处于不可用状态,用户权限是普通用户
*/
void userRegFunc() {
//使用SQL语句获取数据表的最后一行信息
//MySql : SELECT * FROM 表名 ORDER BY 表_ID DESC LIMIT 1
//SQLServer/Oracle : SELECT TOP 1 * FROM 表名 ORDER BY 表_ID DESC
printf("您的用户ID为:\n");
//int id = getId();
sqlite3 *db9 = NULL;
int rc9 = sqlite3_open("data.db", &db9);
if( rc9 != 0) {
printf("Open database error! \n");
writeLogTime("数据库打开失败[DB9]");
Sleep(200);
return;
}
printf("Open database is OK!\n");
writeLogTime("数据库打开成功[DB9]");
Sleep(200);
sqlite3_stmt* stmt9 = NULL;
int resId = 0;
printf("making select sql... \n");
Sleep(200);
char sql_9[1024] = "select id from userinfo order by id desc limit 1";
rc9 = sqlite3_prepare_v2(db9, sql_9, -1, &stmt9, NULL);
if(rc9 != SQLITE_OK)
{
printf("failed to prepare: %s \n", sqlite3_errmsg(db9));
writeLogTime("SQL指令准备失败[DB9]");
return;
}
writeLogTime("SQL指令准备成功[DB9]");
while(1)
{
rc9 = sqlite3_step(stmt9);
if(rc9 == SQLITE_ROW)
{
resId = sqlite3_column_int(stmt9, 0);
writeLogTime("SQL指令执行成功[DB9]");
}
else if(rc9 == SQLITE_DONE)
{
printf("Finish. \n");
writeLogTime("SQL指令执行完毕[DB9]");
Sleep(200);
break;
}
else
{
printf("Error: %s \n", sqlite3_errmsg(db9));
writeLogTime("SQL指令执行失败[DB9]");
break;
}
}
sqlite3_finalize(stmt9);
printf("SELECT : success!\n");
Sleep(200);
sqlite3_close(db9);
printf("Closing database... \n");
writeLogTime("数据库关闭成功[DB9]");
Sleep(200);
printf("SELECT data is successfully !\n");
Sleep(200);
printf("%d \n",resId+1);
int ids = resId + 1;
printf("请输入您的用户名[不超过15字符]:");
scanf("%s",userInput.username);
printf("\n");
printf("您输入的用户名是==%s==\n",userInput.username);
printf("请输入您的密码[不超过8个字符]:");
scanf("%s",userInput.password);
printf("\n");
printf("您输入的密码是==%s==\n",userInput.password);
printf("请输入您的卡号[默认0000000000000000]:");
scanf("%s",userInput.cardID);
printf("\n");
printf("您输入的卡号是==%s==\n",userInput.cardID);
printf("请输入您的年龄:");
scanf("%d",&userInput.userAge);
printf("\n");
printf("您输入的年龄是==%d==\n",userInput.userAge);
printf("请输入您的您的出生日期[YYYYMMDD]:");
scanf("%s",userInput.userBirthday);
printf("\n");
printf("您输入的出生日期是==%s==\n",userInput.userBirthday);
printf("您的账户不可用,请开户激活!\n");
writeLogTime("取消激活账户");
userInput.isUse = 0;
printf("请输入您的性别[man/woman]:");
scanf("%s",userInput.userSex);
printf("\n");
printf("您输入的性别是==%s==\n",userInput.userSex);
Sleep(200);
printf("你的存款为0,请激活后存款!\n");
Sleep(200);
userInput.money = 0;
writeLogTime("删除存款数据");
printf("您最后一次登录的时间是:");//显示最后一次登录的时间
struct timeData {
int td_year;
int td_mouth;
int td_day;
int td_hour;
int td_min;
int td_sec;
} xtimeData;
time_t now;
time(&now);
struct tm *tm_now;
tm_now = localtime(&now);
xtimeData.td_year = tm_now->tm_year + 1900;
xtimeData.td_mouth = tm_now->tm_mon + 1;
xtimeData.td_day = tm_now->tm_mday;
xtimeData.td_hour = tm_now->tm_hour;
xtimeData.td_min = tm_now->tm_min;
xtimeData.td_sec = tm_now->tm_sec;
sprintf(userInput.lastDate,"%dY%dM%dD %d:%d:%d",xtimeData.td_year,xtimeData.td_mouth,xtimeData.td_day,xtimeData.td_hour,xtimeData.td_min,xtimeData.td_sec);
printf("%s \n",userInput.lastDate);
Sleep(200);
writeLogTime("生成SQL指令...");
printf("您的注册日期是:");
sprintf(userInput.userRegDate,"%dY%dM%dD %d:%d:%d",xtimeData.td_year,xtimeData.td_mouth,xtimeData.td_day,xtimeData.td_hour,xtimeData.td_min,xtimeData.td_sec);
printf("%s \n",userInput.userRegDate);
printf("您的权限是[priValue = 0]\n");
writeLogTime("更改用户权限...");
userInput.priValue = 0;
fflush(stdin);
//insertSQL
sqlite3 *db2 = NULL;
int rc2 = sqlite3_open("data.db", &db2);
if( rc2 != 0) {
Sleep(200);
printf("Open database error! \n");
writeLogTime("数据库打开失败[DB2]");
return;
}
Sleep(200);
printf("Open database is OK!\n");
writeLogTime("数据库打开成功[DB2]");
sqlite3_stmt* stmt2 = NULL;
Sleep(200);
printf("making insert sql... \n");
char sql_2[1024];
sprintf(sql_2,"INSERT INTO userinfo(id,username,password,card_id,age,birthday,is_use,user_sex,money,last_date,reg_date,pri_value) VALUES (%d,'%s','%s','%s',%d,'%s',%d,'%s',%d,'%s','%s',%d)",ids,userInput.username,userInput.password,userInput.cardID,userInput.userAge,userInput.userBirthday,userInput.isUse,userInput.userSex,userInput.money,userInput.lastDate,userInput.userRegDate,userInput.priValue);
printf("==%s==",sql_2);
writeLogTime("生成SQL指令...");
/*
char sql_2[1024] ="INSERT INTO userinfo(id,username,password,card_id,age,birthday,is_use,user_sex,money,last_date,reg_date,pri_value) VALUES (:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)";
/*
"create table IF NOT EXISTS userinfo ("
"id integer primary key,"
"username text,"
"password text,"
"card_id text,"
"age integer,"
"birthday text,"
"is_use integer,"
"user_sex text,"
"money integer,"
"last_date text,"
"reg_date text,"
"pri_value integer"
")";
*/
/*
// 绑定参数
sqlite3_bind_int(stmt2, 1, id); //确定用户数据表ID,主键
sqlite3_bind_text(stmt2, 2, userInput.username, -1, NULL); //绑定用户名
sqlite3_bind_text(stmt2, 3, userInput.password, -1, NULL); //绑定密码
sqlite3_bind_text(stmt2, 4, userInput.cardID, -1, NULL); //绑定卡号
sqlite3_bind_int(stmt2, 5, userInput.userAge); //绑定年龄
sqlite3_bind_text(stmt2, 6, userInput.userBirthday, -1, NULL); //绑定出生日期
sqlite3_bind_int(stmt2, 7, userInput.isUse); //绑定是否冻结
sqlite3_bind_text(stmt2, 8, userInput.userSex, -1, NULL); //绑定性别
sqlite3_bind_int(stmt2, 9, userInput.money); //绑定存款金额
sqlite3_bind_text(stmt2, 10, userInput.lastDate, -1, NULL); //绑定最近一次登录时间
sqlite3_bind_text(stmt2, 11, userInput.userRegDate, -1, NULL); //绑定注册日期
//sqlite3_bind_int(stmt2, 12, userInput.priValue); //绑定权限位
*/
// 初始化SQL Statement对象
rc2 = sqlite3_prepare_v2(db2, sql_2, -1, &stmt2, NULL);
if(rc2 != SQLITE_OK) {
printf("failed to prepare: %s \n", sqlite3_errmsg(db2));
writeLogTime("SQL指令准备失败[DB2]");
return;
}
writeLogTime("SQL指令准备成功[DB2]");
// 执行SQL
rc2 = sqlite3_step(stmt2);
if(rc2 != SQLITE_DONE) {
printf("error: %s \n", sqlite3_errmsg(db2));
writeLogTime("SQL指令执行失败[DB2]");
sqlite3_finalize(stmt2);
return;
}
writeLogTime("SQL指令执行成功[DB2]");
sqlite3_finalize(stmt2);
Sleep(200);
printf("INSERT : success!\n");
sqlite3_close(db2);
Sleep(200);
printf("Closing database... \n");
writeLogTime("数据库关闭成功[DB2]");
Sleep(200);
printf("Insert data is successfully !\n");
}
/*
用户开户函数:通过筛选银行卡ID信息,更新用户的银行卡信息冻结状态
*/
void userKaiHuFunc() {
printf("请输入您的银行卡ID:");
scanf("%s",&userInput.cardID);
printf("\n");
//update
sqlite3 *db4 = NULL;
int rc4 = sqlite3_open("data.db", &db4);
Sleep(200);
printf("Open database is successfully... \n");
if( rc4 != 0) {
printf("Error!\n");
writeLogTime("数据库打开失败[DB4]");
return;
}
writeLogTime("数据库打开成功[DB4]");
printf("请输入您的用户名:");
scanf("%s",&userInput.username);
printf("\n");
printf("请输入您的密码:");
scanf("%s",userInput.password);
printf("\n");
Sleep(200);
printf("making update sql... \n");
char sql_4[1024];
sqlite3_stmt* stmt4 = NULL;
sprintf(sql_4,"update userinfo set is_use = 1 where username = '%s' and password = '%s'",userInput.username,userInput.password);
rc4 = sqlite3_prepare_v2(db4, sql_4, -1, &stmt4, NULL);
if(rc4 != SQLITE_OK) {
printf("Failed to prepare: %s \n", sqlite3_errmsg(db4));
writeLogTime("SQL指令准备失败[DB4]");
return ;
}
writeLogTime("SQL指令准备成功[DB4]");
char *errmsg;
rc4 = sqlite3_exec(db4,sql_4,NULL,NULL,&errmsg);
if(errmsg == NULL) {
Sleep(200);
printf("UPDATE database is OK... \n");
writeLogTime("SQL指令执行成功[DB4]");
} else {
printf("Error,%s \n",errmsg);
writeLogTime("SQL指令执行成功[DB4]");
}
sqlite3_close(db4);
writeLogTime("数据库关闭成功[DB4]");
/*
int sqlite3_exec(
sqlite3*,
const char *sql,
int (*callback)(void*,int,char**,char**),
void *,
char **errmsg
);
各个参数的意义为:
sqlite3描述的是数据库句柄
sql 要执行的SQL语句
callback回调函数
void *回调函数的第一个参数
errmsg错误信息,如果没有SQL问题则值为NULL
回调函数式一个比较复杂的函数。它的原型是这样的:
int callback(void *params,int column_size,char **column_value,char **column_name){
每一个参数意义如下:
params是sqlite3_exec传入的第四个参数
column_size是结果字段的个数
column_value是返回记录的一位字符数组指针
column_name是结果字段的名称
通常情况下callback在select操作中会使用到,尤其是处理每一行记录数。返回的结果每一行记录都会调用下“回调函数”。 如果回调函数返回了非0,那么sqlite3_exec将返回SQLITE_ABORT,并且之后的回调函数也不会执行,同时未执行的子查询也不会继续执行。
对于更新、删除、插入等不需要回调函数的操作,sqlite3_exec的第三、第四个参数可以传入0或者NULL。
*/
}
/*
用户销户函数:通过筛选银行卡ID信息,更新用户的银行卡信息冻结状态
*/
void userXiaoHuFunc() {
//update
printf("请输入您的银行卡ID:");
scanf("%s",&userInput.cardID);
printf("\n");
//update
sqlite3 *db5 = NULL;
int rc5 = sqlite3_open("data.db", &db5);
Sleep(200);
if( rc5 != 0) {
printf("Error!\n");
writeLogTime("数据库打开失败[DB5]");
return;
}
printf("Open database is successfully... \n");
writeLogTime("数据库打开成功[DB5]");
writeLogTime("");
printf("请输入您的用户名:");
scanf("%s",&userInput.username);
printf("\n");
printf("请输入您的密码:");
scanf("%s",userInput.password);
printf("\n");
Sleep(200);
printf("making update sql... \n");
char sql_5[1024];
sqlite3_stmt* stmt5 = NULL;
sprintf(sql_5,"update userinfo set is_use = 0 where username = '%s' and password = '%s'",userInput.username,userInput.password);
rc5 = sqlite3_prepare_v2(db5, sql_5, -1, &stmt5, NULL);
if(rc5 != SQLITE_OK) {
printf("Failed to prepare: %s \n", sqlite3_errmsg(db5));
writeLogTime("SQL指令准备失败[DB5]");
return ;
}
writeLogTime("SQL指令准备成功[DB5]");
char *errmsg;
rc5 = sqlite3_exec(db5,sql_5,NULL,NULL,&errmsg);
if(errmsg == NULL) {
Sleep(200);
printf("UPDATE database is OK... \n");
writeLogTime("SQL指令执行成功[DB5]");
} else {
printf("Error,%s \n",errmsg);
writeLogTime("SQL指令执行成功[DB5]");
}
sqlite3_close(db5);
writeLogTime("数据库关闭成功[DB5]");
}
long getUserMoney(char userName[20],char passWord[20]) {
printf("Get data as message for sql userName==%s== && passWord==%s== \n",userName,passWord);
sqlite3 *db8 = NULL;
int rc8 = sqlite3_open("data.db", &db8);
Sleep(200);
if( rc8 != 0) {
printf("Error!\n");
writeLogTime("数据库打开失败[DB8]");
return -1;
}
printf("Open database is successfully... \n");
writeLogTime("数据库打开成功[DB8]");
char sql_8[1024];
long resMoney = 0;
sqlite3_stmt* stmt8 = NULL;
Sleep(200);
printf("making select sql... \n");
sprintf(sql_8,"select money from userinfo where username='%s' and password='%s'",userName,passWord);
rc8 = sqlite3_prepare_v2(db8, sql_8, -1, &stmt8, NULL);
if(rc8 != SQLITE_OK) {
printf("failed to prepare: %s \n", sqlite3_errmsg(db8));
writeLogTime("SQL指令准备失败[DB8]");
return -1;
}
writeLogTime("SQL指令准备成功[DB8]");
while(1) {
rc8 = sqlite3_step(stmt8);
if(rc8 == SQLITE_ROW) {
resMoney = sqlite3_column_int(stmt8, 0);
} else if(rc8 == SQLITE_DONE) {
printf("Finish. \n");
writeLogTime("SQL指令执行完毕[DB8]");
break;
} else {
printf("Error: %s \n", sqlite3_errmsg(db8));
writeLogTime("SQL指令执行失败[DB8]");
break;
}
}
sqlite3_finalize(stmt8);
Sleep(200);
printf("SELECT : success!\n");
sqlite3_close(db8);
Sleep(200);
printf("Closing database... \n");
writeLogTime("数据库打开成功[DB8]");
Sleep(200);
printf("SELECT data is successfully !\n");
return resMoney;
}
int getpriValue(char userName[20],char passWord[20]) {
printf("Get data as message for sql userName==%s== && passWord==%s== \n",userName,passWord);
sqlite3 *db8 = NULL;
int rc8 = sqlite3_open("data.db", &db8);
Sleep(200);
if( rc8 != 0) {
printf("Error!\n");
writeLogTime("数据库打开失败[DB8]");
return -1;
}
printf("Open database is successfully... \n");
writeLogTime("数据库打开成功[DB8]");
char sql_8[1024];
int resPri = 0;
sqlite3_stmt* stmt8 = NULL;
Sleep(200);
printf("making select sql... \n");
sprintf(sql_8,"select pri_value from userinfo where username='%s' and password='%s'",userName,passWord);
rc8 = sqlite3_prepare_v2(db8, sql_8, -1, &stmt8, NULL);
if(rc8 != SQLITE_OK) {
printf("failed to prepare: %s \n", sqlite3_errmsg(db8));
writeLogTime("SQL指令准备失败[DB8]");
return -1;
}
writeLogTime("SQL指令准备成功[DB8]");
while(1) {
rc8 = sqlite3_step(stmt8);
if(rc8 == SQLITE_ROW) {
resPri = sqlite3_column_int(stmt8, 0);
} else if(rc8 == SQLITE_DONE) {
printf("Finish. \n");
writeLogTime("SQL指令执行完毕[DB8]");
break;
} else {
printf("Error: %s \n", sqlite3_errmsg(db8));
writeLogTime("SQL语句执行失败[DB8]");
break;
}
}
sqlite3_finalize(stmt8);
Sleep(200);
printf("SELECT : success!\n");
sqlite3_close(db8);
Sleep(200);
printf("Closing database... \n");
writeLogTime("数据库关闭成功[DB8]");
Sleep(200);
printf("SELECT data is successfully !\n");
return resPri;
}
/*
用户增加金额函数:通过筛选银行卡ID信息,更新金额数值
*/
void raiseMoneyFunc() {
printf("请输入您的存款金额数量[每次只允许储蓄¥100以内]:");
scanf("%d",&userInput.money);
printf("\n");
//update
printf("请输入您的银行卡ID:");
scanf("%s",&userInput.cardID);
printf("\n");
sqlite3 *db6 = NULL;
int rc6 = sqlite3_open("data.db", &db6);
if( rc6 != 0) {
printf("Error!\n");
writeLogTime("数据库打开失败[DB6]");
return ;
}
printf("Open database is successfully... \n");
writeLogTime("数据库打开成功[DB6]");
printf("请输入您的用户名:");
scanf("%s",&userInput.username);
printf("\n");
printf("请输入您的密码:");
scanf("%s",userInput.password);
printf("\n");
long quonMoney = getUserMoney(userInput.username,userInput.password);
long modifyMoney = userInput.money + quonMoney;
Sleep(200);
printf("making update sql... \n");
char sql_6[1024];
sqlite3_stmt* stmt6 = NULL;
sprintf(sql_6,"update userinfo set money = %d where username = '%s' and password = '%s'",modifyMoney,userInput.username,userInput.password);
rc6 = sqlite3_prepare_v2(db6, sql_6, -1, &stmt6, NULL);
if(rc6 != SQLITE_OK) {
printf("Failed to prepare: %s \n", sqlite3_errmsg(db6));
writeLogTime("SQL指令准备失败[DB6]");
return ;
}
writeLogTime("SQL指令准备成功[DB6]");
char *errmsg;
rc6 = sqlite3_exec(db6,sql_6,NULL,NULL,&errmsg);
if(errmsg == NULL) {
Sleep(200);
printf("UPDATE database is OK... \n");
writeLogTime("SQL指令执行成功[DB6]");
} else {
printf("Error,%s \n",errmsg);
writeLogTime("SQl指令执行失败[DB6]");
}
sqlite3_close(db6);
writeLogTime("数据库关闭成功[DB6]");
}
/*
用户减少金额函数:通过筛选银行卡ID信息,更新金额数值
*/
void reduiceMoneyFunc() {
printf("请输入取款金额数量[每次只允许取款¥1000以内]:");
//update
scanf("%d",&userInput.money);
printf("\n");
//update
printf("请输入您的银行卡ID:");
scanf("%s",&userInput.cardID);
printf("\n");
sqlite3 *db7 = NULL;
int rc7 = sqlite3_open("data.db", &db7);
Sleep(200);
printf("Open database is successfully... \n");
if( rc7 != 0) {
printf("Error!\n");
writeLogTime("数据库打开失败[DB7]");
return ;
}
writeLogTime("数据库打开成功[DB7]");
printf("请输入您的用户名:");
scanf("%s",&userInput.username);
printf("\n");
printf("请输入您的密码:");
scanf("%s",userInput.password);
printf("\n");
long quonMoney = getUserMoney(userInput.username,userInput.password);
long modifyMoney = quonMoney - userInput.money;
if(modifyMoney < 0) {
Sleep(200);
printf("您还没有存款哦... \n");
Sleep(200);
printf("系统忠实记录了您已经贷款... \n");
Sleep(200);
printf("贷款信息写入... \n");
}
Sleep(200);
printf("making update sql... \n");
char sql_7[1024];
sqlite3_stmt* stmt7 = NULL;
sprintf(sql_7,"update userinfo set money = %d where username = '%s' and password = '%s'",modifyMoney,userInput.username,userInput.password);
rc7 = sqlite3_prepare_v2(db7, sql_7, -1, &stmt7, NULL);
if(rc7 != SQLITE_OK) {
printf("Failed to prepare: %s \n", sqlite3_errmsg(db7));
writeLogTime("SQL指令准备失败[DB7]");
return;
}
writeLogTime("SQL指令准备成功[DB7]");
char *errmsg;
rc7 = sqlite3_exec(db7,sql_7,NULL,NULL,&errmsg);
if(errmsg == NULL) {
printf("UPDATE database is OK... \n");
writeLogTime("SQL指令准备失败[DB7]");
} else {
printf("Error,%s \n",errmsg);
writeLogTime("SQL指令准备成功[DB7]");
}
sqlite3_close(db7);
writeLogTime("数据库关闭成功[DB7]");
}
/*程序的初始化函数*/
int initFunc() {
Sleep(200);
printf("Initializing... \n");
//创建日志文件
Sleep(200);
printf("Create log file...\n");
//touchLogFile();
Sleep(200);
printf("Create data file... \n");
sqlite3 *db = NULL;
//打开数据库
int rc = sqlite3_open("data.db", &db);
//判断数据库是否打开
if( rc != 0) {
Sleep(200);
printf("Open database error! \n");
return -1;
}
Sleep(200);
printf("Open database is OK!\n");
sqlite3_stmt* stmt = NULL;
Sleep(200);
printf("making create table sql... \n");
//创建SQL语句
char sql[1024] =
"create table IF NOT EXISTS userinfo ("
"id integer primary key AUTOINCREMENT,"
"username text,"
"password text,"
"card_id text,"
"age integer,"
"birthday text,"
"is_use integer,"
"user_sex text,"
"money integer,"
"last_date text,"
"reg_date text,"
"pri_value integer"
")";
//准备SQL语句
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if(rc != SQLITE_OK) {
Sleep(200);
printf("Failed to prepare: %s \n", sqlite3_errmsg(db));
return -1;
}
//执行SQL语句
rc = sqlite3_step(stmt);
if(rc != SQLITE_DONE) {
printf("Error: %s \n", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
return -1;
}
sqlite3_finalize(stmt);
//关闭数据库
sqlite3_close(db);
Sleep(200);
printf("Closing database... \n");
Sleep(200);
printf("Create a datasheet successfully !\n");
Sleep(200);
printf("Wait for the system to start... \n");
system("cls");
int gangCount = 0;
for(gangCount;gangCount < 10;gangCount++){
printf("|");
Sleep(100);
system("cls");
printf("/");
Sleep(100);
system("cls");
printf("-");
Sleep(100);
system("cls");
printf("|");
Sleep(100);
system("cls");
printf("\\");
Sleep(100);
system("cls");
}
printf("\n");
return 0;
}