提交 1710d1f8 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

import data from text files

上级 5230b3f6
......@@ -43,6 +43,12 @@ func GetDB() *gorm.DB {
&model.DataCategory2{},
&model.DataCity{},
&model.DataColor{},
&model.DataColor{},
&model.DataChineseFamily{},
&model.DataChineseGiven{},
&model.DataEnglishFamily{},
&model.DataEnglishFamily{},
)
if err != nil {
fmt.Printf(color.RedString("migrate models failed, error: %s.", err.Error()))
......
......@@ -7,9 +7,7 @@ import (
"strings"
)
func GetExcelTable() (records []map[string]interface{}) {
filePath := "data/address/cn.v1.xlsx"
sheetName := "china"
func GetExcelTable(filePath, sheetName string) (records []map[string]interface{}) {
excel, err := excelize.OpenFile(filePath)
if err != nil {
......
package main
import (
"fmt"
"github.com/easysoft/zendata/src/test/import/comm"
"github.com/easysoft/zendata/src/test/import/model"
)
func main() {
tableName := model.DataCity{}.TableName()
db := comm.GetDB()
truncateTableSql := fmt.Sprintf(comm.TruncateTable, tableName)
db.Exec(truncateTableSql)
filePath := "data/address/cn.v1.xlsx"
sheetName := "china"
createTableSql := fmt.Sprintf(comm.CreateTableTempl, tableName)
err := db.Exec(createTableSql).Error
if err != nil {
fmt.Printf("create table %s failed, err %s", tableName, err.Error())
return
}
db := comm.GetDB()
records := comm.GetExcelTable()
records := comm.GetExcelTable(filePath, sheetName)
for _, record := range records {
po := model.DataCity{
......
package main
import (
"github.com/easysoft/zendata/src/test/import/comm"
"github.com/easysoft/zendata/src/test/import/model"
)
func main() {
filePath := "data/color/v1.xlsx"
sheetName := "color"
db := comm.GetDB()
records := comm.GetExcelTable(filePath, sheetName)
for _, record := range records {
po := model.DataColor{
Chinese: record["chinese"].(string),
English: record["english"].(string),
Hex: record["hex"].(string),
Rgb: record["rgb"].(string),
}
db.Save(&po)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册