英文:
gorp: near "auto_increment": syntax error
问题
我正在尝试编写一个简单的程序,使用gorp
在表中插入行,但在创建表时出现错误。
以下是代码:
package main
import _ "github.com/mattn/go-sqlite3"
import "database/sql"
import "fmt"
import "github.com/go-gorp/gorp"
func main() {
type Person struct {
Identi int64
Created int64
FName string
LName string
}
db, _ := sql.Open("sqlite3", "mydb.db")
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
_ = dbmap.AddTable(Person{}).SetKeys(true, "Identi")
err := dbmap.CreateTables()
if err != nil {
fmt.Println("table not created : " + err.Error())
}
person := &Person{
FName: "Joe",
LName: "Smith",
}
err = dbmap.Insert(person)
if err != nil {
fmt.Println("err" + err.Error())
}
}
我得到以下错误:
table not created : near "auto_increment": syntax error
err no such table: Person
我将非常感谢您的帮助!
英文:
I am trying to write simple program to insert the row in the table using the gorp
but I am getting error on creating the table.
Following is the code:
package main
import _ "github.com/mattn/go-sqlite3"
import "database/sql"
import "fmt"
import "github.com/go-gorp/gorp"
func main() {
type Person struct {
Identi int64
Created int64
FName string
LName string
}
db, _ := sql.Open("sqlite3", "mydb.db")
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
_ = dbmap.AddTable(Person{}).SetKeys(true, "Identi")
err := dbmap.CreateTables()
if err != nil {
fmt.Println("table not created : " + err.Error())
}
person := &Person{
FName: "Joe",
LName: "Smith",
}
err = dbmap.Insert(person)
if err != nil {
fmt.Println("err" + err.Error())
}
}
I am getting following error:
table not created : near "auto_increment": syntax error
err no such table: Person
I would appreciate your help!
答案1
得分: 0
你正在使用SQLite数据库的MySQL方言。将以下代码进行更改:
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
更改为:
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
英文:
You're using MySQL dialect with an SQLite database. Change
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
to
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论