github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/cmd/test/others/func/import/idiom.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "flag" 6 "fmt" 7 "strings" 8 9 "github.com/easysoft/zendata/cmd/test/others/func/comm" 10 "github.com/easysoft/zendata/cmd/test/others/func/model" 11 fileUtils "github.com/easysoft/zendata/pkg/utils/file" 12 ) 13 14 // 成语 15 func main() { 16 var insertTemplate = "INSERT INTO %s (derivation, example, explanation, pinyin, word, abbreviation) VALUES %s;" 17 var createTableTempl = `CREATE TABLE IF NOT EXISTS %s ( 18 id bigint auto_increment, 19 20 derivation varchar(1000) not null, 21 example varchar(1000) not null, 22 explanation varchar(1000) not null, 23 pinyin varchar(1000) not null, 24 word varchar(1000) not null, 25 abbreviation varchar(1000) not null, 26 27 tag varchar(50), 28 primary key(id) 29 ) engine=innodb default charset=utf8 auto_increment=1;` 30 31 var tableName string 32 var filePath string 33 34 flag.StringVar(&tableName, "t", "", "") 35 flag.StringVar(&filePath, "f", "", "") 36 37 flag.Parse() 38 39 tableName = "biz_" + strings.TrimLeft(tableName, "biz_") 40 db := comm.GetDB() 41 db.Exec(fmt.Sprintf(comm.TruncateTable, tableName)) 42 43 createTableSql := fmt.Sprintf(createTableTempl, tableName) 44 err := db.Exec(createTableSql).Error 45 if err != nil { 46 fmt.Printf("create table %s failed, err %s", tableName, err.Error()) 47 return 48 } 49 50 content := fileUtils.ReadFileBuf(filePath) 51 records := make([]model.DataIdiom, 0) 52 json.Unmarshal(content, &records) 53 54 insertSqlArr := make([]string, 0) 55 56 for _, record := range records { 57 derivation := strings.ReplaceAll(record.Derivation, "'", "''") 58 example := strings.ReplaceAll(record.Example, "'", "''") 59 explanation := strings.ReplaceAll(record.Explanation, "'", "''") 60 pinyin := strings.ReplaceAll(record.Pinyin, "'", "''") 61 word := strings.ReplaceAll(record.Word, "'", "''") 62 abbreviation := strings.ReplaceAll(record.Abbreviation, "'", "''") 63 64 insert := fmt.Sprintf("('%s', '%s', '%s', '%s', '%s', '%s')", 65 derivation, example, explanation, pinyin, word, abbreviation) 66 insertSqlArr = append(insertSqlArr, insert) 67 } 68 69 for i := 0; i < 10000; i++ { 70 start := i * 1000 71 end := (i + 1) * 1000 72 73 if end > len(insertSqlArr) { 74 end = len(insertSqlArr) 75 } 76 77 arr := insertSqlArr[start:end] 78 79 sql := fmt.Sprintf(insertTemplate, tableName, strings.Join(arr, ",")) 80 err = db.Exec(sql).Error 81 if err != nil { 82 fmt.Printf("insert data failed, err %s", err.Error()) 83 return 84 } 85 86 if end >= len(insertSqlArr) { 87 break 88 } 89 } 90 }