当前位置:网站首页>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内核浏览器。
边栏推荐
- 2022giao考游记
- 2022-2028 global cloud based remote browser isolation industry research and trend analysis report
- How can the computer tablet be connected to the computer
- Evaluate the generalization performance of models and build integrated models using out of pocket prediction (oof)
- Analysis report on market demand situation and investment direction of China's optical transmission equipment industry from 2022 to 2028
- What is the difficulty of the form tool that costs billions of dollars? Exclusive interview with si no
- Lecture 14 of the Blue Bridge Cup -- number theory [example]
- St2110 network connected display for virtualized production
- HotSpot JVM 「01」类加载、链接和初始化
- ITU AI and multimedia Seminar: exploring new areas and cross SDO synergy
猜你喜欢

Devops之制品库平台nexus实践

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

Relinearization in homomorphic encryption (ckks)

2022爱分析· IT运维厂商全景报告

记|一次exists关键字的学习记录
![[WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF](/img/50/bb9e73cb4eabcef4bee8f6d5b2fcb6.png)
[WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF

Diagram of stack frame running process

OSPF - detailed explanation of GRE tunnel (including configuration command)

Huasheng lithium battery IPO meeting: 9-month revenue of 690million; shenjinliang's family relationship is complex

2022-2028 global horizontal reciprocating compressor industry research and trend analysis report
随机推荐
Devops之制品库平台nexus实践
Progress of the 137th MPEG Conference
St2110 network connected display for virtualized production
Analysis report on market demand situation and investment direction of China's optical transmission equipment industry from 2022 to 2028
QT learning setting executable exe attribute (solving the problem of Chinese attribute garbled)
Why is BeanUtils not recommended?
Leetcode topic [array] -18- sum of four numbers
2022-2028 global RBI platform industry research and trend analysis report
China soft magnetic material market demand status and prospect scale forecast report 2022-2028
Use apiccloud AVM multi terminal component to quickly realize the search function in the app
Hotspot JVM "01" class loading, linking and initialization
Huawei cloud SMS has tested that many mobile phones prompt frequent sending
EVC, VVC, lcevc test: how about the performance of the latest MPEG codec?
How to use Matplotlib library to realize enlarged display of graphic local data
Facing the "industry, University and research" gap in AI talent training, how can shengteng AI enrich the black land of industrial talents?
2022-2028 global variable frequency compressor technology industry research and trend analysis report
Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology
Factorymethod factory method
聊聊Adapter模式
Audio orchestrator: orchestrate immersive interactive audio using multiple devices