linux 下的数据库安装
在虚拟机的终端输入:sqlite3
tony@ubuntu:~/workspace/sqlite$ sqlite3
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .quit //退出sqlite环境
tony@ubuntu:~/workspace/sqlite$
如果出现上面的打印说明系统中已经安装了sqlite,
如果没有安装使用安装命令:sudo apt-get install sqlite3
再次输入: sqlite3 查看能否识别到版本信息,识别到则是安装成功
linux 下的数据库使用
1. 创建数据库
1. tony@ubuntu:~/workspace/sqlite$ sqlite3 test.db //打开test.db,没有则创建
2. sqlite> .open test1.db //打开test.db,没有则创建
tony@ubuntu:~/workspace/sqlite$ sqlite3
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .open test2.db
sqlite> .databases
seq name file
--- --------------- ----------------------------------------------------------
0 main /home/tony/workspace/sqlite/test2.db
sqlite> .quit
tony@ubuntu:~/workspace/sqlite$ sqlite3
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .databases
seq name file
--- --------------- ----------------------------------------------------------
0 main
sqlite> .quit
tony@ubuntu:~/workspace/sqlite$ sqlite3 test.db
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
几个常用的命令:
.databases 命令来检查它是否在数据库列表中
.tables 显示数据库中所有的表.
.schema 显示所有的表的创建语句.
.schema table_name 显示表table_name 的创建语句.
.quit 退出
2. 创建并打开数据库成功后就可以在数据库中创建我们需要的东西,可以把sqlite 看成一个execl表样式使用
sqlite> create table students(id int primary key,name text,age int ,score real);
sqlite> .schema students
CREATE TABLE students(id int primary key,name text,age int ,score real);
sqlite>
创建了一个表名为students的表,里面包含id , name, age, score四个元素
3. 当前表中还没有数据,我们需要往表中添加数据,添加方式使用insert
sqlite> insert into students(id,name,age,score) values(1,'张三',25,80.00);
sqlite> insert into students(id,name,age,score) values(2,'李四',25,90.00);
sqlite> select * from students;
1|张三|25|80.0
2|李四|25|90.0
sqlite>
也可以使用
sqlite> select * from students;
1|张三|25|80.0
2|李四|25|90.0
sqlite> insert into students values(3,'赵士',26,85.00);
sqlite> insert into students values(4,'李留',25,97.00);
sqlite>
sqlite>
sqlite>
sqlite> select * from students;
1|张三|25|80.0
2|李四|25|90.0
3|赵士|26|85.0
4|李留|25|97.0
sqlite>
这种查看方式不好看我们可以修改显示方式
header : 显示标识, mode column :左对齐 ,参数有很多, .show查看具体用法 ,mode --help 查看参数说明
sqlite> .header on
sqlite> .mode column
sqlite> select * from students;
id name age score
---------- ---------- ---------- ----------
3 赵士 26 85.0
2 赵士 26 85.0
6 赵士 26 85.0
8 赵士 26 85.0
10 赵士 26 85.0
sqlite>
当使用insert添加数据时如果没有添加主键值,这种数据如果和其他数据没有明显的差异化,是无法单一的将此值删除的而不影响其他,在设置主键值时最好添加参数唯一和不为空设置方式
sqlite> insert into students (name, age, score) values ('李留',25,97.00);
sqlite> select * from students
...> ;
1|张三|25|80.0
3|赵士|26|85.0
4|李留|25|97.0
|李留|25|97.0
sqlite>
sqlite>
sqlite>
sqlite> CREATE TABLE students(id int primary key NOT NULL ,name text,age int ,score real);
sqlite>
4. 删除表中的某些数据或者某一类的数据
例如要删除名字为张三的数据
sqlite> select * from students;
1|张三|25|80.0
3|赵士|26|85.0
4|李留|25|97.0
2|赵士|26|85.0
5|李留|25|97.0
6|赵士|26|85.0
7|李留|25|97.0
8|赵士|26|85.0
9|李留|25|97.0
10|赵士|26|85.0
11|李留|25|97.0
sqlite>
sqlite>
sqlite> delete from students where name="张三";
sqlite> select * from students;
3|赵士|26|85.0
4|李留|25|97.0
2|赵士|26|85.0
5|李留|25|97.0
6|赵士|26|85.0
7|李留|25|97.0
8|赵士|26|85.0
9|李留|25|97.0
10|赵士|26|85.0
11|李留|25|97.0
sqlite>
例如要删除年龄不为26的数据
sqlite> select * from students;
3|赵士|26|85.0
4|李留|25|97.0
2|赵士|26|85.0
5|李留|25|97.0
6|赵士|26|85.0
7|李留|25|97.0
8|赵士|26|85.0
9|李留|25|97.0
10|赵士|26|85.0
11|李留|25|97.0
sqlite> delete from students where age<>26;
sqlite> select * from students;
3|赵士|26|85.0
2|赵士|26|85.0
6|赵士|26|85.0
8|赵士|26|85.0
10|赵士|26|85.0
sqlite>
5. 数据进行排序(asc升序排列, desc 降序排列,添加where判断条件)
sqlite> select * from students;
id name age score
---------- ------------ ------------ ----------
3 赵士 26 85.0
2 赵士 26 85.0
6 赵士 26 85.0
8 赵士 26 85.0
10 赵士 26 85.0
5 李留 20 97.0
7 李留 29 97.0
9 李留 22 97.0
11 李留 25 97.0
sqlite> select * from students order by age asc;
id name age score
---------- ------------ ------------ ----------
5 李留 20 97.0
9 李留 22 97.0
11 李留 25 97.0
3 赵士 26 85.0
2 赵士 26 85.0
6 赵士 26 85.0
8 赵士 26 85.0
10 赵士 26 85.0
7 李留 29 97.0
sqlite> select * from students where score > 90 order by age asc;
id name age score
---------- ------------ ------------ ----------
5 李留 20 97.0
9 李留 22 97.0
11 李留 25 97.0
7 李留 29 97.0
sqlite> select * from students where score > 90 order by age desc;
id name age score
---------- ------------ ------------ ----------
7 李留 29 97.0
11 李留 25 97.0
9 李留 22 97.0
5 李留 20 97.0
sqlite>
6. 修改表中的数据(update), 修改的时候要特别的注意要加条件限制(where) 到具体的某一个或者某一类,不加会将set整列数据全部修改,毕竟那不是我们需要的
sqlite> select * from students order by age asc;
id name age score
---------- ------------ ------------ ----------
5 李留 20 97.0
9 李留 22 97.0
11 李留 25 97.0
3 赵士 26 85.0
2 赵士 26 85.0
6 赵士 26 85.0
8 赵士 26 85.0
10 赵士 26 85.0
7 李留 29 97.0
sqlite> update students set age = 40 where id = 5;
sqlite> select * from students order by age asc;
id name age score
---------- ------------ ------------ ----------
9 李留 22 97.0
11 李留 25 97.0
3 赵士 26 85.0
2 赵士 26 85.0
6 赵士 26 85.0
8 赵士 26 85.0
10 赵士 26 85.0
7 李留 29 97.0
5 李留 40 97.0
sqlite> update students set age = 40
...> ;
sqlite> select * from students order by age asc;
id name age score
---------- ------------ ------------ ----------
3 赵士 40 85.0
2 赵士 40 85.0
6 赵士 40 85.0
8 赵士 40 85.0
10 赵士 40 85.0
5 李留 40 97.0
7 李留 40 97.0
9 李留 40 97.0
11 李留 40 97.0
sqlite>
7. 查找需要的数据 ,主要是select和where 函数配合使用
sqlite> select * from students where id < 5;
id name age score
---------- ------------ ------------ ----------
2 赵士 40 85.0
3 赵士 40 85.0
sqlite> update students set age = 30 where id =2;
sqlite> select * from students where id < 5 and age < 40;
id name age score
---------- ------------ ------------ ----------
2 赵士 30 85.0
sqlite>
这些就是命令行下的sqlite的增删改查基本操作,在编程中就是将这些运用到代码中