加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C语言调用sqlite3 学生管理系统

发布时间:2020-12-12 19:52:56 所属栏目:百科 来源:网络整理
导读:sqlite3 编译时加 -lsqlite3 #include stdio.h#include stdlib.h#include sqlite3.h#include string.h#define DB_NAME "zb.db"#define TRUE 1#define FALSE 0sqlite3 *pdb = NULL;char *Errmsg = NULL;//创建数据库 zb.dbint open_db(){int rc; rc = sqlite3

sqlite3

编译时加 -lsqlite3


#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <string.h>

#define DB_NAME "zb.db"
#define TRUE        1
#define FALSE       0


sqlite3 *pdb = NULL;
char *Errmsg = NULL;

//创建数据库 zb.db
int open_db()
{
	int rc;
    rc = sqlite3_open(DB_NAME,&pdb);
    if( rc != SQLITE_OK)
    {
        printf("can't open database:%sn",sqlite3_errmsg(pdb));
        sqlite3_close(pdb) ;
        exit(1);
    }
    return 0;
}

//创建表  student
int creat_table()
{
    int rc;
    char *strsql = "create table if not exists student(id integer primary key,name text,age integer);";
	//if not exists如果表student不存在则创建它,当我们第二次运行时执行这个语句(如果表存在,不再创建表)。

    rc = sqlite3_exec(pdb,strsql,&Errmsg);
    if(rc != 0)
    {
        fprintf(stderr,"can't open database: %sn",sqlite3_errmsg(pdb));
        sqlite3_close(pdb);
        exit(1);
    }   
    return 0;
}

/*数据插入*/
int insert_data()       
{
    int flag = TRUE;
    char buff[1024];
    char *strsql;
    int rc = 0 ;
    char temp[3][20];
    strsql = buff;


    if(open_db != 0)//创建数据库 创建表
    {
        open_db();
    }//open_db();
    creat_table();
 
    
    strcpy(strsql,"insert into student");
    strcat(strsql," values(?,?,?);");
 
    sqlite3_stmt  *stmt = NULL;
    rc = sqlite3_prepare_v2(pdb,strlen(strsql),&stmt,NULL);
    if(rc != SQLITE_OK)
    {
        if(stmt)
        {
            sqlite3_finalize(stmt);
        }
        sqlite3_close(pdb);
        return -1;
    }
 
    while(flag) 
    {       
            memset(temp,sizeof(temp));
            printf("学号:");
            scanf("%s",temp[0]);
            printf("姓名:");
            scanf("%s",temp[1]);
            printf("年龄:");
            scanf("%s",temp[2]);
 
            sqlite3_bind_text( stmt,1,temp[0],strlen(temp[0]),NULL);
             
            sqlite3_bind_text( stmt,2,temp[1],strlen(temp[1]),3,temp[2],strlen(temp[2]),NULL);


/*
在SQL声明准备好之后(其中绑定的步骤是可选的),需要调用以下的方法来执行:
int sqlite3_step(sqlite3_stmt*);
如果SQL返回了一个单行结果集,sqlite3_step() 函数将返回 SQLITE_ROW,如果
SQL语句执行成功或者正常将返回SQLITE_DONE,否则将返回错误代码. 如果不能
打开数据库文件则会返回 SQLITE_BUSY . 如果函数的返回值是 SQLITE_ROW.
*/
            if(sqlite3_step(stmt) != SQLITE_DONE)
            {   
                 
                sqlite3_finalize(stmt);
                sqlite3_close(pdb);
                printf(" faile n" );
                return 0;
            }       
        sqlite3_reset(stmt);
        printf("Do you want to do insert ?(0 break,1 insert):n");
        scanf("%d",&flag);
    }
    sqlite3_finalize(stmt);
    printf("insert success!n");
    sqlite3_close(pdb);
   
    return 0;
}


/*数据的查询*/
int search_data()
{
    int rc;
    if(open_db != 0)
    {
        open_db();
    }
    char *strsql = "select * from student" ;
 
    sqlite3_stmt  *stmt = NULL;


    rc = sqlite3_prepare_v2(pdb,NULL);
    if(rc != SQLITE_OK)
    {
        if(stmt)
        {
            sqlite3_finalize(stmt);
        }
        sqlite3_close(pdb);
        return -1;
    }


/*
sqlite3_column_count()函数返回结果集中包含的列数. 
sqlite3_column_count() 可以在执行了 sqlite3_prepare()之后的任何时刻调用. 
sqlite3_data_count()除了必需要在sqlite3_step()之后调用之外,其他跟
sqlite3_column_count() 大同小异. 如果调用sqlite3_step() 返回值是 
SQLITE_DONE 或者一个错误代码,则此时调用sqlite3_data_count() 将返回 0 ,
然而 sqlite3_column_count() 仍然会返回结果集中包含的列数.

sqlite3_column_type()函数返回第N列的值的数据类型. 具体的返回值如下:
       #define SQLITE_INTEGER  1
       #define SQLITE_FLOAT    2
       #define SQLITE_TEXT     3
       #define SQLITE_BLOB     4
       #define SQLITE_NULL     5

sqlite3_setp()
用于执行有前面sqlite3_prepare创建的准备语句。这个语句,执行到结果的第一行可用的位置。
继续前进(执行)到结果的第二行的话,只需再次调用sqlite3_setp()。继续调用sqlite3_setp()
直到这个语句完成。
不返回结果的语句(如:INSERT,UPDATE,或DELETE),sqlite3_step()只执行一次就返回。

*/
    int nColumn = sqlite3_column_count(stmt); //ncolumn 表的列数
    int vtype,i;
    do{ 
        rc = sqlite3_step(stmt);
        if(rc == SQLITE_ROW)
        {
             
            for(i = 0 ; i < nColumn ; i++ ) //打印一行的信息
            {
             
                vtype = sqlite3_column_type(stmt,i); //sqlite3_column_type()函数返回第列的值的数据类型
                if(vtype == SQLITE_INTEGER)
                {
                    printf("%s:%dn",sqlite3_column_name(stmt,i),sqlite3_column_int(stmt,i));
                }
                else if(vtype == SQLITE_TEXT)
                {
                    printf("%s:%sn",sqlite3_column_text(stmt,i));
                }
                else if(vtype == SQLITE_NULL)
                {
                    printf("no valuesn");
                }
            }
            printf("---------------n");
             
       }else if(rc == SQLITE_DONE)
	   {
           //printf("Select finishn");
            printf("查看成功n");
            break;
	   }else
	   {
            printf("Select failen");
            sqlite3_finalize(stmt);
            break;
	   }
         
    }while(1);
    sqlite3_finalize(stmt);
    sqlite3_close(pdb);
    return 0;
}

int main(int argc,char **argv)
{
    int n;
    printf("1.添加 2.修改 3.删除  4.查看n");
    printf("choose[1 - 4]:");
    scanf("%d",&n);
    getchar();
    switch(n)
    {
        case 1:
             
            insert_data();
        //  printf("添加成功n");
            break;
        case 2:
            printf("修改成功n");
            break;
        case 3:
            printf("删除成功n");
            break;
        case 4:
            search_data();
            break;
        default :
            printf("errorn");
    }
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读