使用sqlite3(数据库知识)的接口函数完成一个用户注册功能模块设计 要封装成函数,在独立的main中调用测试

/*====================================================
 *   Copyright (C) 2018  All rights reserved.
 *
 *   文件名称:sqltie3_get_tables.c
 *   创 建 者:cyz [email protected]
 *   创建日期:2018年01月15日
 *
 
*1、使用sqlite3的接口函数完成一个用户注册功能模块设计
   要封装成函数,在独立的main中调用测试;
   描    述:
 ================================================================*/


#include <unistd.h>
#include <stdio.h>
#include <sqlite3.h>


int main(int argc, char *argv[])
{
sqlite3 * db = NULL;
int ret = sqlite3_open("./msg.db", &db);
if(ret < 0)
{
perror("sqlite3 open error \n");
return -1;
}
printf("sqlite 3 open ok \n");


char buf[128]={0};
char ** rest  = NULL;
int  row =0, col  = 0;
char name[128]={0};
char pass[128]={0};
while(1){
printf("请注册新用户\n");
printf("please input name:");
scanf("%s",name);
printf("please input passwd:");
scanf("%s",pass);
sprintf(buf,"select * from user where name = '%s';",name);
ret = sqlite3_get_table(db,buf,&rest,&row,&col,NULL);
if(ret < 0)
{
perror("sqlite3 get table error");
return -1;
}
if(row!=0)
{
printf("用户已存在\n");
continue;
}


sprintf(buf,"insert into user(name,pass) values (\"%s\",\"%s\");",name,pass);
ret = sqlite3_get_table(db,buf,&rest,&row,&col,NULL);
if(ret < 0)
{
perror("sqlite3 get table error");
return -1;
}
// printf("%s\n",buf);
printf("register is ok!\n");
break;
}
sqlite3_close(db);
return 0;
}

猜你喜欢

转载自blog.csdn.net/flying_man_/article/details/79068365