网络 / day06 作业

1. sqlite3 操作

1> 创建一个工人信息库,包含工号(主键)、姓名、年龄、薪资。

sqlite> create table worker
   ...> (
   ...> id integer primary key autoincrement,
   ...> age int(2),
   ...> name varchar(20),
   ...> salary double
   ...> );
sqlite> .table
worker
sqlite> .schema
CREATE TABLE worker
(
id integer primary key autoincrement,
age int(2),
name varchar(20),
salary double
);
CREATE TABLE sqlite_sequence(name,seq);

2> 添加三条工人信息(可以完整信息,也可以非完整信息)

sqlite> insert into worker (age, name, salary) values(20, "zhangsan", 20000);
sqlite> insert into worker (age, name, salary) values(21, "lisi", 21000);
sqlite> insert into worker (age, name, salary) values(22, "wangwu", 22000);
sqlite> select * from worker
   ...> ;
1|20|zhangsan|20000.0
2|21|lisi|21000.0
3|22|wangwu|22000.0
sqlite> .header on
sqlite> .mode column
sqlite> select * from worker
   ...> ;
id          age         name        salary    
----------  ----------  ----------  ----------
1           20          zhangsan    20000.0   
2           21          lisi        21000.0   
3           22          wangwu      22000.0 

3> 修改某一个工人的薪资(确定的一个)

sqlite> select * from worker
   ...> ;
id          age         name        salary    
----------  ----------  ----------  ----------
1           20          zhangsan    20000.0   
2           21          lisi        21000.0   
3           22          wangwu      22000.0   
sqlite> update worker set salary=23000 where id=3;
sqlite> select * from worker
   ...> ;
id          age         name        salary    
----------  ----------  ----------  ----------
1           20          zhangsan    20000.0   
2           21          lisi        21000.0   
3           22          wangwu      23000.0 

4> 展示出工资在10000到20000之间的所有工人信息

sqlite> select * from worker;
id          age         name        salary    
----------  ----------  ----------  ----------
1           20          zhangsan    17000.0   
2           21          lisi        21000.0   
3           22          wangwu      13000.0   
sqlite> select * from worker where salary between 10000 and 20000
   ...> ;
id          age         name        salary    
----------  ----------  ----------  ----------
1           20          zhangsan    17000.0   
3           22          wangwu      13000.0  

5> 删除掉指定姓名工人的信息

sqlite> select * from worker;
id          age         name        salary    
----------  ----------  ----------  ----------
1           20          zhangsan    17000.0   
2           21          lisi        21000.0   
3           22          wangwu      13000.0   
sqlite> delete from worker where name="lisi";
sqlite> select * from worker;
id          age         name        salary    
----------  ----------  ----------  ----------
1           20          zhangsan    17000.0   
3           22          wangwu      13000.0 

6> 工厂倒闭,删除整个工人信息库

sqlite> drop table worker
   ...> ;
sqlite> .table

2. 静态库制作

2.1. 原始文件

主程序 main.c

#include "test.h"

int main(int argc, const char *argv[])
{
	int res = sum(1, 2);
	printf("res=%d\n", res );
	
	return 0;
}

目标库文件 test.c

int sum(int a, int b)
{
	return a + b;
}

头文件 test.h

#ifndef __TEST_H__
#define __TEST_H__

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

int sum(int a, int b);

#endif 

目录结构

.
├── head
│   └── test.h
├── lib
└── src
    ├── main.c
    └── test.c

3 directories, 3 files

2.2 通过test.c制作成静态库

2.2.1. 将源文件只编译不链接生产二进制文件

gcc -c test.c -o ../lib/test.o


.
├── head
│   └── test.h
├── lib
│   └── test.o
└── src
    ├── main.c
    └── test.c



2.2.2. 将上述生成的二进制文件制作成静态库

ar -crs lib库名.a test.o

ar:用于生产静态库的指令

-c:表时创建静态库

-r:将文件插入或替代之前库中的名称

-s:重置静态库

ar -crs ../lib/libtest.a ../lib/test.o


.
├── head
│   └── test.h
├── lib
│   ├── libtest.a
│   └── test.o
└── src
    ├── main.c
    └── test.c

2.2.3. 使用静态库

gcc 主程序.c -L 库的路径 -l库名 -I 头文件路径

ubuntu@ubuntu:~/embedded/05_network/libmake/static/src$ gcc main.c -L ../lib/ -ltest -I ../head/ -o test
ubuntu@ubuntu:~/embedded/05_network/libmake/static/src$ ls -la
total 28
drwxrwxr-x 2 ubuntu ubuntu 4096 12月 21 19:17 .
drwxrwxr-x 5 ubuntu ubuntu 4096 12月 21 15:49 ..
-rw-rw-r-- 1 ubuntu ubuntu   87 12月 21 15:27 main.c
-rwxrwxr-x 1 ubuntu ubuntu 8224 12月 21 19:17 test
-rw-rw-r-- 1 ubuntu ubuntu   42 12月 21 15:28 test.c

3. 动态库制作

3.1. 原始文件同2

.
├── head
│   └── test.h
├── lib
└── src
    ├── main.c
    └── test.c

3.2. 生成动态库

gcc -fPIC -c test.c -o test.o //-fPIC忽略文件位置将 test.c生产test.o

gcc -shared test.o -o libtest.so //将二进制文件编译生产库文件

也可以统一成如下指令

gcc -fPIC -shared test.c -o libtest.so

ubuntu@ubuntu:~/embedded/05_network/libmake/static/src$ gcc -fPIC -shared test.c -o ../lib/libtest.so



.
├── head
│   └── test.h
├── lib
│   └── libtest.so
└── src
    ├── main.c
    └── test.c

3.3 使用动态库

gcc 主程序.c -L 库的路径 -l库名 -I 头文件路径

ubuntu@ubuntu:~/embedded/05_network/libmake/static/src$ gcc main.c -L ../lib/ -ltest -I ../head/ -o test

ubuntu@ubuntu:~/embedded/05_network/libmake/static/src$ ls -la
total 28
drwxrwxr-x 2 ubuntu ubuntu 4096 12月 21 19:30 .
drwxrwxr-x 5 ubuntu ubuntu 4096 12月 21 15:49 ..
-rw-rw-r-- 1 ubuntu ubuntu  124 12月 21 19:19 main.c
-rwxrwxr-x 1 ubuntu ubuntu 8328 12月 21 19:30 test
-rw-rw-r-- 1 ubuntu ubuntu   42 12月 21 15:28 test.c




  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值