使用数据库sqlite3 C语言实现登陆注册的功能

//此代码为注册功能


void create_regtable()
{    
	int rc;       //rc为返回值,判断函数是否执行成功  rc=0函数执行成功,rc !=0函数执行失败
    sqlite3 *db;  //SQLite数据库指针 数据库结构体指针sqlite3为结构体
	char *pFileName = "users.db";  //数据库文件名
    char *zErr;
   
 
	rc = sqlite3_open(pFileName, &db); /*打开数据库*/
	if(rc)                             /*假,关闭数据库*/
	{
		     cleardevice();
			 setmode();
			 outtextxy(170,200,"打开数据库失败!");
			 Sleep(1000);
             main();
		     sqlite3_close(db);
	}
	
	char *pSQL="create table users(id int , pwd int ,primary key(id,pwd))";
 
   	rc = sqlite3_exec(db, pSQL, 0,0, &zErr);   /* sqlite执行接口实现表的创建*/
	
	if(rc!=SQLITE_OK)
	{
		if (zErr!=NULL)
		{
		   
			 cleardevice();
			 setmode();
			 outtextxy(170,200,"SQL错误!");
			 Sleep(1000);
             main();
			 sqlite3_free(zErr);  /* 释放错误信息所占用的空间*/
		}
	}
    	sqlite3_close(db);
 
}




void reg_Insert() 
{	
    users a;
	int rc;
 	char No_id[10]={0,0,0,0,0,0,0,0,0,0};  
	char code[6]={"12345"};
	char incode[6];
    sqlite3 *db;  //SQLite数据库指针
    char *pSQL;
    char * zErr;   /* 定义返回错误信息的变量*/
 
    char *pFileName = "users.db";  //数据库文件名  
    rc = sqlite3_open(pFileName, &db); /*打开数据库*/
 
    if(rc)                             /*假,关闭数据库*/
	{ 
	         cleardevice();
			 setmode();
			 outtextxy(170,200,"打开数据库失败!");
			 voice_name_error();
             main();
             sqlite3_close(db);
	}

       InputBox(incode,6,"验证注册编号,非教师人员不允许注册");

    if(strcmp(incode,code)==0)
	{
	
        InputBox(a.id,10,"请输入账号");
	   
     	outtextxy(295,270,a.id);

     if(strcmp(a.id,No_id)==0)
	 {
			 cleardevice();
			 setmode();
			 outtextxy(170,200,"用户名不能为空,请重新注册!");
			 voice_name_error();
             main();
		 }
	   else {
 
      
   InputBox(a.pwd,10,"请输入密码");

  	  outtextxy(293,339,"********");
   
      Sleep(1000);
 
    pSQL=sqlite3_mprintf("insert into users values('%s','%s')",a.id,a.pwd); 

	  
 	rc = sqlite3_exec(db,pSQL,0,0,&zErr); 
   
     if(rc) //rc=1
		{
		 cleardevice();
		 setmode();
		 outtextxy(220, 200, "帐号注册失败!");
		 voice_register_ok();
		 
		 main();
		 sqlite3_close(db);
		}
      else
		{
		  outtextxy(220, 200, rc);
		 cleardevice();
		 setmode();
		 outtextxy(220, 200, "帐号注册成功!");
		 voice_register_ok();
		 
		 main();
         sqlite3_close(db);
	  }
	}
        
	  } else{ cleardevice();main();}

   }     
  
//此代码为实现账号密码登录验证


void Login()
{   
    users a;
    int rc;    
    sqlite3 *db;  //SQLite数据库指针   sqlite3结构体  *db指向sqlite3结构体的指针
   const    char *pFileName = "users.db";   
   char * zErr;   /* 定义返回错误信息的变量*/
    rc = sqlite3_open(pFileName, &db); /*打开数据库*/
    if(rc)                             /*假,关闭数据库*/
    {
                   cleardevice(); 
                   setmode();
                   settextstyle(30, 0, "楷体"); 
                   settextcolor(RGB(0,500,0)); 
                   outtextxy(220, 200, "打开数据库失败!");
                   voice_No_name();
                   main();
                   sqlite3_close(db);
    }
    InputBox(a.id,10,"请输入账号");
     outtextxy(295,270,a.id); 
     InputBox(a.pwd,10,"请输入密码");  
    outtextxy(293,339,"********");  
    Sleep(1500); 
  char *pSQL=sqlite3_mprintf("insert into users(id,pwd)values('%s','%s')",a.id,a.pwd); 
  //主要是获取rc返回值,进行判断
 // id和pwd共同组成一个表的主键(联合主键) id/pwd谁都不能插入重复的 rc==1 错误 if取反rc==0登陆成功
 //插入的id不同pwd不同 插入成功 rc==0 if取反rc==1登陆失败
 //相当于用户名不能有重复的,用户名跟密码不能一样,增强账号的安全性。
 rc = sqlite3_exec(db, pSQL, NULL, NULL, &zErr);
//!rc==1;  error
        if (!rc)  
        {  //删除多余的数据
            char *pSQL=sqlite3_mprintf("delete from users where id = '%s' and pwd = '%s' ",a.id,a.pwd); 
            rc = sqlite3_exec(db, pSQL, NULL, NULL, &zErr);
           cleardevice(); 
           setmode();
           settextstyle(30, 0, "楷体");
           settextcolor(RGB(255,0,0)); 
           outtextxy(250, 200, "登录失败!");
           Sleep(1000);
           cleardevice();       
           main();      
        }
       else     ///rc=0 ok
      {
            cleardevice(); 
            setmode();
            settextstyle(30, 0, "楷体");
            settextcolor(RGB(0,500,0)); 
            outtextxy(250, 200, "登录成功,欢迎使用!");
            voice_succeed();
            cleardevice();       
            option_Menu();      
      }
         sqlite3_close(db);
}
原文地址:https://www.cnblogs.com/zhaocundang/p/4924308.html