首先来看一下我设置的效果吧:
1、我的设置如下图:
2、我的效果如如下图:
数据库和表的相关命令
先建立一个 Db目录,并在 Db 目录中创建一个 test.db 数据库文件,打开控制台窗口,命令如下:
mkdir Db
cd Db
sqlite3 test.db
2、打开一个已经存在的数据库:sqlite3已经存在的文件名
创建一个新数据库和打开一个已经存在的数据库命令是一模一样的,如果文件在当前目录下不存在,则新建;如果存在,则打开。
3、导入数据:.read数据文件
打开记事本,并将下列 sql语句复制到记事本中,保存为 test.sql到上面说到的 Db 目录下,在命令行环境中输入
.read test.sql
即将所有的数据导入到 test.db数据库中。
BEGINTRANSACTION;
CREATE TABLE Cars(Id integer PRIMARY KEY,Name text,Cost integer);
INSERT INTO Cars VALUES(1,'Audi',52642);
INSERT INTO Cars VALUES(2,'Mercedes',57127);
INSERT INTO Cars VALUES(3,'Skoda',9000);
INSERT INTO Cars VALUES(4,'Volvo',29000);
INSERT INTO Cars VALUES(5,'Bentley',350000);
INSERT INTO Cars VALUES(6,'Citroen',21000);
INSERT INTO Cars VALUES(7,'Hummer',41400);
INSERT INTO Cars VALUES(8,'Volkswagen',21600);
COMMIT;
BEGINTRANSACTION;
CREATE TABLE Orders(Id integer PRIMARY KEY,OrderPrice integerCHECK(OrderPrice>0),
Customer text);
INSERT INTO Orders(OrderPrice,Customer) VALUES(1200,'Williamson');
INSERT INTO Orders(OrderPrice,Customer) VALUES(200,'Robertson');
INSERT INTO Orders(OrderPrice,Customer) VALUES(40,Customer) VALUES(1640,'Smith');
INSERT INTO Orders(OrderPrice,Customer) VALUES(100,Customer) VALUES(50,Customer) VALUES(150,Customer) VALUES(250,Customer) VALUES(840,'Brown');
INSERT INTO Orders(OrderPrice,Customer) VALUES(440,'Black');
INSERT INTO Orders(OrderPrice,Customer) VALUES(20,'Brown');
COMMIT;
BEGINTRANSACTION;
CREATE TABLE Friends(Id integer PRIMARY KEY,Name text UNIQUE NOT NULL,
Sex text CHECK(Sex IN ('M','F')));
INSERT INTO Friends VALUES(1,'Jane','F');
INSERT INTO Friends VALUES(2,'Thomas','M');
INSERT INTO Friends VALUES(3,'Franklin','M');
INSERT INTO Friends VALUES(4,'Elisabeth','F');
INSERT INTO Friends VALUES(5,'Mary','F');
INSERT INTO Friends VALUES(6,'Lucy','F');
INSERT INTO Friends VALUES(7,'Jack','M');
COMMIT;
BEGINTRANSACTION;
CREATE TABLECustomers(CustomerIdinteger PRIMARY KEY,Name text);
INSERT INTO Customers(Name) VALUES('Paul Novak');
INSERT INTO Customers(Name) VALUES('Terry Neils');
INSERT INTO Customers(Name) VALUES('Jack Fonda');
INSERT INTO Customers(Name) VALUES('Tom Willis');
CREATETABLEReservations(Idinteger PRIMARY KEY,
CustomerId integer,Day text);
INSERT INTO Reservations(CustomerId,Day) VALUES(1,'2009-22-11');
INSERT INTO Reservations(CustomerId,Day) VALUES(2,'2009-28-11');
INSERT INTO Reservations(CustomerId,'2009-29-11');
INSERT INTO Reservations(CustomerId,Day) VALUES(3,'2009-02-12');
COMMIT;
BEGINTRANSACTION;
CREATE TABLE Names(Id integer,Name text);
INSERT INTO Names VALUES(1,'Tom');
INSERT INTO Names VALUES(2,'Lucy');
INSERT INTO Names VALUES(3,'Frank');
INSERT INTO Names VALUES(4,'Jane');
INSERT INTO Names VALUES(5,'Robert');
COMMIT;
BEGINTRANSACTION;
CREATE TABLE Books(Id integer PRIMARY KEY,Title text,Author text,
Isbn text default 'not available');
INSERT INTO Books VALUES(1,'War and Peace','Leo Tolstoy','978-0345472403');
INSERT INTO Books VALUES(2,'The Brothers Karamazov',
'Fyodor Dostoyevsky','978-0486437910');
INSERT INTO Books VALUES(3,'Crime and Punishment','978-1840224306');
COMMIT;
4、列出所有的数据表:.tables
完成上面所有的工作以后,我们就可以列出所有的数据表了
6、显示表的结构:.schema 表名
7、导出某个表的数据:.dump 表名
五、数据显示相关命令
1、设置分隔符:.separator分隔符
我们可以首先运行 SELECT * FROMNames;,可以看到默认的分隔符是 |
运行.separator@@@@@@@@@以后,再 SELECT * FROM Names;,可以看到分隔符已经变成@@@@@@@@@@了
2、设置显示模式:.mode模式
有好几种显示模式,默认的是 list显示模式,一般我们使用 column显示模式,还有其他几种显示模式可以 .help看 mode 相关内容。看看下面的图,和上面是不是显示的不一样了?
看看,是不是又不太一样了?
4.列出当前显示格式设置情况:.show