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
主程序 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
#include
#include
int sum(int a, int b);
#endif
目录结构
.
├── head
│ └── test.h
├── lib
└── src
├── main.c
└── test.c
3 directories, 3 files
gcc -c test.c -o ../lib/test.o
.
├── head
│ └── test.h
├── lib
│ └── test.o
└── src
├── main.c
└── test.c
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
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
.
├── head
│ └── test.h
├── lib
└── src
├── main.c
└── test.c
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
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