func initDb() { var err error Engine, err = xorm.NewEngine("mysql", mysql_dsn) if err != nil { panic(err.Error()) // proper error handling instead of panic in your app } tbMapper := core.NewPrefixMapper(core.SnakeMapper{}, "pk_") Engine.SetTableMapper(tbMapper) Engine.ShowSQL = true //则会在控制台打印出生成的SQL语句; Engine.ShowDebug = true //则会在控制台打印调试信息; Engine.ShowErr = true //则会在控制台打印错误信息; Engine.ShowWarn = true //则会在控制台打印警告信息; f, err := os.Create("sql.log") if err != nil { println("error:") println(err.Error()) return } defer f.Close() Engine.Logger = xorm.NewSimpleLogger(f) // fmt.Println(Engine) }
func main() { var err error engine, err = xorm.NewEngine("mysql", "root:123456@tcp(192.168.1.172:3306)/jc_test") if err != nil { panic(err.Error()) // proper error handling instead of panic in your app } tbMapper := core.NewPrefixMapper(core.SnakeMapper{}, "pk_") engine.SetTableMapper(tbMapper) engine.ShowSQL = true //则会在控制台打印出生成的SQL语句; engine.ShowDebug = true //则会在控制台打印调试信息; engine.ShowErr = true //则会在控制台打印错误信息; engine.ShowWarn = true //则会在控制台打印警告信息; fmt.Println(err) // fmt.Println(engine.DBMetas()) // sql := "show tables" //results, err := engine.Query(sql) //fmt.Println(results) //fmt.Println(err) f, err := os.Create("sql.log") if err != nil { println(err.Error()) return } defer f.Close() engine.Logger = xorm.NewSimpleLogger(f) var odds_url = "http://odds.500.com/fenxi/yazhi-TTT.shtml" a := time.Now().Unix() - 86400 fmt.Println(a) yesday := time.Unix(a, 0).Format("2006-01-02") schs := make([]Schedule, 0) engine.Distinct("schedule_fid", "schedule_no", "schedule_date").Where("schedule_date = ?", yesday).Find(&schs) //fmt.Println(schs) for _, sch_info := range schs { fmt.Println(sch_info.ScheduleFid) fid := fmt.Sprintf("%d", sch_info.ScheduleFid) schedule_no := sch_info.ScheduleNo schedule_date := sch_info.ScheduleDate schedule_odds_url := strings.Replace(odds_url, "TTT", fid, -1) fmt.Println(schedule_odds_url) res := ParseOddUrl(schedule_odds_url, fid, schedule_no, schedule_date) fmt.Println(res) } }
func init() { var err error DB, err = xorm.NewEngine("mysql", "root:1@/chat?charset=utf8") if err != nil { panic(err) } tbMapper := core.NewPrefixMapper(core.SnakeMapper{}, "t_") DB.SetTableMapper(tbMapper) DB.SetColumnMapper(core.SameMapper{}) DB.SetMaxOpenConns(10) }
func EngineForCfg(cfg *MysqlConfig) (engine *xorm.Engine, err error) { dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8", cfg.Username, cfg.Password, cfg.Host, cfg.Port, cfg.DbName) engine, err1 := xorm.NewEngine("mysql", dataSourceName) if err1 != nil { err = err1 return } tbMapper := core.NewPrefixMapper(core.SnakeMapper{}, "t_") engine.SetTableMapper(tbMapper) engine.SetColumnMapper(core.SameMapper{}) engine.ShowSQL = cfg.ShowSql engine.ShowDebug = cfg.ShowDebug engine.ShowErr = cfg.ShowErr engine.ShowWarn = cfg.ShowWarn err = engine.Ping() return }
func init() { // read configure Conf = ReadConf() // setup xorm var err error if DB, err = xorm.NewEngine(Conf["DB"], Conf["DSN"]); err != nil { panic(err.Error()) } // set table prefix tbMapper := core.NewPrefixMapper(core.SnakeMapper{}, Conf["PREFIX"]) DB.SetTableMapper(tbMapper) // record sql DB.ShowSQL = true f, err := os.Create("sql.log") if err != nil { panic(err.Error()) } DB.Logger = xorm.NewSimpleLogger(f) }
var InitDB func() = func() { driver, spec := driverInfoFromConfig() var err error Db, err = sql.Open(driver, spec) if err != nil { log.Fatal("SQL Open failed:", err) } app.Engine, err = xorm.NewEngine(driver, spec) if err != nil { log.Fatal("NewEngine failed:", err) } app.Engine.ShowSQL = true // revel.Config.BoolDefault("db.show_sql", true) app.Engine.ShowDebug = true app.Engine.SetTableMapper(core.NewPrefixMapper(core.SnakeMapper{}, "t_")) app.Engine.DropTables("t_user") err = app.Engine.Sync2(new(entity.User), new(entity.UserRole), new(entity.UserLevel), new(entity.Location), new(entity.UserDetail), new(entity.UserItem)) if err != nil { log.Fatal("Sync2 with error:", err) } // do init tryInitData() } func tryInitData() { total, err := app.Engine.Count(&entity.User{}) if total > 0 && err == nil {
//初始化模塊 func Initialize() { driver, ok := revel.Config.String("database.driver") if !ok { panic("database.driver ( app.conf ) not configure") } str, ok := revel.Config.String("database.str") if !ok { panic("database.str ( app.conf ) not configure") } var err error g_engine, err = xorm.NewEngine(driver, str) if err != nil { panic(err) } //打開 日誌 默認為 false //g_engine.ShowSQL = true //在控制台打印生成的 sql //設置 表名 前綴 後綴 prefix, ok := revel.Config.String("database.prefix") if !ok { prefix = "cerberus_" } g_prefix = prefix suffix, ok := revel.Config.String("database.suffix") if !ok { suffix = "_test" } g_suffix = suffix var mapper core.IMapper mapper = core.NewPrefixMapper(core.SnakeMapper{}, prefix) mapper = core.NewSuffixMapper(mapper, suffix) g_engine.SetTableMapper(mapper) //初始化 數據庫 表 session := g_engine.NewSession() defer func() { //捕獲異常 if err := recover(); err != nil { session.Rollback() session.Close() //關閉 數據庫 連接 g_engine.Close() g_engine = nil panic(err) } else { session.Commit() session.Close() } }() //初始化表 /*user := &data.User{} if ok, err = session.IsTableExist(user); !ok { if err = session.CreateTable(user); err != nil { panic(err) } if err = session.CreateUniques(user); err != nil { panic(err) } }*/ }