The complete code
package main
import "fmt"
/ / Point call the roll
type Point struct {
PointID uint64 `json:"pointID"`
PathName string `json:"pathName"`
PointValue float32 `json:"pointValue"`
StoreTime string `json:"storeTime"`
}
// GetSQLList too much data is divided into multiple SQL statements and returns slices of multiple SQL statements
// groupSize is changed according to personal requirements, which means that each groupSize record is merged into 1 INSERT statement
func GetSQLList(pointList []Point, groupSize int) []string {
const insertHeader string = "INSERT INTO `local_data`(`point_id`,`path_name`,`point_value`,`store_time`) VALUES"
sqlList := []string{}
sql := ""
for i := 0; i < len(pointList); i++ {
if i%groupSize == 0 {
ifsql ! ="" {
// Store the result of the last SQL concatenation
sqlList = append(sqlList, sql)
}
/ / reset the SQL
sql = insertHeader
}
ifsql ! = insertHeader { sql = sql +","
}
sql = sql + fmt.Sprintf("(%d,'%s',%f,'%s')",
pointList[i].PointID,
pointList[i].PathName,
pointList[i].PointValue,
pointList[i].StoreTime,
)
}
// Store the last SQL generated
sqlList = append(sqlList, sql)
return sqlList
}
func main(a) {
points := []Point{
{1."p1".3.14."The 2013-11-10 12:12:00"},
{2."p1".3.14."The 2013-12-10 12:12:00"},
{3."p1".3.14."The 2013-11-10 12:12:00"},
{4."p1".3.14."The 2013-12-10 12:12:00"},
{5."p1".3.14."The 2013-12-10 12:12:00"},
{6."p1".3.14."The 2013-11-10 12:12:00"},
{7."p1".3.14."The 2013-11-10 12:12:00"},
{8."p1".3.14."The 2013-12-10 12:12:00"},
{9."p1".3.14."The 2013-12-10 12:12:00"},
{10."p1".3.14."The 2013-11-12 12:12:00"},
{11."p1".3.14."The 2013-11-12 12:12:00"},
{12."p1".3.14."The 2013-11-12 12:12:00"},
{13."p1".3.14."The 2013-12-12 12:12:00"},
{14."p1".3.14."The 2013-11-12 12:12:00"},
{15."p1".3.14."The 2013-11-12 12:12:00"},
{16."p1".3.14."The 2013-12-12 12:12:00"},
{17."p1".3.14."The 2013-11-12 12:12:00"},
}
sqls := GetSQLList(points, 5)
for _, s := range sqls {
fmt.Println(s)
}
}
Copy the code
๐ technology project phase ii | and I Go those things…