目前我正在寫一些代碼來將請求記入sqlite數據庫。爲了讓數據庫不會如此臃腫起來,我使用了包含不變數據(apps
,machineID
,ips
和platforms
)的不同表格,這些表格可能會出現很多次,但通常是唯一的,主表(access
)只是通過它們的ID保留其他表中的行的引用。現在我想創建一個視圖,顯示來自其他表的主數據而不是其他表的索引。SQLITE:通過索引創建視圖到多個表
例子:
apps Table
----------
id application buildNum
1 app1 24.112
2 app2 24.113
machineID Table
--------------
id machineID
1 12345
2 1235
ips Table
---------
id ip
1 192.168.9.53
platforms Table
---------------
id platform os
1 windows win7
2 windows win8
access Table
------------
date ip_id machineID_id platform_id application_id responseCode
1391677790.7363 1 1 1 1 404
1391677797.5792 1 1 1 1 404
1391677800.7379 1 2 2 2 404
1391677802.493 1 2 2 2 404
1391677889.7193 1 1 1 1 404
1391677890.6034 1 2 2 2 404
現在我想創建一個類似如下的觀點:
date ip machineID platform os application buildNum responseCode
1391677790.7363 192.168.9.53 12345 windows win7 app1 24.112 404
1391677797.5792 192.168.9.53 12345 windows win7 app1 24.112 404
1391677800.7379 192.168.9.53 1235 windows win8 app2 24.113 404
1391677802.493 192.168.9.53 1235 windows win8 app2 24.113 404
1391677889.7193 192.168.9.53 12345 windows win7 app1 24.112 404
1391677890.6034 192.168.9.53 1235 windows win8 app2 24.113 404
如何使用SQLite做任何線索。對不起,如果這可能看起來像一個新手問題,但我不熟悉SQL。
這裏將是建立例子表代碼:
BEGIN TRANSACTION;
CREATE TABLE ips (id INTEGER PRIMARY KEY,ip TEXT NOT NULL UNIQUE);
INSERT INTO "ips" VALUES(1,'192.168.9.53');
CREATE TABLE platforms (id INTEGER PRIMARY KEY,platform TEXT NOT NULL,os TEXT NOT NULL, UNIQUE(platform,os));
INSERT INTO "platforms" VALUES(1,'windows','win7');
INSERT INTO "platforms" VALUES(2,'windows','win8');
CREATE TABLE apps (id INTEGER PRIMARY KEY,application TEXT NOT NULL,buildNum TEXT not null, UNIQUE(application,buildNum));
INSERT INTO "apps" VALUES(1,'app1','24.112');
INSERT INTO "apps" VALUES(2,'app2','24.113');
CREATE TABLE machineIDs (id INTEGER PRIMARY KEY,machineID TEXT NOT NULL UNIQUE);
INSERT INTO "machineIDs" VALUES(1,'12345');
INSERT INTO "machineIDs" VALUES(2,'1235');
CREATE TABLE access (date REAL PRIMERY KEY DEFAULT ((julianday('now') - 2440587.5)*86400.0),ip_id INTEGER NOT NULL,machineID_id INTEGER,platform_id INTEGER,application_id INTEGER,responseCode INTEGER);
INSERT INTO "access" VALUES(1391677790.7363,1,1,1,1,404);
INSERT INTO "access" VALUES(1391677797.5792,1,1,1,1,404);
INSERT INTO "access" VALUES(1391677800.7379,1,2,2,2,404);
INSERT INTO "access" VALUES(1391677802.493,1,2,2,2,404);
INSERT INTO "access" VALUES(1391677889.7193,1,1,1,1,404);
INSERT INTO "access" VALUES(1391677890.6034,1,2,2,2,404);
COMMIT;
感謝您的幫助!
你應該學習JOIN命令 –