当前位置:网站首页>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内核浏览器。
边栏推荐
- TLog 助力盘古框架实现微服务链路日志追踪
- Analysis report on market business model and development direction of China mobile operation industry from 2022 to 2028
- 圖解棧幀運行過程
- 简单好用的缓存库 gcache
- Win11 start menu right click blank? The right button of win11 start menu does not respond. Solution
- 2022-2028 global open source cloud storage industry research and trend analysis report
- HNU network counting experiment: Experiment 4 application layer and transport layer protocol analysis (packettracer)
- 聊聊Adapter模式
- How to open a futures account safely at present? Which futures companies are more reliable?
- Travel notes of 2022giao
猜你喜欢
Sqlmap learning (sqli labs as an example)
Nacos source code analysis 01 code structure
聊聊Adapter模式
Win11 start menu right click blank? The right button of win11 start menu does not respond. Solution
Summary of basic knowledge of neural network
2022-2028 global proton exchange membrane hydrogen electrolyzer industry survey and trend analysis report
Lecture 14 of the Blue Bridge Cup -- number theory [exercises]
2022爱分析· IT运维厂商全景报告
SSH modifies grub in heiqunhui ds918+ system 7.0.1 cfg
Obsidian basic tutorial
随机推荐
Research Report on China's new energy technology and equipment market competition analysis and marketing strategy suggestions 2022-2028
Some reflections on preparing for the Blue Bridge Cup
HLS. JS: past, present and future
The difference between synchronize and volatile
Open source optimized VVC encoder in general scenarios
2022-2028 global co extrusion production line industry research and trend analysis report
记|一次exists关键字的学习记录
NRM source switching tool
2022-2028 global industrial touch screen industry research and trend analysis report
Nacos source code analysis 01 code structure
Flutter 網絡請求封裝之Dio(Cookie管理、添加攔截器、下載文件、异常處理、取消請求等)
简单好用的缓存库 gcache
2022-2028 global SiC igniter industry research and trend analysis report
Research and Analysis on the status quo of China's Cross lamp market and forecast report on its development prospect (2022)
When we talk about the metauniverse, what are we talking about?
China coated abrasive tools industry market depth analysis and development strategy consulting report 2022-2028
Application runtime layotto enters CNCF cloud native panorama
Huasheng lithium battery IPO meeting: 9-month revenue of 690million; shenjinliang's family relationship is complex
TLog 助力盘古框架实现微服务链路日志追踪
Obsidian basic tutorial