github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/cmd/test/others/func/import/dict.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/easysoft/zendata/cmd/test/others/func/comm"
     8  	"github.com/easysoft/zendata/cmd/test/others/func/model"
     9  	fileUtils "github.com/easysoft/zendata/pkg/utils/file"
    10  )
    11  
    12  func main() {
    13  	filePath := "/Users/aaron/work/zentao/product/zd/新华字典/data/word.json"
    14  
    15  	db := comm.GetDB()
    16  	db.Exec(fmt.Sprintf(comm.TruncateTable, model.DataColor{}.TableName()))
    17  	db.AutoMigrate(
    18  		&model.DataDict{},
    19  	)
    20  
    21  	content := fileUtils.ReadFileBuf(filePath)
    22  	records := make([]model.DataDict, 0)
    23  	json.Unmarshal(content, &records)
    24  
    25  	for _, record := range records {
    26  		db.Save(&record)
    27  	}
    28  }