当前位置:网站首页>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内核浏览器。
边栏推荐
- Canoe: the fifth simulation project: simulation + test
- China bed and mattress market status research analysis and development prospect forecast report (2022)
- MySQL Chapter 15 lock
- Research and Analysis on the current situation of Chinese acne drug market and forecast report on its development prospect (2022)
- Application runtime layotto enters CNCF cloud native panorama
- Factorymethod factory method
- China soft magnetic material market demand status and prospect scale forecast report 2022-2028
- 2022-2028 global co extrusion production line industry research and trend analysis report
- Hello, teacher, is it really safe to open an account in Alipay fund?
- HNU network counting experiment: Experiment 5 network layer and link layer protocol analysis (packettracer)
猜你喜欢

Three layer architecture + routing experiment

2022-2028 global variable frequency compressor technology industry research and trend analysis report

Huasheng lithium battery IPO meeting: 9-month revenue of 690million; shenjinliang's family relationship is complex
No nonsense, code practice will help you master strong caching and negotiation caching!

Illustration de l'exécution du cadre de pile

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

2022-2028 global transmission type photoelectric circuit breaker industry research and trend analysis report

Obsidian basic tutorial

【WPF】CAD工程图纸转WPF可直接使用的xaml代码技巧

OSPF - detailed explanation of GRE tunnel (including configuration command)
随机推荐
HNU network counting experiment: Experiment 5 network layer and link layer protocol analysis (packettracer)
OSPF - detailed explanation of GRE tunnel (including configuration command)
用idea建立第一個網站
Reasons why MySQL cannot be connected externally after installing MySQL database on ECs and Solutions
Yyds dry goods inventory JD 2, why is redis so fast?
be careful! This written examination method is gradually being replaced
聊聊Adapter模式
Factorymethod factory method
图解栈帧运行过程
2022giao考游记
Leetcode topic [array] -18- sum of four numbers
2022-2028 global extrusion coating and lamination production line industry research and trend analysis report
Openwrt (VIII) application layer development
Créer le premier site Web avec idea
Relinearization in homomorphic encryption (ckks)
Market depth analysis and development strategy consulting report of China's fire equipment market 2022-2028
2022-2028 global industrial touch screen industry research and trend analysis report
Data annotation in the second half: growth flywheel under PLG mode Manfu Technology
Zero Trust: break the passive development mode of "attack and defense" and build a "moat" for enterprise safety
Privatization lightweight continuous integration deployment scheme -- 03 deployment of Web services (Part 2)