我已經從Maxmind下載GeoLiteCountry
CSV文件 - http://www.maxmind.com/app/geolitecountry。使用給我的格式作爲標準(以便這可以成爲一個自動化的任務),我試圖將所有的數據導入到一個表中。將Maxmind CSV導入到SQL Server中
我創建了一個新的表提供IPCountries2
具有精確匹配的列列:
FromIP varchar(50),
ToIP varchar(50),
BeginNum bigint,
EndNum bigint,
CountryCode varchar(50),
CountryName varchar(250)
使用的代碼,我能找到的各種塊,我無法得到它的工作使用字段終止符和行終止:
BULK
INSERT CSVTest
FROM 'c:\csvtest.txt'
WITH
(
FIELDTERMINATOR = '","',
ROWTERMINATOR = '\n'
)
GO
這樣做的結果是單行插入,正確的除了最後一個曾與下一行溢出(大概是整個數據庫,如果我沒有限制)。另外,第一個單元在開始時有一個報價。
我環顧四周,發現一種稱爲格式文件(從未使用過這些文件)。製作一個看起來像:
10.0
6
1 SQLCHAR 0 50 "," 1 FromIP ""
2 SQLCHAR 0 50 "," 2 ToIP ""
3 SQLBIGINT 0 19 "," 3 BeginNum ""
4 SQLBIGINT 0 19 "," 4 EndNum ""
5 SQLCHAR 0 50 "," 5 CountryCode ""
6 SQLCHAR 0 250 "\n" 6 CountryName ""
但對BIGINT線這樣的錯誤:
消息4867,級別16,狀態1,行1
爲行批量加載數據轉換錯誤(溢出) 1,第3列(BeginNum)。
它做了10次,然後停止因爲最大錯誤計數。
我能夠得到第一個方法,如果我把它放入Excel並重新保存,這將刪除引號。但是,我不想依賴這種方法,因爲我希望這個方法每週自動更新一次,而不必手動打開並重新保存。
我不介意我最終使用的兩種方法中的哪一種,只要它使用乾淨的文件。我看了他們的文檔,但他們只有PHP或MS Access的代碼。
編輯
從CSV文件中的一些行:
"1.0.0.0","1.0.0.255","16777216","16777471","AU","Australia"
"1.0.1.0","1.0.3.255","16777472","16778239","CN","China"
"1.0.4.0","1.0.7.255","16778240","16779263","AU","Australia"
"1.0.8.0","1.0.15.255","16779264","16781311","CN","China"
"1.0.16.0","1.0.31.255","16781312","16785407","JP","Japan"
"1.0.32.0","1.0.63.255","16785408","16793599","CN","China"
"1.0.64.0","1.0.127.255","16793600","16809983","JP","Japan"
"1.0.128.0","1.0.255.255","16809984","16842751","TH","Thailand"
"1.1.0.0","1.1.0.255","16842752","16843007","CN","China"
"1.1.1.0","1.1.1.255","16843008","16843263","AU","Australia"
"1.1.2.0","1.1.63.255","16843264","16859135","CN","China"
"1.1.64.0","1.1.127.255","16859136","16875519","JP","Japan"
"1.1.128.0","1.1.255.255","16875520","16908287","TH","Thailand"
更新
經過一段持續存在的,我能夠把事情的工作95%,與原來的方法(不格式文件)。然而,微微一變,看起來像這樣:
BULK INSERT IPCountries2
FROM 'c:\Temp\GeoIPCountryWhois.csv'
WITH
(
FIELDTERMINATOR = '","',
ROWTERMINATOR = '"'
)
GO
一切順利在正確的領域,因爲他們應該,我唯一的問題是,在第一列有在一開始的報價。一些示例數據:
FromIP ToIP BeginNum EndNum CountryCode Country
"2.21.248.0 2.21.253.255 34994176 34995711 FR France
"2.21.254.0 2.21.254.255 34995712 34995967 EU Europe
"2.21.255.0 2.21.255.255 34995968 34996223 NL Netherlands
您可以發佈CSV文件的前幾行數據嗎? – datagod 2012-03-20 16:42:56
啊是我多麼愚蠢,在我的OP中找到 – Chris 2012-03-20 16:45:35
在你的原始批量插入中,你指定FIELDTERMINATOR爲'「,」'。爲什麼逗號周圍有雙引號? – datagod 2012-03-20 16:48:25