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

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

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{}}

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:

确定