当前位置:网站首页>c语言与数据库的创建使用
c语言与数据库的创建使用
2022-06-25 21:47:00 【游戏编程】
c语言与数据库的创建使用
data.h头文件
/********************************************************************************************** * :|x]. ?ObU: +jfl ?Zdr' '"I>>iI"' * n$$${ [email protected]$$k; n$$$Mi [B$$$c "-xZ*%$$$$$%*pX+ * >8$$k` j$$$C^ ]$$$$$Q. `k$$$$8l ~Y#$$%kQznnuY0qhk| * j$$$n {@$$0' .q$$$$$B< ($$$$$$/ 'uB$$p{: * 'm$$B+ +&$$b, [email protected]$$B$$$z ;#$$8%$$Z. +$$$M: * !&$$b^ !*$$#! C$$$\k$$M: [email protected]}X$$8! ,w$$$bc|}-~<iI". * f$$$x :k$$&+ _B$$p./$$$x /$$$u [email protected]$$x '[[email protected]$$$$$$$%oOr> * 'w$$%~ ^w$$B} `[email protected] "h$$Wl _8$$O' 'p$$o" ^:!><+](n0#[email protected]> * >8$$b^ 'L$$$\ x$$$C )$$$C Io$$a; ($$$t `[#$$*: * /$$$X.c$$$x <8$$&l [email protected]\w$$%+ "a$$#; ,]]" p$$$> * [email protected]$$$Y. .O$$$n >8$$$$$$t \$$$J 0$$*\" '!(p$$$u. * "d$$$$$Q' >B$$%i r$$$$$L. ^[email protected]{ _b$$$&qJvnnncCq#$$$&Q- * lh$$$Y' [$$$U `[email protected]^ {@$$a^ .+jQk&@[email protected]&aOn[: * :\r] '{x)^ .+}> ?UJ). ^:l>>>l:^. * * *********************************************************************************************//* 主程序数据处理头文件 *//* 在线用户结构体 */struct ONLINE_USER{ int fd; //-1 int flage; // registed or not char name[32]; char passwd[32];};/* 添加一个用户 *//* 客户端发送注册请求后,服务器端注册用户信息到数据库中 */int db_add_user(char name[], char passwd[]);/* 判断某个用户名是否注册 *//* 功能:判断某个用户名是否注册 返回值:有名字返回index,否则返回-1 */int db_user_if_reg(char *name);/* 判断用户名密码是否正确 */int db_user_pwd_corrct(char *name, char *passwd);/* 数据库初始化 */int database_init();/* 关闭数据库 */void database_close();data.c实现文件
/* 主程序数据处理源文件 */#include <stdio.h>#include <sqlite3.h>#include <time.h>#include <sys/types.h>#include <unistd.h>/********************************************************************************************** * :|x]. ?ObU: +jfl ?Zdr' '"I>>iI"' * n$$${ [email protected]$$k; n$$$Mi [B$$$c "-xZ*%$$$$$%*pX+ * >8$$k` j$$$C^ ]$$$$$Q. `k$$$$8l ~Y#$$%kQznnuY0qhk| * j$$$n {@$$0' .q$$$$$B< ($$$$$$/ 'uB$$p{: * 'm$$B+ +&$$b, [email protected]$$B$$$z ;#$$8%$$Z. +$$$M: * !&$$b^ !*$$#! C$$$\k$$M: [email protected]}X$$8! ,w$$$bc|}-~<iI". * f$$$x :k$$&+ _B$$p./$$$x /$$$u [email protected]$$x '[[email protected]$$$$$$$%oOr> * 'w$$%~ ^w$$B} `[email protected] "h$$Wl _8$$O' 'p$$o" ^:!><+](n0#[email protected]> * >8$$b^ 'L$$$\ x$$$C )$$$C Io$$a; ($$$t `[#$$*: * /$$$X.c$$$x <8$$&l [email protected]\w$$%+ "a$$#; ,]]" p$$$> * [email protected]$$$Y. .O$$$n >8$$$$$$t \$$$J 0$$*\" '!(p$$$u. * "d$$$$$Q' >B$$%i r$$$$$L. ^[email protected]{ _b$$$&qJvnnncCq#$$$&Q- * lh$$$Y' [$$$U `[email protected]^ {@$$a^ .+jQk&@[email protected]&aOn[: * :\r] '{x)^ .+}> ?UJ). ^:l>>>l:^. * *********************************************************************************************/#include <sys/wait.h>#include <string.h>#include <stdlib.h>#include <signal.h>#include <pthread.h>#include "data.h"#define DATABASE_NAME "user.db"#define TABLE_USER "user"pthread_mutex_t db_mutex;sqlite3 *db; //数据库/* 添加一个用户 *//* 客户端发送注册请求后,服务器端注册用户信息到数据库中 */int db_add_user(char name[], char passwd[]){ char *errmsg; char sqlstr[1024] = {0}; if ((name == NULL) || (passwd == NULL)) { printf("Invalid name or password entered.\r\n"); return -1; } if ((strlen(name) > 32) || (strlen(passwd) > 32)) { printf("The entered name or password is too long.\r\n"); return -1; } sprintf(sqlstr, "insert into %s values('%s', '%s',-1, 1,0)", TABLE_USER, name, passwd); if (sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg) != 0) { printf("Database query error: %s\r\n", sqlite3_errmsg(db)); } else { //printf("User added successfully.\r\n"); } //printf("\r\n"); return 1;}/* 判断某个用户名是否注册 *//* 功能:判断某个用户名是否注册 返回值:有名字返回index,否则返回-1 */int db_user_if_reg(char *name){ int state = -1; char **result, *errmsg; int nrow, ncolumn, i, j, index; char sqlstr[1024] = {0}; sprintf(sqlstr, "select regist from %s where name='%s'", TABLE_USER, name); if (sqlite3_get_table(db, sqlstr, &result, &nrow, &ncolumn, &errmsg) != 0) { printf("Database query error: %s\r\n", errmsg); sqlite3_free(errmsg); } index = ncolumn; if (nrow > 0) { //在数据库中找到指定名字的用户(数据),至少有一条数据 state = 1; } else { //在数据库中没有找到指定名字的用户(数据) state = -1; } sqlite3_free_table(result); return state;}/* 判断用户名密码是否正确 */int db_user_pwd_corrct(char *name, char *passwd){ int state = -1; char **result, *errmsg; int nrow, ncolumn, i, j, index; char sqlstr[1024] = {0}; sprintf(sqlstr, "select * from %s where name='%s' and passwd='%s'", TABLE_USER, name, passwd); /*功能:执行SQL操作 参数: db:数据库句柄 sql:SQL语句 resultp:用来指向sql执行结果的指针 nrow:满足条件的记录的数目 ncolumn:每条记录包含的字段数目 errmsg:错误信息指针的地址 返回值: 成功返回0,失败返回错误码 */ if (sqlite3_get_table(db, sqlstr, &result, &nrow, &ncolumn, &errmsg) != 0) { printf("Database query error: %s\r\n", errmsg); sqlite3_free(errmsg); return -1; } index = ncolumn; if (nrow > 0) { //在数据库中找到指定名字和密码的用户(数据) state = 1; } else { //在数据库中没有找到指定名字和密码的用户(数据) state = -1; } sqlite3_free_table(result); return state;}/* 数据库初始化 */int database_init(){ int n; int ret; char *errmsg; char sql[] = "CREATE TABLE IF NOT EXISTS user(name INT PRIMARY KEY NOT NULL,passwd TEXT NOT NULL,fd INT NOT NULL,regist INT NOT NULL,keygen INT);"; pthread_mutex_init(&db_mutex, NULL); if (sqlite3_open(DATABASE_NAME, &db) < 0) { printf("Failed to open sqlite3: %s\r\n", sqlite3_errmsg(db)); return -1; } /* 使用回调函数执行SQL语句 */ ret = sqlite3_exec(db, sql, NULL, 0, &errmsg); if (ret != SQLITE_OK) { fprintf(stderr, "Database query error: %s\r\n", errmsg); sqlite3_free(errmsg); } else { fprintf(stdout, "The database initialized successfully.\r\n"); } return 0;}/* 关闭数据库 */void database_close(){ sqlite3_close(db);}作者:努力变强&
游戏编程,一个游戏开发收藏夹~
如果图片长时间未显示,请使用Chrome内核浏览器。
边栏推荐
- 再突破!阿里云进入Gartner云AI开发者服务挑战者象限
- 剖析虚幻渲染体系(16)- 图形驱动的秘密
- Huasheng lithium battery IPO meeting: 9-month revenue of 690million; shenjinliang's family relationship is complex
- Facing the "industry, University and research" gap in AI talent training, how can shengteng AI enrich the black land of industrial talents?
- 【WPF】CAD工程图纸转WPF可直接使用的xaml代码技巧
- Yyds dry goods inventory CEPH installation visual dashboard
- Flutter 網絡請求封裝之Dio(Cookie管理、添加攔截器、下載文件、异常處理、取消請求等)
- Lecture 14 of the Blue Bridge Cup -- number theory [example]
- 简单好用的缓存库 gcache
- Audio orchestrator: orchestrate immersive interactive audio using multiple devices
猜你喜欢
No nonsense, code practice will help you master strong caching and negotiation caching!

Use apiccloud AVM multi terminal component to quickly realize the search function in the app

Chapter 3 use of requests Library

2022-2028 global open source cloud storage industry research and trend analysis report

Cvpr2022 tutorial | machine learning remote sensing processing: agriculture and food security, University of Maryland

Why is BeanUtils not recommended?

Jingwei Hengrun is registered through the science and Innovation Board: it plans to raise 5billion yuan, with a 9-month revenue of 2.1 billion yuan

简单好用的缓存库 gcache

How to design a complex business system? From the understanding of domain design, cloud native, micro service, and middle platform
What are the debugging methods for nodejs
随机推荐
OSPF - detailed explanation of GRE tunnel (including configuration command)
【WPF】CAD工程图纸转WPF可直接使用的xaml代码技巧
记|一次exists关键字的学习记录
Huasheng lithium battery IPO meeting: 9-month revenue of 690million; shenjinliang's family relationship is complex
2022-2028 global selective laser sintering service industry research and trend analysis report
Analysis of China's tractor manufacturing and operation situation and forecast report of prospect trend 2022-2028
Lecture 14 of the Blue Bridge Cup -- number theory [exercises]
[intensive lecture] 2022 PHP intermediate and advanced interview questions (II)
Where is win11 screen recording data saved? Win11 screen recording data storage location
再突破!阿里云进入Gartner云AI开发者服务挑战者象限
2022-2028 global industrial touch screen industry research and trend analysis report
聊聊Adapter模式
Talk about adapter mode
Dio encapsulated by the flutter network request (cookie management, adding interceptors, downloading files, exception handling, canceling requests, etc.)
Reasons why MySQL cannot be connected externally after installing MySQL database on ECs and Solutions
2022-2028 global horizontal reciprocating compressor industry research and trend analysis report
Application runtime layotto enters CNCF cloud native panorama
Perceptual video coding based on saliency
Analysis of gpl3.0 license software copyright dispute cases
Conglin environmental protection IPO meeting: annual profit of more than 200million to raise 2.03 billion