I am using in-memory sqlite as follows.
func init() {
global.ConductorConfig = readConfig()
log.Println(utils.GetCurrentDir() + global.ConductorConfig.DbFile)
//db = sqlx.MustConnect("sqlite3", utils.GetCurrentDir()+global.ConductorConfig.DbFile)
db = sqlx.MustConnect("sqlite3", ":memory:")
db.Exec(schema)
task:=model.Task{}
SaveTask(&task)
db.MapperFunc(func(s string) string {
return s
})
}
in my main func, I create the table
if global.ConductorConfig.DevMode {
db.CreateTables()
}
go job.HeartbeatJob()
go job.TaskClearJob()
app.Action = func(c *cli.Context) error {
ListenAndServe()
return nil
}
Then I go 'no such table' in http handler function.
existed, err := db.GetAgentByServerName(agent.ServerName)
if err != nil {
c.JSON(http.StatusBadRequest, err)
log.Println("[conductor] error occurred when get agent by server name: " + err.Error())
return err
}
func GetAgentByServerName(name string) (*model.Agent, error) {
agent := &model.Agent{}
err := db.Get(agent, "select * from agent where ServerName=$1", name)
if err == sql.ErrNoRows {
err = nil
agent = nil
}
return agent, err
}
When I start the program, I got
error occurred when get agent by server name: no such table: agent
db schema (attributes omitted)
var schema = `
DROP TABLE IF EXISTS agent;
CREATE TABLE agent (
Id INTEGER PRIMARY KEY,
);
DROP TABLE IF EXISTS task;
CREATE TABLE task (
Id INTEGER PRIMARY KEY,
);
I can not share all the code, here is a minimal example to reproduce the same error.
package main
import (
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"log"
)
var db *sqlx.DB
func init() {
db = sqlx.MustConnect("sqlite3", ":memory:")
db.Exec("CREATE TABLE agent (Id INTEGER PRIMARY KEY,);")
}
func main() {
_, err:=db.Exec("insert into agent values (1)")
if err!=nil{
log.Println(err)
}
}