创建了一个table,用faker做虚拟数据的时候,总是提示我错误,print了sql语句:INSERT INTO CONTACTS(ID,CreatedAt,UpdatedAt,AccountId,FirstName,LastName) VALUES (93d0145a-da99-45dd-98eb-96ed4407a605,1994-03-17 21:27:54,1994-08-08 14:43:56,10,Robin,Dietrich)
放到库里发现是value里没有引号,我声明的是string的变量,为什么在代码里运行就不行呢?
func fakerContact(db *sql.DB){
fmt.Println("Create Faker Contact...")
ID := fmt.Sprintf(faker.UUIDHyphenated())
CreatedAt := fmt.Sprintf(faker.Timestamp())
UpdatedAt := fmt.Sprintf(faker.Timestamp())
AccountId := fmt.Sprintf(faker.DayOfMonth())
FirstName := fmt.Sprintf(faker.FirstName())
LastName := fmt.Sprintf(faker.LastName())
test := "INSERT INTO CONTACTS(ID,CreatedAt,UpdatedAt,AccountId,FirstName,LastName) VALUES ("+ID+","+CreatedAt+","+UpdatedAt+","+AccountId+","+FirstName+","+LastName+")"
fmt.Println("test:"+test)
_, err := db.Exec("INSERT INTO CONTACTS(ID,CreatedAt,UpdatedAt,AccountId,FirstName,LastName) VALUES (?,?,?,?,?,?)",ID,CreatedAt,UpdatedAt,AccountId,FirstName,LastName)
if err != nil {
log.Fatal(err)
} else {
fmt.Println("Create Faker Contact Done!")
}
}
有疑问加站长微信联系(非本文作者)