1
无
This commit is contained in:
251
modules/ImportDataBase.go
Normal file
251
modules/ImportDataBase.go
Normal file
@@ -0,0 +1,251 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func ImportDatabaseToString() {
|
||||
mysql.RegisterLocalFile("C:/Users/wangh/Desktop/code/checkIP/db/GeoLite2-City-Blocks-IPv4-string.csv")
|
||||
db, err := sql.Open("mysql", "root:990821@tcp(192.168.2.20:3306)/checkip")
|
||||
if err != nil {
|
||||
fmt.Println("数据库连接错误:", err)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
//创建表
|
||||
CreateTable := `
|
||||
CREATE TABLE geolite2_city_ipv4_string (
|
||||
network_start_ip VARCHAR(16) NOT NULL,
|
||||
network_last_ip VARCHAR(16) NOT NULL,
|
||||
geoname_id INT,
|
||||
registered_country_geoname_id INT,
|
||||
represented_country_geoname_id INT,
|
||||
is_anonymous_proxy BOOL,
|
||||
is_satellite_provider BOOL,
|
||||
postal_code TEXT,
|
||||
latitude FLOAT,
|
||||
longitude FLOAT,
|
||||
accuracy_radius INT,
|
||||
INDEX (network_start_ip),
|
||||
INDEX (network_last_ip)
|
||||
);
|
||||
`
|
||||
//导入数据
|
||||
sqlStatement := `
|
||||
LOAD DATA LOCAL INFILE 'C:/Users/wangh/Desktop/code/checkIP/db/GeoLite2-City-Blocks-IPv4-string.csv'
|
||||
INTO TABLE geolite2_city_ipv4_string
|
||||
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 ROWS
|
||||
(@network_start_ip, @network_last_ip, @geoname_id, @registered_country_geoname_id, @represented_country_geoname_id,
|
||||
@is_anonymous_proxy, @is_satellite_provider, @postal_code, @latitude, @longitude, @accuracy_radius)
|
||||
SET network_start_ip = (@network_start_ip),
|
||||
network_last_ip = (@network_last_ip),
|
||||
geoname_id = NULLIF(@geoname_id, ''),
|
||||
registered_country_geoname_id = NULLIF(@registered_country_geoname_id, ''),
|
||||
represented_country_geoname_id = NULLIF(@represented_country_geoname_id, ''),
|
||||
is_anonymous_proxy = NULLIF(@is_anonymous_proxy, ''),
|
||||
is_satellite_provider = NULLIF(@is_satellite_provider, ''),
|
||||
postal_code = NULLIF(@postal_code, ''),
|
||||
latitude = NULLIF(@latitude, ''),
|
||||
longitude = NULLIF(@longitude, ''),
|
||||
accuracy_radius = NULLIF(@accuracy_radius, '')
|
||||
`
|
||||
// 执行SQL语句
|
||||
_, err = db.Exec(CreateTable)
|
||||
if err != nil {
|
||||
fmt.Println("执行SQL语句错误:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("创建表geolite2_city_ipv4_string完成")
|
||||
_, err = db.Exec(sqlStatement)
|
||||
if err != nil {
|
||||
fmt.Println("执行SQL语句错误:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("表geolite2_city_ipv4_string数据加载完成")
|
||||
}
|
||||
|
||||
func ImportDatabaseToHex() {
|
||||
mysql.RegisterLocalFile("C:/Users/wangh/Desktop/code/checkIP/db/GeoLite2-City-Blocks-IPv4-hex.csv")
|
||||
db, err := sql.Open("mysql", "root:990821@tcp(192.168.2.20:3306)/checkip")
|
||||
if err != nil {
|
||||
fmt.Println("数据库连接错误:", err)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
//创建表geolite2_city_ipv4_hex
|
||||
CreateTable := `
|
||||
CREATE TABLE geolite2_city_ipv4_hex (
|
||||
network_start_hex varbinary(16) not null,
|
||||
network_last_hex varbinary(16) not null,
|
||||
geoname_id INT,
|
||||
registered_country_geoname_id INT,
|
||||
represented_country_geoname_id INT,
|
||||
is_anonymous_proxy BOOL,
|
||||
is_satellite_provider BOOL,
|
||||
postal_code TEXT,
|
||||
latitude FLOAT,
|
||||
longitude FLOAT,
|
||||
accuracy_radius INT,
|
||||
INDEX (network_start_hex),
|
||||
INDEX (network_last_hex)
|
||||
);
|
||||
`
|
||||
//导入十六进制数据
|
||||
sqlStatement := `
|
||||
LOAD DATA LOCAL INFILE 'C:/Users/wangh/Desktop/code/checkIP/db/GeoLite2-City-Blocks-IPv4-hex.csv'
|
||||
INTO TABLE geolite2_city_ipv4_hex
|
||||
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 ROWS
|
||||
(@network_start_hex, @network_last_hex, @geoname_id, @registered_country_geoname_id, @represented_country_geoname_id,
|
||||
@is_anonymous_proxy, @is_satellite_provider, @postal_code, @latitude, @longitude, @accuracy_radius)
|
||||
SET network_start_hex = unhex(@network_start_hex),
|
||||
network_last_hex = unhex(@network_last_hex),
|
||||
geoname_id = NULLIF(@geoname_id, ''),
|
||||
registered_country_geoname_id = NULLIF(@registered_country_geoname_id, ''),
|
||||
represented_country_geoname_id = NULLIF(@represented_country_geoname_id, ''),
|
||||
is_anonymous_proxy = NULLIF(@is_anonymous_proxy, ''),
|
||||
is_satellite_provider = NULLIF(@is_satellite_provider, ''),
|
||||
postal_code = NULLIF(@postal_code, ''),
|
||||
latitude = NULLIF(@latitude, ''),
|
||||
longitude = NULLIF(@longitude, ''),
|
||||
accuracy_radius = NULLIF(@accuracy_radius, '')
|
||||
`
|
||||
// 执行SQL语句
|
||||
_, err = db.Exec(CreateTable)
|
||||
if err != nil {
|
||||
fmt.Println("执行SQL语句错误:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("创建表geolite2_city_ipv4_hex完成")
|
||||
_, err = db.Exec(sqlStatement)
|
||||
if err != nil {
|
||||
fmt.Println("执行SQL语句错误:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("表geolite2_city_ipv4_hex数据加载完成")
|
||||
}
|
||||
|
||||
func ImportLocation_zhCN() {
|
||||
mysql.RegisterLocalFile("C:/Users/wangh/Desktop/code/checkIP/db/GeoLite2-City-Locations-zh-CN.csv")
|
||||
db, err := sql.Open("mysql", "root:990821@tcp(192.168.2.20:3306)/checkip")
|
||||
if err != nil {
|
||||
fmt.Println("数据库连接错误:", err)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
//创建表
|
||||
CreateTable := `
|
||||
CREATE TABLE geolite2_city_locations_zh_cn (
|
||||
geoname_id int not null,
|
||||
locale_code text not null,
|
||||
continent_code text not null,
|
||||
continent_name text not null,
|
||||
country_iso_code text,
|
||||
country_name text,
|
||||
subdivision_1_iso_code text,
|
||||
subdivision_1_name text,
|
||||
subdivision_2_iso_code text,
|
||||
subdivision_2_name text,
|
||||
city_name text,
|
||||
metro_code int,
|
||||
time_zone text,
|
||||
is_in_european_union bool,
|
||||
primary key (geoname_id, locale_code(5))
|
||||
);
|
||||
`
|
||||
//导入geolite2-city-locations_zh_cn位置信息数据
|
||||
sqlStatement := `LOAD DATA LOCAL INFILE 'C:/Users/wangh/Desktop/code/checkIP/db/GeoLite2-City-Locations-zh-CN.csv'
|
||||
into table geolite2_city_locations_zh_cn
|
||||
fields terminated by ',' enclosed by '"' lines terminated by '\n' ignore 1 rows (
|
||||
geoname_id, locale_code, continent_code, continent_name,
|
||||
@country_iso_code, @country_name, @subdivision_1_iso_code, @subdivision_1_name,
|
||||
@subdivision_2_iso_code, @subdivision_2_name, @city_name, @metro_code, @time_zone,
|
||||
is_in_european_union
|
||||
)
|
||||
set country_iso_code = nullif(@country_iso_code, ''),
|
||||
country_name = nullif(@country_name, ''),
|
||||
subdivision_1_iso_code = nullif(@subdivision_1_iso_code, ''),
|
||||
subdivision_1_name = nullif(@subdivision_1_name, ''),
|
||||
subdivision_2_iso_code = nullif(@subdivision_2_iso_code, ''),
|
||||
subdivision_2_name = nullif(@subdivision_2_name, ''),
|
||||
city_name = nullif(@city_name, ''),
|
||||
metro_code = nullif(@metro_code, ''),
|
||||
time_zone = nullif(@time_zone, '');
|
||||
`
|
||||
// 执行SQL语句
|
||||
_, err = db.Exec(CreateTable)
|
||||
if err != nil {
|
||||
fmt.Println("执行SQL语句错误:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("创建表geolite2_city_locations_zh_cn完成")
|
||||
_, err = db.Exec(sqlStatement)
|
||||
if err != nil {
|
||||
fmt.Println("执行SQL语句错误:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("表geolite2_city_locations_zh_cn数据加载完成")
|
||||
}
|
||||
|
||||
func ImportLocation_en() {
|
||||
mysql.RegisterLocalFile("C:/Users/wangh/Desktop/code/checkIP/db/GeoLite2-City-Locations-en.csv")
|
||||
db, err := sql.Open("mysql", "root:990821@tcp(192.168.2.20:3306)/checkip")
|
||||
if err != nil {
|
||||
fmt.Println("数据库连接错误:", err)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
//创建表
|
||||
CreateTable := `
|
||||
CREATE TABLE geolite2_city_locations_en (
|
||||
geoname_id int not null,
|
||||
locale_code text not null,
|
||||
continent_code text not null,
|
||||
continent_name text not null,
|
||||
country_iso_code text,
|
||||
country_name text,
|
||||
subdivision_1_iso_code text,
|
||||
subdivision_1_name text,
|
||||
subdivision_2_iso_code text,
|
||||
subdivision_2_name text,
|
||||
city_name text,
|
||||
metro_code int,
|
||||
time_zone text,
|
||||
is_in_european_union bool,
|
||||
primary key (geoname_id, locale_code(5))
|
||||
);
|
||||
`
|
||||
//导入geolite2_city_locations_en位置信息数据
|
||||
sqlStatement := `LOAD DATA LOCAL INFILE 'C:/Users/wangh/Desktop/code/checkIP/db/GeoLite2-City-Locations-en.csv'
|
||||
into table geolite2_city_locations_en
|
||||
fields terminated by ',' enclosed by '"' lines terminated by '\n' ignore 1 rows (
|
||||
geoname_id, locale_code, continent_code, continent_name,
|
||||
@country_iso_code, @country_name, @subdivision_1_iso_code, @subdivision_1_name,
|
||||
@subdivision_2_iso_code, @subdivision_2_name, @city_name, @metro_code, @time_zone,
|
||||
is_in_european_union
|
||||
)
|
||||
set country_iso_code = nullif(@country_iso_code, ''),
|
||||
country_name = nullif(@country_name, ''),
|
||||
subdivision_1_iso_code = nullif(@subdivision_1_iso_code, ''),
|
||||
subdivision_1_name = nullif(@subdivision_1_name, ''),
|
||||
subdivision_2_iso_code = nullif(@subdivision_2_iso_code, ''),
|
||||
subdivision_2_name = nullif(@subdivision_2_name, ''),
|
||||
city_name = nullif(@city_name, ''),
|
||||
metro_code = nullif(@metro_code, ''),
|
||||
time_zone = nullif(@time_zone, '');
|
||||
`
|
||||
// 执行SQL语句
|
||||
_, err = db.Exec(CreateTable)
|
||||
if err != nil {
|
||||
fmt.Println("执行SQL语句错误:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("创建表geolite2_city_locations_en完成")
|
||||
_, err = db.Exec(sqlStatement)
|
||||
if err != nil {
|
||||
fmt.Println("执行SQL语句错误:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("表geolite2_city_locations_en数据加载完成")
|
||||
}
|
||||
Reference in New Issue
Block a user