假期作业10.5

//结合数据库的方式完成用户注册,要求客户端输入账户密码,发送给服务器服务器接收到账户密码后,写入到数据库中不允许重复注册
#include 

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("my.db", &sqldb) != SQLITE_OK)
    {
        fprintf(stderr, "数据库打开失败 : %d : %s\n", sqlite3_errcode(sqldb), sqlite3_errmsg(sqldb));
        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\n", sqlite3_errcode(sqldb), sqlite3_errmsg(sqldb));
		return -1;
	}

	int opt;
	while (1)
	{
		printf("-------------------------------\n");
		printf("-------1. 用户注册-------------\n");
		printf("-------2. 显示用户信息---------\n");
		printf("-------3. 退出-----------------\n");
		printf("-------------------------------\n");
		printf("输入指令 >>> ");
		scanf("%d",&opt);
		while (getchar()!= 10);
		switch(opt)
		{
		case 1:
			//注册
			printf("注册\n");
			insert_into(sqldb);
			break;
		case 2:
			//显示所有用户信息
			find_all(sqldb);
			break;
		case 3:
			//退出
			goto END;
		}
	}

END:
	if (sqlite3_close(sqldb) != SQLITE_OK)
	{
		fprintf(stderr, "close database : %d : %s\n", sqlite3_errcode(sqldb), sqlite3_errmsg(sqldb));
		return -1;
	}

	printf("数据库已关闭\n");
	return 0;
}
//注册
int insert_into(sqlite3 *sqldb)
{
    char uname[128] = "";
    char passwd[128] = "";
LOOP:
    printf("输入用户名 >>> ");
    fgets(uname, sizeof(uname), stdin);
    uname[strlen(uname) - 1] = 0;

    printf("输入密码 >>> ");
    fgets(passwd, sizeof(passwd), stdin);
    passwd[strlen(passwd) - 1] = 0;

    while (find_by_uname(sqldb, uname))
    {
        printf("用户名已被注册,按任意键重新注册>>>");
		getchar();
		return 0;
    }

    char sql_str[256] = "";
	printf("用户[%s]注册成功\n",uname);
    char *errmsg = NULL;
    if (sqlite3_exec(sqldb, sql_str, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        fprintf(stderr, "insert into register : %d : %s\n", sqlite3_errcode(sqldb), errmsg);
        return -1;
    }
    return 1;
}

//用户名查找
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\n", sqlite3_errcode(sqldb), errmsg);
        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\n", sqlite3_errcode(sqldb), errmsg);
        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;
}

你可能感兴趣的:(jvm,数据库,oracle)