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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/easysoft/zendata/cmd/test/others/func/comm"
     8  	fileUtils "github.com/easysoft/zendata/pkg/utils/file"
     9  )
    10  
    11  func main() {
    12  	var insertTemplate = "INSERT INTO %s (name) VALUES %s;"
    13  
    14  	tableName := "animal_plant"
    15  	filePath := "/Users/aaron/work/zentao/product/zd/行业数据/动植物.txt"
    16  
    17  	tableName = "biz_data_" + tableName
    18  	db := comm.GetDB()
    19  	err := db.Exec(fmt.Sprintf(comm.TruncateTable, tableName)).Error
    20  	if err != nil {
    21  		panic(err)
    22  	}
    23  
    24  	content := fileUtils.ReadFile(filePath)
    25  
    26  	insertSqlArr := make([]string, 0)
    27  
    28  	arr := strings.Split(content, "\n")
    29  	for _, line := range arr {
    30  		col1 := strings.Split(strings.TrimSpace(line), " ")[0]
    31  		col1 = strings.TrimSpace(col1)
    32  
    33  		insert := fmt.Sprintf("('%s')", col1)
    34  		insertSqlArr = append(insertSqlArr, insert)
    35  	}
    36  
    37  	for i := 0; i < 1000; i++ {
    38  		start := i * 10000
    39  		end := (i + 1) * 10000
    40  
    41  		if end > len(insertSqlArr) {
    42  			end = len(insertSqlArr)
    43  		}
    44  
    45  		arr := insertSqlArr[start:end]
    46  
    47  		sql := fmt.Sprintf(insertTemplate, tableName, strings.Join(arr, ","))
    48  		err = db.Exec(sql).Error
    49  		if err != nil {
    50  			fmt.Printf("insert data failed, err %s", err.Error())
    51  			return
    52  		}
    53  
    54  		if end >= len(insertSqlArr) {
    55  			break
    56  		}
    57  	}
    58  }