1. 程式人生 > >sqlite3建立資料庫表

sqlite3建立資料庫表

1、建立一個新的資料庫:sqlite3     檔名 先建立一個 Db 目錄,並在 Db 目錄中建立一個 test.db 資料庫檔案,開啟控制檯視窗,命令如下: mkdir Db cd Db sqlite3 test.db 2、開啟一個已經存在的資料庫:sqlite3 已經存在的檔名 建立一個新資料庫和開啟一個已經存在的資料庫命令是一模一樣的,如果檔案在當前目錄下不存在,則新建;如果存在,則開啟。 3、匯入資料:.read     資料檔案 開啟記事本,並將下列 SQL 語句複製到記事本中,儲存為 test.sql 到上面說到的 Db 目錄下,在命令列環境中輸入 .read   test.sql 即將所有的資料匯入到 test.db 資料庫中。 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; BEGIN TRANSACTION;
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; BEGIN TRANSACTION;
CREATE TABLE  Customers(CustomerId integer 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'); CREATE TABLE  Reservations(Id integer 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, Day) VALUES(2, '2009-29-11');
INSERT INTO Reservations(CustomerId, Day) VALUES(1, '2009-29-11');
INSERT INTO Reservations(CustomerId, Day) VALUES(3, '2009-02-12');
COMMIT; BEGIN TRANSACTION;
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; BEGIN TRANSACTION;
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',
                         'Fyodor Dostoyevsky','978-1840224306');
COMMIT;
4、列出所有的資料表: .tables
完成上面所有的工作以後,我們就可以列出所有的資料表了 SQLite3 <wbr>命令列引數

5、顯示資料庫結構:.schema 其實就是一些 SQL 語句,他們描述了資料庫的結構,如圖 SQLite3 <wbr>命令列引數 6、顯示錶的結構:.schema    表名 SQLite3 <wbr>命令列引數

7、匯出某個表的資料: .dump    表名

SQLite3 <wbr>命令列引數 五、資料顯示相關命令 1、設定分隔符:.separator    分隔符 我們可以首先執行 SELECT * FROM Names; ,可以看到預設的分隔符是 |  執行.separator @@@@@@@@@ 以後,再 SELECT * FROM Names;,可以看到分隔符已經變成 @@@@@@@@@@ 了 SQLite3 <wbr>命令列引數
2、設定顯示模式:.mode
    模式 有好幾種顯示模式,預設的是 list 顯示模式,一般我們使用 column 顯示模式,還有其他幾種顯示模式可以 .help 看 mode 相關內容。看看下面的圖,和上面是不是顯示的不一樣了? SQLite3 <wbr>命令列引數

3、顯示標題欄:.headers   on 看看,是不是又不太一樣了? SQLite3 <wbr>命令列引數

4.列出當前顯示格式設定情況:.show SQLite3 <wbr>命令列引數