如何使用C语言SQLite3编程?大一课程设计看这里!

如何使用C语言SQLite3编程?大一课程设计看这里!,第1张

概述本程序由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 <stdl

本程序由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();

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);

}

} else if(initResultinput == 1) {

printf("Stating... \n");

printf("Error :( \n");

}

int main() {

wel();

isInit();

writeLogTime("正在初始化系统...");

/*Some Values*/

int selectRoleValue;

//显示欢迎信息

welcome();

writeLogTime("进入FirstUI界面...");

firstUI:

while(1) {

//选择需要的 *** 作

selectRole();

printf("请输入 *** 作代码:");

getchar();

Sleep(200);

printf("\n");

if(selectRoleValue == 0) {

break;

if(selectRoleValue == 1) {

if(selectRoleValue == 2) {

inputError();

int userLoginFunCMSgRecv = 0;

//登录

userLogin();

writeLogTime("用户登录...");

//userLoginFunCMSgRecv = userLoginFunc();

if(userLoginFunCMSgRecv == 1 || userLoginFunCMSgRecv == 2) {

//注册

userReg();

writeLogTime("用户注册...");

userRegFunc();

//回到第一个界面

goto firstUI;

//退出

printf("Shutdown the System... \n");

showMessagebar_warning();

writeLogTime("系统退出...");

exit(0);

int subUser = 0;

if(userLoginFunCMSgRecv == 1) {

functionSelect();

if(userLoginFunCMSgRecv == 2) {

adminDialog();

systemFunc();

printf("请输入代码:");

int userSelectValue;

printf("您选择的是[%d] \n",userSelectValue);

showDataMessage();

switch(userSelectValue) {

case 0:

userKaiHu();

userKaiHuFunc();

writeLogTime("用户开户...");

case 1:

userXiaoHu();

userXiaoHuFunc();

writeLogTime("用户销户...");

case 2:

raiseMoney();

raiseMoneyFunc();

writeLogTime("用户存款...");

case 3:

reduiceMoney();

reduiceMoneyFunc();

writeLogTime("用户提款...");

case 4:

showtable();

writeLogTime("用户报表...");

showTime();

printf("请输入您的用户名:");

char un[20];

scanf("%s",&un);

char pw[20];

printf("请输入您的密码:");

long mm;

Sleep(100);

mm = getUserMoney(un,pw);


showUserMoney(mm);

case 5:

subUser = 1;

case 6:

systemStop();

adminDeleteLog();

writeLogTime("删除日志文件...");

removeLogfile();

case 7:

adminShowLog();

writeLogTime("查看日志文件...");

system("less ba.log");

case 8:

adminresetSystem();

writeLogTime("删除数据...");

removeDatafile();

writeLogTime("");

case 9:

writeLogTime("重新启动...");

case 10:

writeLogTime("密码更改...");

printf("暂不支持管理员密码更改和用户权限更改,请联系柜台, *** 作后台数据库!\n");

break;


default:

printf(" *** 作失败! \n");

writeLogTime("SecondUI *** 作失败...");

if(subUser == 1) {

writeLogTime("返回FirstUI...");

return 0;

}


ops.h全部代码:

#include <windows.h>

#include <string.h>

#include <time.h>

//#include <sqlite3.h>

#include "sqlite3.h"

/*需求分析:

定义用户元数据,用户注册系统账号,注册元数据信息,保存在数据库中,但是yhk处于冻结状态。

用户注册系统账号以后需要开户,读取yhkID信息和解冻yhk状态。

用户进行增删改查的时候需要用户名和密码进行筛选。

用户注销yhk的时候冻结yhkID信息。

只要是普通用户注册priValue都是0

只要是管理员注册rpiValue都是1


*/

voID wel(){

printf("yyy yyy ggggggggggg hhh tttt \n");

printf(" yyy yyy ggg ggg hhh ttt \n");

printf(" yyy yyy gggg gg hhh ttt \n");

printf(" yyy yyy ggggg gg hhh rrrr ttt \n");

printf(" yyy yyy oooooo gg gg hhh uuuu uuuuu rrr ttt \n");

printf(" yyyyyyy oo oo gg gggg hhh uuuu uuuu rrr tttttttttttttttttttttt \n");

printf(" yyy oo oo gg ggg hhh uuuu uuuu rrrr rrrr ttttttttttttttttttttttt \n");

printf(" yyy oo oo ggggg gggg hhhhhhhhhhhhhhhh uuu uuu rrrr rrrrrr ttt \n");

printf(" yyy oo oo gggg hhhhhhhhhhhhhhhhhh uuuu uuuu rrrrrr ttt \n");

printf(" yyy oo oo ggg hhh hhh uuu uuuu rrrr ttt \n");

printf(" yyy oo oo gggg hhh hhh uuu uuu rrr ttt \n");

printf(" yyy oo oo ggg ggg hhh hhh uuuu uuuuu rrr ttt \n");

printf(" yyy oo oo ggg ggg hhh hhh uuuuu uuuuu rrr ttt \n");

printf(" yyy oo oo ggg ggg hhh hhh uuuuuuuuuuuuuuuuuuuuuuu rrr ttt \n");

printf(" yyy oo oo ggg ggg hhh hhh uuuu rrr ttt ttttt \n");

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");

writeLogTime("正在启动...");

//显示用户进行什么 *** 作,这是第二个界面

voID selectRole() {

printf("| 登录=[0] | \n");

printf("| 注册=[1] | \n");

printf("| 退出=[2] | \n");

writeLogTime("FirstUI...");

//登录后可以进行的 *** 作

voID functionSelect() {

printf("| 开户=[0] | \n");

printf("| 销户=[1] | \n");

printf("| 存款=[2] | \n");

printf("| 取款=[3] | \n");

printf("| 报表=[4] | \n");

printf("| 返回=[5] | \n");

writeLogTime("UserInterface...");

//系统管理员额外的权限

voID systemFunc() {

printf("| 删除日志=[6] | \n");

printf("| 查看日志=[7] | \n");

printf("| 删除数据=[8] | \n");

printf("| 重新启动=[9] | \n");

printf("| 密码更改=[10] | \n");

writeLogTime("adminInterface...");

//显示普通的进度条,耗时 *** 作

voID showMessagebar() {

writeLogTime("发送消息...");

int i=0;


printf("0%%");

printf("=");

i++;

if(i > 55) {

printf("100%%");

printf("\n");



//显示警告进度条

voID showMessagebar_warning() {

writeLogTime("显示警告 *** 作");

printf("#");

//提交数据的时候显示

voID showDataMessage() {


printf("|正在提交数据... | \n");

printf("+--------------------------------------------------------------+ \n");


/*显示系统警告简略信息*/

voID systemStop() {


printf("**************************************************************** \n");

printf("***************************系统告警***************************** \n");

printf("**************************************************************** \n");


//用户注册的界面

voID userReg() {


printf("| 用户注册 | \n");

//用户登录的界面

voID userLogin() {


printf("| 用户登录 | \n");

//用户输入有错误的时候显示

voID inputError() {


printf("*****************************:(********************************* \n");

//用户开户的时候显示

voID userKaiHu() {


printf("| 建行开户 | \n");

//用户销户的时候显示

voID userXiaoHu() {


printf("| 建行销户 | \n");

//用户存钱的时候显示

voID raiseMoney() {


printf("| 便捷储蓄 | \n");

//用户取款的时候显示

voID reduiceMoney() {

printf("| 便捷取款 | \n");

//管理员需要打印所有信息的显示

voID showtable() {


printf("| 存取明细 | \n");

//查看系统的日志记录的时候显示

voID adminShowLog() {


printf("| 日志查看 | \n");

//重新初始化系统的时候显示

voID adminresetSystem() {

printf("| 重置系统 | \n");

//删除日志的时候显示

voID adminDeleteLog() {


printf("| 删除日志 | \n");

//管理员进行维护的时候显示

voID adminDialog() {


printf("| 管理员 *** 作 | \n");

//用户显示自己的余额的时候显示

voID showUserMoney(long moneyNumber) {


printf("| 您的余额: %d | \n",moneyNumber);

}



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("月");

printf("日");

printf(" ");

printf(":");

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");

fprintf(logF,"%d年","%d月","%d日","%d:","%d "," ");

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("请输入您的用户名:");

printf("请输入您的密码:");

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]");

writeLogTime("sql指令准备成功[DB3]");

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");

printf("正在登录... \n");

writeLogTime("sql指令执行成功[DB3]");

userLoginFunCMSg = 1;

int getpriValues = getpriValue(userinput.username,80); white-space: pre;">userLoginFunCMSg = getpriValues +1;//1 2

} else if(cmpRes > 0 || cmpRes < 0) {

printf("登录失败,您输入的用户名或密码错误或包含空格... \n");

printf("请您注册,谢谢合作... \n");

} else if(rc3 == sqlITE_DONE) {

printf("Finish. \n");

writeLogTime("sql指令执行完毕[DB3]");

printf("Error: %s \n",80); white-space: pre;">writeLogTime("sql指令执行失败[DB3]");

}


sqlite3_finalize(stmt3);

printf("SELECT : success!\n");

sqlite3_close(db3);

printf("Closing database... \n");

writeLogTime("数据库关闭成功[DB3]");

printf("SELECT data is successfully !\n");

return userLoginFunCMSg;

用户注册函数,用户输入元数据,向表中插入一条数据,yhk处于不可用状态,用户权限是普通用户

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) {

writeLogTime("数据库打开失败[DB9]");

return;

writeLogTime("数据库打开成功[DB9]");

sqlite3_stmt* stmt9 = NulL;

int resID = 0;

char sql_9[1024] = "select ID from userinfo order by ID desc limit 1";

rc9 = sqlite3_prepare_v2(db9,sql_9,&stmt9,80); white-space: pre;">if(rc9 != sqlITE_OK)

{

writeLogTime("sql指令准备失败[DB9]");

writeLogTime("sql指令准备成功[DB9]");

while(1)

rc9 = sqlite3_step(stmt9);

if(rc9 == sqlITE_ROW)

resID = sqlite3_column_int(stmt9,80); white-space: pre;">writeLogTime("sql指令执行成功[DB9]");

else if(rc9 == sqlITE_DONE)

writeLogTime("sql指令执行完毕[DB9]");

else

writeLogTime("sql指令执行失败[DB9]");

sqlite3_finalize(stmt9);

sqlite3_close(db9);

writeLogTime("数据库关闭成功[DB9]");

int IDs = resID + 1;


printf("请输入您的用户名[不超过15字符]:");

printf("您输入的用户名是==%s==\n",userinput.username);



printf("请输入您的密码[不超过8个字符]:");

printf("您输入的密码是==%s==\n",userinput.password);


printf("请输入您的卡号[默认0000000000000000]:");

printf("您输入的卡号是==%s==\n",userinput.cardID);


printf("请输入您的年龄:");

printf("您输入的年龄是==%d==\n",userinput.userAge);


printf("请输入您的您的出生日期[YYYYMMDD]:");

printf("您输入的出生日期是==%s==\n",userinput.userBirthday);


printf("您的账户不可用,请开户激活!\n");

writeLogTime("取消激活账户");


userinput.isUse = 0;


printf("请输入您的性别[man/woman]:");

printf("您输入的性别是==%s==\n",80); white-space: pre;">Sleep(200);


printf("你的存款为0,请激活后存款!\n");

userinput.money = 0;

writeLogTime("删除存款数据");


printf("您最后一次登录的时间是:");//显示最后一次登录的时间


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,80); white-space: pre;">printf("%s \n",userinput.lastDate);

writeLogTime("生成sql指令...");

printf("您的注册日期是:");

sprintf(userinput.userRegDate,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) {

writeLogTime("数据库打开失败[DB2]");

writeLogTime("数据库打开成功[DB2]");

sqlite3_stmt* stmt2 = NulL;

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',%d,%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);

/*

char sql_2[1024] ="INSERT INTO userinfo(ID,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,80); white-space: pre;"> "password text,80); white-space: pre;"> "card_ID text,80); white-space: pre;"> "age integer,80); white-space: pre;"> "birthday text,80); white-space: pre;"> "is_use integer,80); white-space: pre;"> "user_sex text,80); white-space: pre;"> "money integer,80); white-space: pre;"> "last_date text,80); white-space: pre;"> "reg_date text,80); white-space: pre;"> "pri_value integer"

")";

*/


// 绑定参数

sqlite3_bind_int(stmt2,1,ID); //确定用户数据表ID,主键

sqlite3_bind_text(stmt2,2,NulL); //绑定用户名

//sqlite3_bind_int(stmt2,12,userinput.priValue); //绑定权限位

// 初始化sql Statement对象

rc2 = sqlite3_prepare_v2(db2,sql_2,&stmt2,80); white-space: pre;">if(rc2 != sqlITE_OK) {

writeLogTime("sql指令准备失败[DB2]");

writeLogTime("sql指令准备成功[DB2]");

// 执行sql

rc2 = sqlite3_step(stmt2);

if(rc2 != sqlITE_DONE) {

printf("error: %s \n",80); white-space: pre;">writeLogTime("sql指令执行失败[DB2]");

sqlite3_finalize(stmt2);

writeLogTime("sql指令执行成功[DB2]");


printf("INSERT : success!\n");

sqlite3_close(db2);

writeLogTime("数据库关闭成功[DB2]");

printf("Insert data is successfully !\n");


用户开户函数:通过筛选yhkID信息,更新用户的yhk信息冻结状态

voID userKaiHuFunc() {


printf("请输入您的yhkID:");

//update


sqlite3 *db4 = NulL;

int rc4 = sqlite3_open("data.db",&db4);

printf("Open database is successfully... \n");

if( rc4 != 0) {

printf("Error!\n");

writeLogTime("数据库打开失败[DB4]");

writeLogTime("数据库打开成功[DB4]");

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.password);

rc4 = sqlite3_prepare_v2(db4,sql_4,&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,NulL,&errmsg);


if(errmsg == NulL) {

printf("UPDATE database is OK... \n");

writeLogTime("sql指令执行成功[DB4]");

printf("Error,%s \n",errmsg);

sqlite3_close(db4);

writeLogTime("数据库关闭成功[DB4]");


int sqlite3_exec(

sqlite3*,

const char *sql,80); white-space: pre;"> int (*callback)(voID*,int,char**,char**),80); white-space: pre;"> voID *,80); white-space: pre;"> 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。

用户销户函数:通过筛选yhkID信息,更新用户的yhk信息冻结状态

voID userXiaoHuFunc() {


//update

sqlite3 *db5 = NulL;

int rc5 = sqlite3_open("data.db",&db5);

Sleep(200);

if( rc5 != 0) {

writeLogTime("数据库打开失败[DB5]");

printf("Open database is successfully... \n");

writeLogTime("数据库打开成功[DB5]");

char sql_5[1024];

sqlite3_stmt* stmt5 = NulL;

sprintf(sql_5,"update userinfo set is_use = 0 where username = '%s' and password = '%s'",80); white-space: pre;">rc5 = sqlite3_prepare_v2(db5,sql_5,&stmt5,80); white-space: pre;">if(rc5 != sqlITE_OK) {

writeLogTime("sql指令准备失败[DB5]");

writeLogTime("sql指令准备成功[DB5]");

rc5 = sqlite3_exec(db5,80); white-space: pre;">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);

if( rc8 != 0) {

writeLogTime("数据库打开失败[DB8]");

writeLogTime("数据库打开成功[DB8]");

char sql_8[1024];

long resMoney = 0;

sqlite3_stmt* stmt8 = NulL;

sprintf(sql_8,"select money from userinfo where username='%s' and password='%s'",80); white-space: pre;">rc8 = sqlite3_prepare_v2(db8,sql_8,&stmt8,80); white-space: pre;">if(rc8 != sqlITE_OK) {

writeLogTime("sql指令准备失败[DB8]");

writeLogTime("sql指令准备成功[DB8]");

rc8 = sqlite3_step(stmt8);


if(rc8 == sqlITE_ROW) {

resMoney = sqlite3_column_int(stmt8,80); white-space: pre;">} else if(rc8 == sqlITE_DONE) {

writeLogTime("sql指令执行完毕[DB8]");

writeLogTime("sql指令执行失败[DB8]");

sqlite3_finalize(stmt8);

sqlite3_close(db8);

printf("SELECT data is successfully !\n");


return resMoney;

int getpriValue(char username[20],80); white-space: pre;">int resPri = 0;

resPri = sqlite3_column_int(stmt8,80); white-space: pre;">writeLogTime("SQL语句执行失败[DB8]");

writeLogTime("数据库关闭成功[DB8]");

return resPri;

用户增加金额函数:通过筛选yhkID信息,更新金额数值

voID raiseMoneyFunc() {


printf("请输入您的存款金额数量[每次只允许储蓄¥100以内]:");

sqlite3 *db6 = NulL;

int rc6 = sqlite3_open("data.db",&db6);

if( rc6 != 0) {

writeLogTime("数据库打开失败[DB6]");

writeLogTime("数据库打开成功[DB6]");

long quonMoney = getUserMoney(userinput.username,80); white-space: pre;">long modifyMoney = userinput.money + quonMoney;

char sql_6[1024];

sqlite3_stmt* stmt6 = NulL;

sprintf(sql_6,"update userinfo set money = %d where username = '%s' and password = '%s'",modifyMoney,80); white-space: pre;">rc6 = sqlite3_prepare_v2(db6,sql_6,&stmt6,80); white-space: pre;">if(rc6 != sqlITE_OK) {

writeLogTime("sql指令准备失败[DB6]");

writeLogTime("sql指令准备成功[DB6]");

rc6 = sqlite3_exec(db6,80); white-space: pre;">writeLogTime("sql指令执行成功[DB6]");

writeLogTime("sql指令执行失败[DB6]");

sqlite3_close(db6);

writeLogTime("数据库关闭成功[DB6]");

用户减少金额函数:通过筛选yhkID信息,更新金额数值

voID reduiceMoneyFunc() {


printf("请输入取款金额数量[每次只允许取款¥1000以内]:");

sqlite3 *db7 = NulL;

int rc7 = sqlite3_open("data.db",&db7);

if( rc7 != 0) {

writeLogTime("数据库打开失败[DB7]");

writeLogTime("数据库打开成功[DB7]");

long modifyMoney = quonMoney - userinput.money;

if(modifyMoney < 0) {

printf("您还没有存款哦... \n");

printf("系统忠实记录了您已经贷款... \n");

printf("贷款信息写入... \n");

char sql_7[1024];

sqlite3_stmt* stmt7 = NulL;

sprintf(sql_7,80); white-space: pre;">rc7 = sqlite3_prepare_v2(db7,sql_7,&stmt7,80); white-space: pre;">if(rc7 != sqlITE_OK) {

writeLogTime("sql指令准备失败[DB7]");

writeLogTime("sql指令准备成功[DB7]");

rc7 = sqlite3_exec(db7,80); white-space: pre;">sqlite3_close(db7);

writeLogTime("数据库关闭成功[DB7]");

/*程序的初始化函数*/

int initFunc() {

printf("Initializing... \n");

//创建日志文件

printf("Create log file...\n");

//touchLogfile();

printf("Create data file... \n");

sqlite3 *db = NulL;

//打开数据库

int rc = sqlite3_open("data.db",&db);

//判断数据库是否打开

if( rc != 0) {

sqlite3_stmt* stmt = NulL;

printf("making create table sql... \n");

//创建SQL语句

char sql[1024] =

"create table IF NOT EXISTS userinfo ("

"ID integer primary key autoINCREMENT,80); white-space: pre;">//准备SQL语句

rc = sqlite3_prepare_v2(db,sql,&stmt,80); white-space: pre;">if(rc != sqlITE_OK) {

//执行SQL语句

rc = sqlite3_step(stmt);

if(rc != sqlITE_DONE) {

sqlite3_finalize(stmt);

//关闭数据库

sqlite3_close(db);

printf("Create a datasheet successfully !\n");

printf("Wait for the system to start... \n");

system("cls");

int gangCount = 0;

for(gangCount;gangCount < 10;gangCount++){

printf("|");

printf("/");

printf("-");

printf("\\");

Sleep(100);

}

总结

以上是内存溢出为你收集整理的如何使用C语言SQLite3编程?大一课程设计看这里!全部内容,希望文章能够帮你解决如何使用C语言SQLite3编程?大一课程设计看这里!所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1162383.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存