cs
复制代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/sem.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define ERR_MAG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
} while(0)
#include <sqlite3.h>
#define DB "register.db"
int insert_into(sqlite3 *sqldb);
int find_by_uname(sqlite3 *sqldb, char *uname);
int find_all(sqlite3 *sqldb);
int main(int argc, const char *argv[])
{
// 打开数据库
sqlite3 *sqldb = NULL;
if (sqlite3_open(DB, &sqldb) != SQLITE_OK)
{
fprintf(stderr, "数据库打开错误 : %d : %s\t__%d__\n", sqlite3_errcode(sqldb), sqlite3_errmsg(sqldb), __LINE__);
return -1;
}
// 创建表
char *create_table = "CREATE TABLE if not exists register(uname char,passwd char)";
char *err_msg = NULL;
if (sqlite3_exec(sqldb, create_table, NULL, NULL, &err_msg) != SQLITE_OK)
{
fprintf(stderr, "create table : %d : %s : __%d__\n", sqlite3_errcode(sqldb), sqlite3_errmsg(sqldb), __LINE__);
return -1;
}
char choose;
// 操作选项
while (1)
{
printf("---------------------------------------\n");
printf("------------1. 用户注册----------------\n");
// printf("------------2. 根据用户名注销用户----------------\n");
// printf("------------3. 根据用户名修改密码----------------\n");
// printf("------------4. 根据用户名查询信息----------------\n");
printf("------------5. 查询全部信息-------------\n");
printf("------------6. 退出--------------------\n");
printf("---------------------------------------\n");
printf("请选择操作 >>> ");
choose = getchar();
printf("choose = %c\n", choose);
while (getchar() != 10);
switch (choose)
{
case '1':
// 增
printf("注册\n");
insert_into(sqldb);
break;
case '2':
// 根据用户名删除
break;
case '3':
// 根据用户名更改
break;
case '4':
// 根据用户名查询
break;
case '5':
// 查询全部用户
find_all(sqldb);
break;
case '6':
// 退出
goto END;
}
}
END:
if (sqlite3_close(sqldb) != SQLITE_OK)
{
fprintf(stderr, "close database : %d : %s : __%d__\n", sqlite3_errcode(sqldb), sqlite3_errmsg(sqldb), __LINE__);
return -1;
}
printf("数据库关闭成功\n");
return 0;
}
return 0;
}
// 插入
int insert_into(sqlite3 *sqldb)
{
char uname[128] = "";
char passwd[128] = "";
printf("请输入准备注册的用户名 >>> ");
fgets(uname, sizeof(uname), stdin);
uname[strlen(uname) - 1] = 0;
// while (getchar() != 10)
// ;
printf("请输入密码 >>> ");
fgets(passwd, sizeof(passwd), stdin);
passwd[strlen(passwd) - 1] = 0;
// while (getchar() != 10)
// ;
while (find_by_uname(sqldb, uname))
{
printf("请重新输入准备注册的用户名,输入exit取消注册 >>> ");
fgets(uname, sizeof(uname), stdin);
uname[strlen(uname) - 1] = 0;
// while (getchar() != 10)
// ;
if (!strcmp(uname, "exit"))
{
printf("退出注册\n");
return 0;
}
}
char sql_str[256] = "";
sprintf(sql_str, "insert into register (uname,passwd) values('%s','%s');", uname, passwd);
char *errmsg = NULL;
printf("sql_str = %s\n", sql_str);
if (sqlite3_exec(sqldb, sql_str, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "insert into register : %d : %s __%d__\n", sqlite3_errcode(sqldb), errmsg, __LINE__);
return -1;
}
printf("用户 [%s] 注册成功\n", uname);
return 1;
}
// 根据用户名删除
int del_by_uname()
{
}
// 根据用户名更改密码
int updata_by_uname()
{
}
// 根据用户名查询
int find_by_uname(sqlite3 *sqldb, char *uname)
{
char sql_str[256] = "";
char *errmsg = NULL;
char **pres = NULL;
int row, colum;
sprintf(sql_str, "select * from register where uname='%s';", uname);
if (sqlite3_get_table(sqldb, sql_str, &pres, &row, &colum, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "find_by_uname error : %d : %s : __%d__\n", sqlite3_errcode(sqldb), errmsg, __LINE__);
return 0;
}
return row;
}
// 查询所有
int find_all(sqlite3 *sqldb)
{
char sql_str[256] = "";
char *errmsg = NULL;
char **pres = NULL;
int row, colum;
sprintf(sql_str, "select * from register;");
if (sqlite3_get_table(sqldb, sql_str, &pres, &row, &colum, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "find_by_uname error : %d : %s : __%d__\n", sqlite3_errcode(sqldb), errmsg, __LINE__);
return 0;
}
for (int i = 0; i <= row; i++)
{
for (int j = 0; j < colum; j++)
{
printf("%s\t", pres[(row * i) + j]);
}
putchar(10);
}
return row;
}