当前位置:网站首页>C language and the creation and use of database
C language and the creation and use of database
2022-06-25 22:52:00 【Game programming】
c Creation and use of language and database
data.h The header file
/********************************************************************************************** * :|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:^. * * *********************************************************************************************//* Main program data processing header file *//* Online user structure */struct ONLINE_USER{ int fd; //-1 int flage; // registed or not char name[32]; char passwd[32];};/* Add a user *//* After the client sends the registration request , The server side registers user information into the database */int db_add_user(char name[], char passwd[]);/* Determine whether a user name is registered *//* function : Determine whether a user name is registered Return value : Return with name index, Otherwise return to -1 */int db_user_if_reg(char *name);/* Determine whether the user name and password are correct */int db_user_pwd_corrct(char *name, char *passwd);/* Database initialization */int database_init();/* Close the database */void database_close();data.c Implementation file
/* Main program data processing source file */#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; // database /* Add a user *//* After the client sends the registration request , The server side registers user information into the database */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;}/* Determine whether a user name is registered *//* function : Determine whether a user name is registered Return value : Return with name index, Otherwise return to -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) { // Find the user with the specified name in the database ( data ), At least one piece of data state = 1; } else { // No user with the specified name was found in the database ( data ) state = -1; } sqlite3_free_table(result); return state;}/* Determine whether the user name and password are correct */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); /* function : perform SQL operation Parameters : db: Database handle sql:SQL sentence resultp: Used for pointing sql Pointer to the execution result nrow: The number of records that satisfy the condition ncolumn: Number of fields per record errmsg: The address of the error message pointer Return value : Successfully returns 0, Failure return error code */ 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) { // Find the user with the specified name and password in the database ( data ) state = 1; } else { // The user with the specified name and password was not found in the database ( data ) state = -1; } sqlite3_free_table(result); return state;}/* Database initialization */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; } /* Use the callback function to perform SQL sentence */ 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;}/* Close the database */void database_close(){ sqlite3_close(db);}author : Try to be strong &
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
- Global and Chinese oleic acid operation mode and market supply and demand forecast report 2022 ~ 2028
- A3.ansible production practice case -- system initialization roles
- 2022 love analysis · panoramic report of it operation and maintenance manufacturers
- APP测试要点
- This 110 year old "longevity" enterprise has been planning for the next century
- 2022-2028 global RBI platform industry research and trend analysis report
- Initialization process of gstlibav
- Unity技术手册 - 生命周期旋转RotationOverLifetime-速度旋转RotationBySpeed-及外力
- 2022-2028 global carbon fiber unidirectional tape industry research and trend analysis report
- [WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF
猜你喜欢

2022-2028 global horizontal reciprocating compressor industry research and trend analysis report
Data annotation in the second half: growth flywheel under PLG mode Manfu Technology

面对AI人才培养的“产学研”鸿沟,昇腾AI如何做厚产业人才黑土地?

聊聊Adapter模式

Zero Trust: break the passive development mode of "attack and defense" and build a "moat" for enterprise safety
![Lecture 14 of the Blue Bridge Cup -- number theory [exercises]](/img/96/0971909c8bf25820c2d4f520bb83fb.jpg)
Lecture 14 of the Blue Bridge Cup -- number theory [exercises]
![[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

Openwrt (VIII) application layer development

27 Chinese scholars including Yaoban and chendanqi from Tsinghua won the awards, and the list of winners of Sloan award in 2022 was issued

2022-2028 global transmission type photoelectric circuit breaker industry research and trend analysis report
随机推荐
NRM source switching tool
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
OSPF - detailed explanation of GRE tunnel (including configuration command)
2022-2028 global variable frequency compressor technology industry research and trend analysis report
Does jQuery cache any selectors- Does jQuery do any kind of caching of “selectors”?
Illustration de l'exécution du cadre de pile
2022-2028 global RBI platform industry research and trend analysis report
How do I project points on a 3D plane- How to project a point onto a plane in 3D?
2022-2028 global web and browser isolation platform industry research and trend analysis report
Exclusive interview with deepmindceo hassabis: we will see a new scientific Renaissance! AI's new achievements in nuclear fusion are officially announced today
Some reflections on preparing for the Blue Bridge Cup
How to guarantee idempotency of message queue
Research and Analysis on the status quo of China's Cross lamp market and forecast report on its development prospect (2022)
2022-2028 global co extrusion production line industry research and trend analysis report
2022-2028 global TFT LCD touch screen industry research and trend analysis report
面对AI人才培养的“产学研”鸿沟,昇腾AI如何做厚产业人才黑土地?
Research and Analysis on the current situation of China's magnetic detector Market and forecast report on its development prospect (2022)
NARI radar's IPO meeting: it plans to raise nearly 1billion yuan. Bao Xiaojun and his wife are Canadians
China soft magnetic material market demand status and prospect scale forecast report 2022-2028
How to open a futures account safely at present? Which futures companies are more reliable?