我们知道,在SQL Server数据库中查询数据时需要用到SELECT……FROM语句结合WHERE条件来实现的,其实SQLite数据库查询数据的方式也是这样的,本文通过一个实例来介绍这一过程,希望能给大家带来收获。
首先将下面的SQL 语句保存到 data.sql 文件中(为了以后的练习使用):
- BEGIN TRANSACTION;
- 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;
- BEGIN TRANSACTION;
- CREATE TABLE Orders(Id integer PRIMARY KEY, OrderPrice integer CHECK(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, "Robertson");
- INSERT INTO Orders(OrderPrice, Customer) VALUES(1640, "Smith");
- INSERT INTO Orders(OrderPrice, Customer) VALUES(100, "Robertson");
- INSERT INTO Orders(OrderPrice, Customer) VALUES(50, "Williamson");
- INSERT INTO Orders(OrderPrice, Customer) VALUES(150, "Smith");
- INSERT INTO Orders(OrderPrice, Customer) VALUES(250, "Smith");
- INSERT INTO Orders(OrderPrice, Customer) VALUES(840, "Brown");
- INSERT INTO Orders(OrderPrice, Customer) VALUES(440, "Black");
- INSERT INTO Orders(OrderPrice, Customer) VALUES(20, "Brown");
- COMMIT;
然后在在终端执行命令 .read data.sql,将数据导入到数据库中:
- sqlite>
- sqlite> .tables
- Friends
- sqlite> .read data.sql
- sqlite> .tables
- Cars Orders Teachers
- sqlite>
可以看到,Cars 表和 Orders 表已经导入到数据库中,现在可以查询了:
- sqlite>
- sqlite> SELECT * FROM Cars;
- Id Name Cost
- ---- --------------- ---------------
- 1 Audi 52642
- 2 Mercedes 57127
- 3 Skoda 9000
- 4 Volvo 29000
- 5 Bentley 350000
- 6 Citroen 21000
- 7 Hummer 41400
- 8 Volkswagen 21600
- sqlite> SELECT * FROM Orders;
- Id OrderPrice Customer
- ---- --------------- ---------------
- 1 1200 Williamson
- 2 200 Robertson
- 3 40 Robertson
- 4 1640 Smith
- 5 100 Robertson
- 6 50 Williamson
- 7 150 Smith
- 8 250 Smith
- 9 840 Brown
- 10 440 Black
- 11 20 Brown
- sqlite>
关于SQLite数据库查找数据的知识就介绍到这里,我们还会在以后的文章里继续介绍,谢谢各位一直以来的支持!
【编辑推荐】