• 【SqlServer】导入MaxMind中的IP数据(.csv)文件到SQL Server数据库(转)


    原文链接:http://creativeclr.com/blog/importing-maxmind-ip-database-into-sql-server

    I've recently found a very accurate free ip database provided by MaxMind.

    I have tried some methods to import this csv data into my Sql Server database, but what I have found that either the example was broken or wasn't complete (files were missing from the script download).

    So I started to digg into the issue and found a simple and fast way of importing the 2 csv's into my sql server database.

      1. First, you need to edit the 2 csv files and remove all quotes ("). Since the files are fairly large, use a smart editor (I used Notepad2).
      2. Run the following code. Don't forget to replace the paths to the files with your own.
    -- Geo IP V4
    IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'GeoIP')BEGIN
    DROP TABLE GeoIP
    END
    
    CREATE TABLE GeoIP (
    	startIpNum bigint,
    	endIpNum bigint,
    	locId  bigint
    )
    
    -- first remove ALL quotes from csv to import
    BULK INSERT GeoIP FROM 'yourpath	oGeoLiteCity-Blocks.csv' WITH (FIRSTROW = 2, FIELDTERMINATOR=',', ROWTERMINATOR = '0x0a')
    
    CREATE CLUSTERED INDEX Geo_IP_Look
    ON GeoIP
    ([StartIpNum], [endIpNum],  [locId])
    
    -- Geo IP V6
    
    IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'GeoIPSix')BEGIN
    DROP TABLE GeoIPSix
    END
    
    CREATE TABLE GeoIPSix(
    	startIpNum bigint,
    	endIpNum bigint,
    	locId  bigint
    )
    
    -- first remove ALL quotes from csv to import
    BULK INSERT GeoIP FROM 'yourpath	oGeoLiteCity-Blocks-IPv6.csv' WITH (FIRSTROW = 2, FIELDTERMINATOR=',', ROWTERMINATOR = '0x0a')
    
    CREATE CLUSTERED INDEX Geo_IP_Look
    ON GeoIP
    ([StartIpNum], [endIpNum],  [locId])
    
    
    -- Geo Loc
    
    IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'GeoLoc')BEGIN
    DROP TABLE GeoLoc
    END
    
    
    CREATE TABLE GeoLoc (
    	 locId bigint,
    	 country nvarchar(2),
    	 region nvarchar(3),
    	 city nvarchar(255),
    	 postalCode nvarchar(10),
    	 latitude nvarchar(15),
    	 longitude nvarchar(15),
    	 metroCode nvarchar(15),
    	 areaCode nvarchar(15)
    )
    
    BULK INSERT GeoLoc FROM 'yourpath	oGeoLiteCity-Location.csv' WITH (FIRSTROW = 2, FIELDTERMINATOR=',', ROWTERMINATOR = '0x0a')  
    
    CREATE CLUSTERED INDEX Geo_Info_Look
    ON GeoLoc
    ([locId], [country], [region], [city], [latitude], [longitude])
    

      

    I tested the code on my SQL Server 2008 R2 db, and it all worked OK.

    Update: you might want to remove the headers from the maxmind csv's.

  • 相关阅读:
    1086. Tree Traversals Again (25)
    1094. The Largest Generation (25)
    1076. Forwards on Weibo (30)
    1083. List Grades (25)
    1082. Read Number in Chinese (25)
    【七夕特辑】程序员表白网页合集
    flex布局
    Nodejs进阶:基于express+multer的文件上传
    Git 和 SVN 之间的五个基本区别
    React通用后台管理系统
  • 原文地址:https://www.cnblogs.com/HDK2016/p/14254829.html
Copyright © 2020-2023  润新知