Search code examples
sqlgogorp

gorp: near "auto_increment": syntax error


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!


Solution

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