func init() { // dbhost := beego.AppConfig.String("dbhost") // dbport := beego.AppConfig.String("dbport") dbuser := beego.AppConfig.String("dbuser") dbpassword := beego.AppConfig.String("dbpassword") dbname := beego.AppConfig.String("dbname") // if dbport == "" { // dbport = "3306" // } //注册mysql Driver orm.RegisterDriver("mysql", orm.DRMySQL) //构造conn连接 conn := dbuser + ":" + dbpassword + "@/" + dbname + "?charset=utf8" //数据库别名 alice := "default" //不强制建数据库 force := false //打印建表过程 verbose := true orm.RegisterDataBase(alice, "mysql", conn) orm.RegisterModel(new(Admin), new(Article), new(ArticleCate), new(AutoUser), new(Baoliao), new(Item), new(ItemCate), new(ItemComment), new(ItemTag), new(Mall), new(Tag)) //建表 err := orm.RunSyncdb(alice, force, verbose) if err != nil { beego.Error(err) } if beego.AppConfig.String("runmode") == "dev" { orm.Debug = true } }
func RegistDB() { orm.RegisterModel(new(User), new(Article), new(Stream)) orm.RegisterDriver("mysql", orm.DR_MySQL) orm.RegisterDataBase("default", "mysql", "root:mysql@/newblog?charset=utf8") orm.RunSyncdb("default", false, true) }
// Init will initialize database to create tables automatically. func init() { // Registry archci database models. models.RegisterModels() // Initialize database with environment variables. server := "" username := "******" password := "******" database := "mysql" if os.Getenv(ENV_MYSQL_SERVER) != "" { server = os.Getenv(ENV_MYSQL_SERVER) } if os.Getenv(ENV_MYSQL_USERNAME) != "" { username = os.Getenv(ENV_MYSQL_USERNAME) } if os.Getenv(ENV_MYSQL_PASSWORD) != "" { password = os.Getenv(ENV_MYSQL_PASSWORD) } if os.Getenv(ENV_MYSQL_DATABASE) != "" { database = os.Getenv(ENV_MYSQL_DATABASE) } // The datasource looks like "root:root@/archci?charset=utf8". DATASOURCE := username + ":" + password + "@" + server + "/" + database + "?charset=utf8" fmt.Println("Connect to database with " + DATASOURCE) orm.RegisterDriver(MYSQL_DRIVER, orm.DR_MySQL) orm.RegisterDataBase("default", MYSQL_DRIVER, DATASOURCE, 30) orm.RunSyncdb("default", false, true) }
func init() { orm.RegisterDataBase("default", "sqlite3", DATABASEPATH) orm.RegisterModel(new(DB_message), new(DB_transaction), new(DB_blocks), new(DB_send)) orm.RunSyncdb("default", false, true) myorm = orm.NewOrm() myorm.Using("default") }
func main() { orm.Debug = true orm.RunSyncdb("default", false, true) /* 设置Session配置*/ beego.SessionOn = false beego.Run() }
func init() { /* 로그기록 */ //NewLogger(10000) beego.SetLogger("file", `{"filename":"server.log"}`) /* 데이타베이스 드라이브 설정 */ orm.RegisterDriver("mysql", orm.DR_MySQL) orm.RegisterDataBase("default", "mysql", "root:Kim616294.@/cluber?charset=utf8&loc=Asia%2FSeoul") /* 데이타베이스 테이블 등록 */ orm.RegisterModel(new(models.User)) orm.RegisterModel(new(models.Log)) orm.RegisterModel(new(models.Contents)) // 사용 데이타베이스 name := "default" // 서버 시작시 테이블 재생성 force := false // SQL 로그 확인 verbose := true // 데이타베이스를 동기화 하고 에러 발생시 err 에 에러내용 입력 err := orm.RunSyncdb(name, force, verbose) if err != nil { fmt.Println(err) } }
func main() { orm.Debug = true orm.RunSyncdb("default", true, true) beego.Router("/", &controllers.MainController{}) beego.Run() }
func LoadConfig() { redisServer := beego.AppConfig.String("redis_resource") redisServer = "{\"conn\":\"" + redisServer + "\"}" store, err := cache.NewCache("redis", redisServer) if err != nil { log.Panic("缓存出错" + err.Error()) } Captcha = captcha.NewWithFilter("/captcha/", store) driverName := beego.AppConfig.String("driverName") dataSource := beego.AppConfig.String("dataSource") maxIdle, _ := beego.AppConfig.Int("maxIdle") maxOpen, _ := beego.AppConfig.Int("maxOpen") orm.RegisterDriver("mysql", orm.DR_MySQL) // set default database err = orm.RegisterDataBase("default", driverName, dataSource, maxIdle, maxOpen) if err != nil { beego.Error(err) } orm.RunCommand() err = orm.RunSyncdb("default", false, false) if err != nil { beego.Error(err) } SocialAuthInit() config.InitConfig() }
func main() { // Database alias. name := "default" // Drop table and re-create. force := true // Print log. verbose := true // Error. err := orm.RunSyncdb(name, force, verbose) if err != nil { fmt.Println(err) } o := orm.NewOrm() o.Using("default") seed(o) beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{ AllowAllOrigins: true, })) beego.Run() }
func main() { // get config data db_driver := beego.AppConfig.String("db_driver") if db_driver != "" { log.Println("db_driver: ", db_driver) } db_source := beego.AppConfig.String("db_source") if db_source != "" { log.Println("db_source: ", db_source) } db_maxidle, err := beego.AppConfig.Int("db_maxidle") if err == nil && db_maxidle != 0 { log.Println("db_maxidle: ", db_maxidle) } db_maxconn, err := beego.AppConfig.Int("db_maxconn") if err == nil && db_maxconn != 0 { log.Println("db_maxconn: ", db_maxconn) } // set database err = orm.RegisterDataBase("default", db_driver, db_source, db_maxidle, db_maxconn) if err != nil { beego.Error(err) } err = orm.RunSyncdb("default", false, false) if err != nil { beego.Error(err) } beego.SetStaticPath("/upload", "upload") beego.SessionOn = true beego.Run() }
func DBInit() { orm.Debug = true orm.RegisterDataBase("default", "sqlite3", "data.db", 30) orm.RunSyncdb("default", true, true) o := orm.NewOrm() o.Using("default") user := &User{ Account: "*****@*****.**", Password: "******", Showname: "管理员", } h := md5.New() h.Write([]byte(user.Password)) user.Password = hex.EncodeToString(h.Sum(nil)) id, err := o.Insert(user) if err != nil { log.Info("%s.", err.Error()) } else { log.Info("User added,ID is:%d.", id) } }
////////////////////////////////////////////////// // init initializes the database and rebuilds the tables from file data func init() { dbuser := beego.AppConfig.String("mysqluser") dbpass := beego.AppConfig.String("mysqlpass") database := beego.AppConfig.String("mysqldb") orm.RegisterDataBase("default", "mysql", dbuser+":"+dbpass+"@/"+database+"?charset=utf8") orm.SetMaxIdleConns("default", 30) orm.SetMaxOpenConns("default", 30) orm.RegisterModel(new(User), new(Project), new(ProjectUser), new(Message), new(Star), new(Groups), new(GroupUser), new(Rights), new(SessionToken)) // Drop all runtime tables o := orm.NewOrm() o.Raw("SET FOREIGN_KEY_CHECKS=0;").Exec() _, err := o.Raw("TRUNCATE TABLE user;").Exec() if err != nil { // If table can't be truncated, rebuild all tables (CAUTION: Star and Message db are lost!) // This is only for absolute startup err := orm.RunSyncdb("default", true, true) if err != nil { beego.Error(err) } } o.Raw("TRUNCATE TABLE message;").Exec() o.Raw("TRUNCATE TABLE rights;").Exec() o.Raw("TRUNCATE TABLE project;").Exec() o.Raw("TRUNCATE TABLE project_user;").Exec() o.Raw("TRUNCATE TABLE groups;").Exec() o.Raw("TRUNCATE TABLE group_user;").Exec() o.Raw("SET FOREIGN_KEY_CHECKS=1;").Exec() createAllUserDatabaseEntries() }
func main() { orm.Debug = true orm.RunSyncdb("default", false, true) beego.Router("/", &controllers.MainController{}) beego.Router("/login", &controllers.LoginController{}) beego.Router("/category", &controllers.CategoryController{}) beego.Router("/reply", &controllers.ReplyController{}) beego.AutoRouter(&controllers.ReplyController{}) beego.Router("/topic", &controllers.TopicController{}) beego.AutoRouter(&controllers.TopicController{}) os.Mkdir("attachment", os.ModePerm) beego.SetStaticPath("/attachment", "attachment") err := i18n.SetMessage("en-US", "conf/locale_en-US.ini") if err != nil { beego.Error(err) } err = i18n.SetMessage("zh-CN", "conf/locale_zh-CN.ini") if err != nil { beego.Error(err) } err = i18n.SetMessage("zh-HK", "conf/locale_zh-HK.ini") if err != nil { beego.Error(err) } beego.AddFuncMap("i18n", i18n.Tr) beego.Run() }
func init() { //orm.RegisterDataBase("default", "mysql", "root@/gotalk?charset=utf8", 30) beego.AppConfigPath = "../conf/app.conf" beego.ParseConfig() setting.ReadConfig() orm.RunSyncdb("default", true, false) }
/** * 同步数据库 */ func Syncdb() { createdb() Connect() o = orm.NewOrm() // 数据库别名 name := "default" // drop table 后再建表 force := true // 打印执行过程 verbose := true // 遇到错误立即返回 err := orm.RunSyncdb(name, force, verbose) if err != nil { fmt.Println(err) } insertUser() insertCategory() insertShop() insertFood() //insertNodes() fmt.Println("database init is complete.\nPlease restart the application") }
func init() { //For version 1.6 //orm.DRMySQL //orm.DRSqlite //orm.DRPostgres // < 1.6 //orm.DR_MySQL //orm.DR_Sqlite //orm.DR_Postgres // 参数1 driverName // 参数2 数据库类型 // 这个用来设置 driverName 对应的数据库类型 // mysql / sqlite3 / postgres 这三种是默认已经注册过的,所以可以无需设置 orm.RegisterDriver("mysql", orm.DRMySQL) // 参数1 数据库的别名,用来在ORM中切换数据库使用 // 参数2 driverName // 参数3 对应的链接字符串 orm.RegisterDataBase("default", "mysql", "root:admin@/sport_collage?charset=utf8") // 参数4(可选) 设置最大空闲连接 // 参数5(可选) 设置最大数据库连接 (go >= 1.2) //maxIdle := 30 //maxConn := 30 //orm.RegisterDataBase("default", "mysql", "root:root@/orm_test?charset=utf8", maxIdle, maxConn) orm.RunSyncdb("default", false, false) }
func main() { err := orm.RunSyncdb("default", false, false) if err != nil { beego.Error(err) } beego.Run() }
func main() { // 开启 ORM 调试模式 orm.Debug = true // 自动建表 orm.RunSyncdb("default", false, true) // o := orm.NewOrm() // //o.Using("default") // var user models.User // user.Id = 1 // user.Name = "admin" // user.Age = 20 // var user_conn models.UserConn // user_conn.Id = 1 // user_conn.Phone = "023-1231123" // user_conn.Mobile = "12312312312" // user_conn.Email = "*****@*****.**" // user_conn.User = &user // fmt.Println(o.Insert(&user)) // fmt.Println(o.Insert(&user_conn)) beego.Run() }
func init() { dbname := "default" runmode := beego.AppConfig.String("runmode") datasource := beego.AppConfig.String("datasource") switch runmode { case "prod": //orm.RegisterDriver("postgres", orm.DR_Postgres) //orm.RegisterDataBase(dbname, "postgres", datasource, 30) //orm.SetMaxIdleConns(dbname, 100) //orm.SetMaxOpenConns(dbname, 100) orm.RegisterDriver("mysql", orm.DRMySQL) orm.RegisterDataBase(dbname, "mysql", datasource) case "dev": orm.RegisterDriver("mysql", orm.DRMySQL) orm.RegisterDataBase(dbname, "mysql", datasource) default: //orm.RegisterDataBase(dbname, "sqlite3", datasource) orm.RegisterDriver("mysql", orm.DRMySQL) orm.RegisterDataBase(dbname, "mysql", datasource) } orm.DefaultTimeLoc = time.FixedZone("Asia/Tokyo", 9*60*60) force, verbose := false, true err := orm.RunSyncdb(dbname, force, verbose) if err != nil { panic(err) } // orm.RunCommand() }
func main() { //初始化 initialize() beego.Info(beego.AppName, APP_VER) if !controllers.IsPro { beego.SetStaticPath("/static_source", "static_source") beego.DirectoryIndex = true } // 开启 ORM 调试模式 orm.Debug = true // 自动建表 orm.RunSyncdb("default", false, true) beego.Router("/", &controllers.MainController{}) beego.Router("/user/reg", &controllers.UserController{}, "post:reg") // Register template functions. beego.AddFuncMap("i18n", i18n.Tr) beego.Run() }
func main() { orm.Debug = true orm.RunSyncdb("default", false, true) //models.AddUser() //models.AddTotalAndWin("martin", true) beego.Run() }
func TestMain(m *testing.M) { var err error test.db, err = sql.Open("sqlite3", ":memory:") panicIfErr(err) _, err = test.db.Exec("CREATE TABLE users(a, b UNIQUE)") panicIfErr(err) _, err = test.db.Exec("INSERT INTO users VALUES(1, 2)") panicIfErr(err) test.dbx = sqlx.NewDb(test.db, "sqlite3") test.dbgorm, err = gorm.Open("sqlite3", ":memory:") panicIfErr(err) user := &User{"1", "2"} test.dbgorm.CreateTable(user) test.dbgorm.Create(user) err = orm.RunSyncdb("default", true, false) panicIfErr(err) test.dbbeegoorm = orm.NewOrm() beeuser := &BeeUser{A: "1", B: "2"} test.dbbeegoorm.Insert(beeuser) os.Exit(m.Run()) }
func initDb() { iniconf, err := config.NewConfig("ini", "conf/app.conf") if err != nil { beego.Error(err) } driverName := iniconf.String("orm::driver_name") dataSource := iniconf.String("orm::data_source") maxIdle, _ := iniconf.Int("orm::max_idle_conn") maxOpen, _ := iniconf.Int("orm::max_open_conn") // set default database err = orm.RegisterDataBase("default", driverName, dataSource, maxIdle, maxOpen) if err != nil { beego.Error(err) } // orm.Debug = true //orm.RunCommand() //执行命令行的 err = orm.RunSyncdb("default", false, false) //建表的 ////数据库别名,不强制建数据库,打印建表过程 if err != nil { beego.Error(err) } }
func init() { duangcfg, err := config.NewConfig("ini", "conf/duang.conf") if err != nil { fmt.Println(err) return } orm.RegisterDriver("mysql", orm.DR_MySQL) orm.RegisterDataBase("default", "mysql", fmt.Sprintf("%s:%s@%s/%s?charset=utf8", duangcfg.String("db_user"), duangcfg.String("db_pass"), duangcfg.String("db_addr"), duangcfg.String("db_name"))) orm.RegisterModel( new(Unit), new(UnitParameter), new(Dockerd), new(Ip), new(Container), //new(Frontend), //new(Backend), ) force, err := duangcfg.Bool("db_dropifexist") if err != nil { force = false } verbose := true err = orm.RunSyncdb("default", force, verbose) if err != nil { beego.Error(err) } }
func init() { //获取服务器数据库相关信息 dbhost := beego.AppConfig.String("db_host") dbport := beego.AppConfig.String("db_port") dbuser := beego.AppConfig.String("db_user") dbpassword := beego.AppConfig.String("db_pass") dbname := beego.AppConfig.String("db_name") dbIdle, _ := beego.AppConfig.Int("db_max_idle_conn") dbConn, _ := beego.AppConfig.Int("db_max_open_conn") dburl := dbuser + ":" + dbpassword + "@tcp(" + dbhost + ":" + dbport + ")/" + dbname + "?charset=utf8" orm.RegisterDataBase("default", "mysql", dburl, dbIdle, dbConn) //注册模型 orm.RegisterModel(new(Category), new(Topic), new(Comment)) if beego.AppConfig.String("runmode") == "dev" { orm.Debug = true } //新建数据库表 orm.RunSyncdb("default", false, true) //附件上传文件夹处理 os.Mkdir("attachment", os.ModePerm) }
func Install() error { err := orm.RunSyncdb("default", true, true) if err != nil { return err } return nil }
func createTables() error { name := "default" force := false verbose := true err := orm.RunSyncdb(name, force, verbose) return err }
func init() { orm.RegisterDriver("mysql", orm.DRMySQL) orm.RegisterDataBase("default", "mysql", "root:root@tcp(localhost:3306)/blog?charset=utf8") orm.RegisterModel(new(class.User)) orm.RunSyncdb("default", false, true) }
func createTable() { name := "default" force := false verbose := true if err := orm.RunSyncdb(name, force, verbose); err != nil { beego.Error(err) } }
func main() { orm.Debug = true orm.RunSyncdb("default", false, true) beego.Router("/", &handler.MainHandler{}) beego.Run() }