苏嵌实训——day19


var code = “a885af14-a5b4-4dd5-b52d-1fcfb087c32c”

一、数据库

1.1 在ubuntu中安装数据库

sudo apt-get install sqlite3 libsqlite3-dev
安装测试:
输入sqlite3,如果能够成功进入数据库,说明安装成功:
在这里插入图片描述

1.2 数据库的操作

1.2.1 数据库命令的分类

系统命令:是以.开头的命令,主要用于对当前数据库操作
注意:系统命令后面不能加;
普通命令:是以;结尾的命令,主要对数据库中的表进行操作
注意:普通命令前不能加 .

1.2.2 常用的系统命令

.help:查看帮助信息,列出所有的系统命令
.exit: 退出数据库
.quit:退出数据库
.databases:查看当前数据库信息
.tables:列出当前数据库中所有表的表名
.schema:列出数据库中所有表的结构

1.2.3 数据中的常用的语句

-- 进入sqlite3
sqlite3 student.db
-- 退出数据库
.exit

-- 创建一张表
 create table stu(id int,name char,sex char,score int);
 注意:字符或者字符串的类型指定为char或者text
 
 -- 查看表结构
 .schema
 -- 插入数据
 insert into stu (id,name,sex,score) values(1001,"zhangsan",'m',98);
 insert into stu values(1002,"lisi",'f',99);
 insert into stu values(1003,"wanger",'m',100),(1004,"xiaoming",'f',101);
 
 --查询数据
select * from stu;  //查找所有记录
select *from stu where sex='f'; //查找符合条件的记录
select *from stu where sex='f' and id = 1002; 
select *from stu where sex='f' or id = 1001;
select id,name from stu;  //只查询id和name

-- 删除记录
delete from stu where id = 1001;
-- 修改记录
update stu set score=77 where id = 1003;

--添加一列
alter table stu add column vip int;

--删除一列
    sqlites不允许直接删除一列
    1)先创建一张新表
    create table stu1 as select id,name from stu;
    2) 删除原来的旧表
    drop table stu;
    3)对新表进行重命名
    alter table stu1 rename to stu;
-- 数据库主键(既设置的数据将会是唯一的存在的)
    create table usr(name text primary key,passwd text);
-- 删除表
    drop table stu;

1.3 sqlite数据库中常用api

1. sqlite3_open

头文件:#include <sqlite3.h>
原型:int sqlite3_open(const char *filename,sqlite3 **ppDb);
功能:打开或者创建一个数据库
参数:
    filename:数据库的名字
    PPdb:操作数据库的指针,句柄。
返回值:
    成功:返回:SQLITE_OK
    失败:error_code  可通过sqlite3_errmsg获取错误信息

2. int sqlite3_close(sqlite3 * db)

功能:关闭一个数据库

3. sqlite3_exec

头文件:#include <sqlite3.h>
原型:int sqlite3_exec(sqlite3 *db,const char *sql,int (*callback)(void *,int,char**,char**),void *arg,char **errmsg);
功能:执行一条sql语句
参数:
    db:数据库的句柄指针
    sql:将要被执行sql语句
    callback:回调函数,只有在查询语句时,才会给回调函数传参
    arg:为callback传参的
    errmsg:错误信息地址
返回值:
    成功:返回:SQLITE_OK
    失败:error_code  可通过sqlite3_errmsg获取错误信息
    ******************************************************
    int (*callback)(void *arg,int ncolumn,char**f_value,char**f_name)
    功能:得到查询的结果
    参数:
        arg:为回调函数传参使用的
        ncolumn:记录包含的字段的数目(列数)
        f_value:包含每个字段值的指针数组
        f_name:包含每个字段名称的指针数组
    返回值:
        成功 0
        出错:非0

4 sqlite3_get_table

原型:int sqlite3_get_table(sqlite3 *db,const char *sql,char ***pazresult,int *pnRow,int *pnColumn,char **pzErrmsg);
功能:查询数据库,它会创建一个新的内存区域来存放查询的结果信息
参数:
    db:数据库的句柄指针
    sql:将要被执行sql语句
    pazresult:查询的结果
    pnRow:行数
    pnColumn 列数
    pzErrmsg 错误信息
返回值:
    成功 0
    出错:errcode
    pnRow的值为查询到的符合条件的记录数(不包含字段名)。
    pnColumn的值为查询到的符合条件的字段数。

5 void sqlite3_free_table(char ** result)

功能:释放内存
在这里插入图片描述

#include <stdio.h>
#include <sqlite3.h>
#include <string.h>
int print(void *arg,int column,char **value,char **name)  //column为列数,name:字段名 value:数据
{
    int i;
    for(i = 0 ; i < column;i++)
    {
        printf("%s = %s ",name[i],value[i]);
    }
    printf("\n");
    return 0;
}
int main(int argc, char const *argv[])
{
    sqlite3 *ppdb;
    //打开或者创建数据库
    int ret = sqlite3_open("stu.db",&ppdb);
    if(ret != SQLITE_OK)
    {
        printf("sqlite3 open: %s\n",sqlite3_errmsg(ppdb));
        return -1;
    }
    //创建表
    char sql[128] = {0};
    sprintf(sql,"create table if not exists stu(id int,name char,sex char,score int);");
    ret = sqlite3_exec(ppdb,sql,NULL,NULL,NULL);
    if(ret != SQLITE_OK)
    {
        printf("sqlite3_exec: %s\n",sqlite3_errmsg(ppdb));
        return -1;        
    }
    //插入数据
    int i;
    int id,score;
    char name[32] = {0};
    char sex;
    /*for(i = 0; i < 2;i++)
    {
        printf("请输入学号、姓名、性别、分数:\n");
        scanf("%d%s %c %d",&id,name,&sex,&score);
        memset(sql,0,sizeof(sql));
        sprintf(sql,"insert into stu values(%d,'%s','%c',%d);",id,name,sex,score);
        ret = sqlite3_exec(ppdb,sql,NULL,NULL,NULL);
        if(ret != SQLITE_OK)
        {
            printf("sqlite3_exec2: %s\n",sqlite3_errmsg(ppdb));
            return -1; 
        }
    }*/
    memset(sql,0,sizeof(sql));
    sprintf(sql,"select * from stu;");
    /*ret = sqlite3_exec(ppdb,sql,print,NULL,NULL);
    if(ret != SQLITE_OK)
    {
        printf("sqlite3_exec: %s\n",sqlite3_errmsg(ppdb));
        return -1;        
    }*/
    char **result;
    int row,column;
    ret = sqlite3_get_table(ppdb,sql,&result,&row,&column,NULL);  //row:行   column:列
    if(ret != SQLITE_OK)
    {
        printf("sqlite3_get_table : %s\n",sqlite3_errmsg(ppdb));
        return -1;
    }
    int j;
    int Index = column;
    for(i = 0; i < row;i++)   //行
    {
        for(j = 0; j < column;j++)   //列
        {
            printf("%s = %s ",result[j],result[Index]);
            Index++;
        }
        putchar(10);
    }
    return 0;
}

二 项目–网络聊天室

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周末不下雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值