在 “auto_increment” 附近有语法错误。

huangapple go评论238阅读模式
英文:

gorp: near "auto_increment": syntax error

问题

我正在尝试编写一个简单的程序,使用gorp在表中插入行,但在创建表时出现错误。

以下是代码:

  1. package main
  2. import _ "github.com/mattn/go-sqlite3"
  3. import "database/sql"
  4. import "fmt"
  5. import "github.com/go-gorp/gorp"
  6. func main() {
  7. type Person struct {
  8. Identi int64
  9. Created int64
  10. FName string
  11. LName string
  12. }
  13. db, _ := sql.Open("sqlite3", "mydb.db")
  14. dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
  15. _ = dbmap.AddTable(Person{}).SetKeys(true, "Identi")
  16. err := dbmap.CreateTables()
  17. if err != nil {
  18. fmt.Println("table not created : " + err.Error())
  19. }
  20. person := &Person{
  21. FName: "Joe",
  22. LName: "Smith",
  23. }
  24. err = dbmap.Insert(person)
  25. if err != nil {
  26. fmt.Println("err" + err.Error())
  27. }
  28. }

我得到以下错误:

  1. table not created : near "auto_increment": syntax error
  2. 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:

  1. package main
  2. import _ "github.com/mattn/go-sqlite3"
  3. import "database/sql"
  4. import "fmt"
  5. import "github.com/go-gorp/gorp"
  6. func main() {
  7. type Person struct {
  8. Identi int64
  9. Created int64
  10. FName string
  11. LName string
  12. }
  13. db, _ := sql.Open("sqlite3", "mydb.db")
  14. dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
  15. _ = dbmap.AddTable(Person{}).SetKeys(true, "Identi")
  16. err := dbmap.CreateTables()
  17. if err != nil {
  18. fmt.Println("table not created : " + err.Error())
  19. }
  20. person := &Person{
  21. FName: "Joe",
  22. LName: "Smith",
  23. }
  24. err = dbmap.Insert(person)
  25. if err != nil {
  26. fmt.Println("err" + err.Error())
  27. }
  28. }

I am getting following error:

  1. table not created : near "auto_increment": syntax error
  2. err no such table: Person

I would appreciate your help!

答案1

得分: 0

你正在使用SQLite数据库的MySQL方言。将以下代码进行更改:

  1. dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}

更改为:

  1. dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
英文:

You're using MySQL dialect with an SQLite database. Change

  1. dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}

to

  1. dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}

huangapple
  • 本文由 发表于 2015年8月22日 00:35:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/32145324.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定