我正在尝试将数据插入到新表中,并且出现sql错误1064。
CREATE TABLE IF NOT EXISTS `users` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`update` INT(11),
`name` VARCHAR(255),
`dob` INT(11),
`status` INT,
`avatar` VARCHAR(255),
`province` VARCHAR(255),
`city` VARCHAR(255),
`district` VARCHAR(255),
`village` VARCHAR(255),
CONSTRAINT `pk_users` PRIMARY KEY (id))
INSERT INTO `users` (`update`, `name`, `dob`, `status`,`avatar`,
`province`, `city`, `district`, `village`) VALUES (1557227442, Cortez Mante, 702086400, 1, http://lorempixel.com/150/150/, Provinsi, Kota, Kecamatan, Kelurahan)
我得到了错误:
$ panic: Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Mante, 702086400, 1, http://lorempixel.com/150/150/, Provinsi, Kota, Kecamatan, ' at line 1
我不知道是什么导致错误。有人可以帮我吗?
顺便说一句,我正在使用Golang创建和插入此表。
这是我连接到mysql并通过Go创建表的方式:
func connect() (db *sql.DB) {
dbDriver := "mysql"
dbUser := "root"
dbPass := "password"
dbName := "user"
db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"@/"+dbName)
if err != nil {
panic(err.Error())
}
return db
}
func create_table() {
db := connect()
sql := fmt.Sprintf(
"CREATE TABLE IF NOT EXISTS `users` (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
"`id` INT UNSIGNED NOT NULL AUTO_INCREMENT",
"`update` INT(11)",
"`name` VARCHAR(255)",
"`dob` INT(11)",
"`status` INT",
"`avatar` VARCHAR(255)",
"`province` VARCHAR(255)",
"`city` VARCHAR(255)",
"`district` VARCHAR(255)",
"`village` VARCHAR(255)",
"CONSTRAINT `pk_users` PRIMARY KEY (id)")
fmt.Println(sql)
insert, err := db.Query(sql)
if err != nil {
panic(err.Error())
}
defer insert.Close()
}
这是我插入数据的方式:
func AddUser(u user.User) {
db := connect()
insert_users := "INSERT INTO `users` (`update`, `name`, `dob`, `status`, `avatar`, `province`, `city`, `district`, `village`)"
values_users := fmt.Sprintf(
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
strconv.Itoa(u.Update), u.Name, strconv.Itoa(u.Dob), strconv.Itoa(u.Status), u.Avatar, u.Province, u.City, u.District, u.Village)
sql := insert_users + " " + values_users
fmt.Println(sql)
insert, err := db.Query(sql)
if err != nil {
panic(err.Error())
} else {
fmt.Println("success add user:", u.Id)
}
defer insert.Close()
}
答案 0 :(得分:2)
在字符串值周围添加引号
INSERT INTO `users` (`update`, `name`, `dob`, `status`,`avatar`,
`province`, `city`, `district`, `village`)
VALUES (1557227442, 'Cortez Mante', 702086400, 1, 'http://lorempixel.com/150/150/', 'Provinsi', 'Kota', 'Kecamatan', 'Kelurahan')
您的内容字符串
'Cortez Mante'
'http://lorempixel.com/150/150/'
'Provinsi', 'Kota', 'Kecamatan', 'Kelurahan'
,并且应使用分号结束每个单独的语句
CREATE TABLE IF NOT EXISTS `users` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`update` INT(11),
`name` VARCHAR(255),
`dob` INT(11),
`status` INT,
`avatar` VARCHAR(255),
`province` VARCHAR(255),
`city` VARCHAR(255),
`district` VARCHAR(255),
`village` VARCHAR(255),
CONSTRAINT `pk_users` PRIMARY KEY (id)
)
;
INSERT INTO `users` (`update`, `name`, `dob`, `status`,`avatar`,
`province`, `city`, `district`, `village`)
VALUES (1557227442, 'Cortez Mante', 702086400, 1, 'http://lorempixel.com/150/150/', 'Provinsi', 'Kota',
'Kecamatan', 'Kelurahan')
;
答案 1 :(得分:1)
在单个代码中添加var char值,这将起作用
province
,city
,district
,village
)值(1557227442,'Cortez Mante',702086400,1,'http://lorempixel.com/150/150/','Provinsi ','Kota','Kecamatan','Kelurahan');