2016-02-03 79 views
0
當我看着我的大桌子的sqlite的文件,使用SQLite的瀏覽器,如Firefox插件 SQLite ManagerSQLite Browser

,我看到根據自己除了表中的序列列顯示(我一直在添加新列在一個循環中)。sqlite howto排序列?

A J D B 
--- --- --- --- 
.. .. .. .. 

這是一個視覺干擾,因爲該表是非常大的有很多很多列,並用於調試的目的,我需要目視檢查相鄰列。如何對錶格進行排序以便將列排序,如:

A B J D 
--- --- --- --- 
.. .. .. .. 

謝謝。

回答

1

SQLite沒有內置的機制來重新排列表的列。

你不得不重新創建表:

CREATE TABLE new_MyTable(A, B, J, D [...]); 
INSERT INTO new_MyTable SELECT A, B, J, D FROM MyTable; 
DROP TABLE MyTable; 
ALTER TABLE new_MyTable RENAME TO MyTable; 
+0

這兩個答案都是偉大的!但我終於使用了這個。我需要做很多字符串操作(使用python),以便能夠自動檢索長列表名稱,然後對它們進行排序。使用[本文](http://stackoverflow.com/a/685212/5177935)和[本文](http://stackoverflow.com/a/7991230/5177935)的有價值的提示。 – xaratustra

1

您可以創建一個的DebugView與重新排序,請你列...

CREATE TABLE ChorleyHandford (IMAGE_REF, DATE, LOCATION, NUMBER); 
INSERT INTO "ChorleyHandford" VALUES('A10006','16/10/1990','RATCLIFFE POWER STATION CASTLE DONNINGTON POWER STATION','27'); 
INSERT INTO "ChorleyHandford" VALUES('A10017','25/10/1990','BROMLEY PARKS','17'); 
INSERT INTO "ChorleyHandford" VALUES('A10018','25/10/1990','CROSSWAYS BUSINESS PARK DARTFORD CROSSING BRIDGE CONSTRUCTION','20'); 
INSERT INTO "ChorleyHandford" VALUES('A10022','25/10/1990','THURROCK LAKESIDE','11'); 
INSERT INTO "ChorleyHandford" VALUES('A10057','01/11/1990','CLANDON REGIS SITE','6'); 

看你擁有的一切:

select * from CHORLEYHANDFORD; 

A10006|16/10/1990|RATCLIFFE POWER STATION CASTLE DONNINGTON POWER STATION|27 
A10017|25/10/1990|BROMLEY PARKS|17 
A10018|25/10/1990|CROSSWAYS BUSINESS PARK DARTFORD CROSSING BRIDGE CONSTRUCTION|20 
A10022|25/10/1990|THURROCK LAKESIDE|11 
A10057|01/11/1990|CLANDON REGIS SITE|6 

創建一個重新排序的視圖,看看它看起來沒有女:

create view dbgview as select NUMBER, LOCATION, DATE, IMAGE_REF FROM CHORLEYHANDFORD; 
select * from dbgview; 

27|RATCLIFFE POWER STATION CASTLE DONNINGTON POWER STATION|16/10/1990|A10006 
17|BROMLEY PARKS|25/10/1990|A10017 
20|CROSSWAYS BUSINESS PARK DARTFORD CROSSING BRIDGE CONSTRUCTION|25/10/1990|A10018 
11|THURROCK LAKESIDE|25/10/1990|A10022 
6|CLANDON REGIS SITE|01/11/1990|A10057