2013-04-13 49 views
0

這是我從CSV導入的當前耙子任務,我可以重寫它來導入並刪除兩個以前的列/將數據連接成一個latlon列,使用空格作爲分隔符?合併點數據列經緯度合併爲一個latlon

require 'csv' 

desc "Import public library data from csv file" 

task :import => [:environment] do 
    namespace :import_incidents_csv do 
    task :create_incidents => :environment do 
     ActiveRecord::Base.connection.execute "COPY PublicLibraryBranchLocations   (DistrictNumber, DistrictName, LibrarySystem, Type, Branch, Phone, Email, Website, PhysicalAddress, City, Province, PostalCode, Latitude, Longitude) FROM '/home/mgmacri/data/PublicLibraryBranchLocations.csv' DELIMITER ',' CSV;" 
    end 
    end 
end 

回答

2

COPY本身無法做到這一點,但您可以在導入之前更改CSV。

一種方法是複製到一個臨時表中,然後使用這樣導入:

INSERT INTO PublicLibraryBranchLocations(DistrictNumber, DistrictName, LibrarySystem, Type, Branch, Phone, Email, Website, PhysicalAddress, City, Province, PostalCode, LatLon) 
SELECT DistrictNumber, DistrictName, LibrarySystem, Type, Branch, Phone, Email, Website, PhysicalAddress, City, Province, PostalCode, Latitude ||' '|| Longitude 
FROM TEMP_PublicLibraryBranchLocations; 

正如你所看到的,有在年底Latitude ||' '|| Longitude串聯。

另一種方法是通過創建使用file_fdw一個外部表,所以你可以閱讀CSV文件直接爲表(也可以叫做PublicLibraryBranchLocations_FDW_CSV)不導入和使用像從該表中導入數據到PublicLibraryBranchLocations

INSERT INTO PublicLibraryBranchLocations(DistrictNumber, DistrictName, LibrarySystem, Type, Branch, Phone, Email, Website, PhysicalAddress, City, Province, PostalCode, LatLon) 
SELECT DistrictNumber, DistrictName, LibrarySystem, Type, Branch, Phone, Email, Website, PhysicalAddress, City, Province, PostalCode, Latitude ||' '|| Longitude 
FROM PublicLibraryBranchLocations_FDW_CSV; 
+0

到目前爲止,這一切都很有意義,我今天要測試,可能會有一些問題。謝謝! –

+0

完全感謝一羣Guedes! –

+0

不錯!不用謝! :) – Guedes