Linux系统编程-DAY12(数据库)

补充:tcp_fork, tcp_pthread
        1.tcp_fork,父进程只管三次握手,fork()之后要关闭文件描述符,子进程就是给客户端服务的
        2.回收时用sign(SIGCHID, handle)函数

一、数据库
1.sqlite3 mysql mssql
   库--->表--->列--->记录
2.关系型数据库  非关系型数据库(行列组成)
ddl data defination language 建表    
dml  新增 修改 删除一行 data modifty  修改表     
dql  data query language  查询
3.分类:大型:ORACLE(甲骨文)
    中型: MYSQL/MSSQL
    小型:SQLITE(只提供数据的增删改查) DBLL pwdfb  
    关系型数据库
4.名词:
    DB:数据库  select updata database
    DBMS:数据库管理系统
    MIS:管理信息系统
    OA:办公自动化
5.嵌入式数据库:
    sqlite3 
特点:1.开源 C语言开发
    2.代码量少 1万行左右,总大小在10M以内
    3.绿色软件无需安装
    4.文件型数据库,可以移动
    5.数据容量最大2T
whereis sqlite3

二、查状态指令:
    .database  列出当前库和系统中那个文件在关联
    .exit  退出    .quit  退出    
    .headers on | off  打开表头或关闭(默认情况下是关闭的)
    .help   show  this message
    .schema(原理图)  显示建表语句
    .tables  跟ls同理

三、SQL语句:
    1.sql语句必须以 ‘;’结尾

    2.创建一个表:ddl
    create table 表名(表字段1,表字段2, ......);
    eg: create table user(id, name, age);
    ps:以上表的表字段,支持如下数据类型:int text real(小数数据) blob(二进制数据)。 默认是text类型
    create table 表名(表字段1 类型,表字段2 类型, ......);
    eg: create table user(id int , name char , age int);
Ctrl + D :强制退出编译

    3.drop table 表名;

    4.插入:insert into 表名(字段名称)values(值名称);
    eg:insert into user (id, age) values(1, 10);
    
    5.查询表中的数据:
   select 列名 from 表名  条件;
   eg:select * from user ;
       select id from user;
    select id,name from user where not  age <30
       select * from user  where name   like '三_'   % _ 通配符
        eg:SELECT * from cyy where name like '小_';

    and or && ||
    eg: select *from user  where age>20 or age<50 

    6.排序:
    order by 顺序(desc 逆序)
    eg:select * from user order by age desc;(年龄逆序排列)

    7.修改表中的数据:
    update 表名 set 表字段= 值 满足条件;
    eg:update user set id = 3 where name = '没坐' and age = 10;

    8.删除表中数据:
   delete from 表名  满足条件:
   eg:delete from user where id  = 1; ///删除id=1 的数据;

    9.自动增长列
sqlite> CREATE TABLE user3(id INTEGER PRIMARY KEY ASC,name char,age int,dt datetime);  主键 
    eg:insert into user1 values (2,'张三',23,datetime('now','+8 hours'));

四、维护命令
1.数据的导出:
    sqlite3 xxx.db .dump >xxx.sql(> 叫做输出重定向)

2.数据的导入:
    sqlite3 xxx.db <xxx.sql  (<叫做输入重定向)

五、sqlite3 数据库编程接口

1、需要的头文件    sqlite3.h
2、编译过程       -lsqlite3
3、编程框架:
        打开数据库 ---> 读写数据库(增,删,改,查)  ---> 关闭数据库

3.1 打开数据库: sqlite3_open
         int sqlite3_open(char * path,sqlite3 ** db);
         功能:打开指定path路径+文件名称的数据库,并将
         打开的地址指向db变量的句柄。
         参数:path 要打开的数据库路径+名称
         db  要打开的数据库地址指针
3.2 关闭数据库: sqlite3_close
        int sqlite3_close(sqlite3 *db);
        功能:关闭指定的数据库
参数:要关闭的数据库地址
3.3 数据库操作:
        查询操作:sqlite3_get_table();  select 

int sqlite3_get_table(sqlite3 *db,char *sql,
char *** rest,int *nrow,int *ncol,
char ** errmsg);

六、练习与代码
1.tcp_fork() --- 服务端

#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/types.h> /* See NOTES */
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

typedef struct sockaddr*(SA);

int	main(int argc, char **argv)
{
    int listfd = socket(AF_INET, SOCK_STREAM, 0);
    if(-1 == listfd)
    {
        perror("socket");
        return 1;
    }

    struct sockaddr_in ser, cli;
    bzero(&ser, sizeof(ser));
    bzero(&cli, sizeof(cli));
    ser.sin_family = AF_INET;
    ser.sin_port = htons(50000);
    ser.sin_addr.s_addr = inet_addr("192.168.0.119");
     int ret = bind(listfd, (SA)&ser, sizeof(ser));
    if (-1 == ret)
    {
        perror("bind");
        return 1;
    }

    listen(listfd, 3);
    socklen_t len = sizeof(cli);

    while(1)
    {
        int conn = accept(listfd, (SA)&cli, &len);
        if(-1 == conn)
        {
            perror("accept");
            continue;
        }

        pid_t pid = fork();
        if(pid > 0)
        {
            close(conn);
        }
        else if (0 == pid)
        {
            close(listfd);
            while(1)
            {
                char buf[256] = {0};
                ret = recv(conn, buf, sizeof(buf), 0);
                if(ret<=0)
                {
                    printf("cli offline\n");
                    close(conn);
                    exit(1);
                }
                printf("cli:%s\n",buf);
                time_t tm;
                time(&tm);
                struct tm * info = localtime(&tm);
                sprintf(buf,"%s %d:%d:%d\n",buf, info->tm_hour,info->tm_min,info->tm_sec);
                send(conn,buf,strlen(buf),0);
            }
        }
        else
        {
            perror("fork");
            continue;
        }
    }
    close(listfd);
    return 0;
}

2.tcp_pthread()--- 服务器端

#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/types.h> /* See NOTES */
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <pthread.h>

typedef struct sockaddr*(SA);

void* th(void*arg)
{
    pthread_detach(pthread_self());//**
    int conn = *(int*)arg;
    //sem_post();
    while(1)    
    {
        char buf[256] = {0};
        int ret = recv(conn, buf, sizeof(buf), 0);
        if(ret<=0)
        {
            printf("cli offline\n");
            close(conn);
            break;
        }
        printf("cli:%s\n",buf);
        time_t tm;
        time(&tm);
        struct tm * info = localtime(&tm);
        sprintf(buf,"%s %d:%d:%d\n",buf, info->tm_hour,info->tm_min,info->tm_sec);
        send(conn,buf,strlen(buf),0);
    }
    return NULL;
}

int	main(int argc, char **argv)
{
    int listfd = socket(AF_INET, SOCK_STREAM, 0);
    if(-1 == listfd)
    {
        perror("socket");
        return 1;
    }

    struct sockaddr_in ser, cli;
    bzero(&ser, sizeof(ser));
    bzero(&cli, sizeof(cli));
    ser.sin_family = AF_INET;
    ser.sin_port = htons(50000);
    ser.sin_addr.s_addr = inet_addr("192.168.0.119");
     int ret = bind(listfd, (SA)&ser, sizeof(ser));
    if (-1 == ret)
    {
        perror("bind");
        return 1;
    }

    listen(listfd, 3);
    socklen_t len = sizeof(cli);

    while(1)
    {
        int conn = accept(listfd, (SA)&cli, &len);
        if(-1 == conn)
        {
            perror("accept");
            continue;
        }
        pthread_t tid;
        pthread_create(&tid, NULL, th, &conn);
    }
    close(listfd);
    return 0;
}

3.数据库编程---修改数据库

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

int	main(int argc, char **argv)
{
    sqlite3 *db = NULL;
    int ret = sqlite3_open("./1.db", &db);
    if(SQLITE_OK != ret)
    {
        fprintf(stderr, "open %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        return 1;
    }

    char *errmsg;
    char sql_cmd[] = "insert into cyy values(7, '牛逼', 85);";
    ret = sqlite3_exec(db, sql_cmd, NULL, NULL, &errmsg);
    if(SQLITE_OK != ret)
    {
        fprintf(stderr, "exec sql:[%s] errmsg :%s\n", sql_cmd, errmsg);
        sqlite3_free(errmsg);
        sqlite3_close(db);
        return 1;
    }

    sqlite3_close(db);

    return 0;
}

4.数据库编程 --- select数据库

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

int show(void *args, int col, char **result, char **title)
{
    int i = 0;
    static int flag = 0;
    if(0 == flag)
    {
        flag = 1;
        for(i = 0; i < col; ++i)
        {
            printf("%s\t", title[i]);
        }
        printf("\n");
    }
    for(i = 0; i < col; ++i)
    {
        printf("%s\t", result[i]);
    }
    printf("\n");
    return 0;
}

int	main(int argc, char **argv)
{
    sqlite3 *db = NULL;
    int ret = sqlite3_open("./1.db", &db);
    if(SQLITE_OK != ret)
    {
        fprintf(stderr, "open %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        return 1;
    }

    char *errmsg;
    char sql_cmd[] = "select * from xiaofan;";
    ret = sqlite3_exec(db, sql_cmd, show, NULL, &errmsg);
    if(SQLITE_OK != ret)
    {
        fprintf(stderr, "exec sql:[%s] errmsg :%s\n", sql_cmd, errmsg);
        sqlite3_free(errmsg);
        sqlite3_close(db);
        return 1;
    }

    sqlite3_close(db);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值