I have a config which contains all the data on the database and I need to import this data. But I can’t import the config variable, which is essentially a string in the БД MySQL opening method!
db, err = sql.Open("mysql", Config.MYSQL.Data) Config.yml:
MYSQL: data: root@/formm columns: api Full code:
import ( "fmt" "database/sql" "github.com/jinzhu/configor" _ "github.com/go-sql-driver/mysql" ) func DBPrimary() { var Config = struct { MYSQL struct { Data string `yaml:data` Columns string `yaml:columns` } `yaml:"FTP"` }{} configor.Load(&Config, "config.yml") var db *sql.DB var err error db, err = sql.Open("mysql", Config.MYSQL.Data) if err != nil { log.Fatal(err) } else { fmt.Println("OK Connect to DB") } defer db.Close() err = db.Ping() if err != nil { log.Fatal(err) } else { fmt.Println("Ok test a connection") } stmt, err := db.Prepare("INSERT INTO (?) VALUES (?,?,?)") if err != nil { log.Fatal(err) } else { fmt.Println("Succ insert a value") } rows, err := stmt.Exec("api", , "succ3", "succ2", "succ1") if err != nil { log.Fatal(err) } else { fmt.Println("Succ insert smtm value!") } ttt, err := db.Exec("CREATE TABLE primary ( db_id integer, db_text text, db_single varchar(255) )") if err != nil { log.Fatal(err) } else { fmt.Println("Succ create table!") } fmt.Println(rows, ttt) } Log: 2017/12/08 11:52:34 Error 1046: No database selected
Data on the database is imported from the config only; by other means it cannot be imported. How to solve a problem?
Config.MYSQL.DatathisConfig.MYSQL.Datalog and show what's in it. - Ivan Blacksql.Open(...)add the linelog.Println("CONFIG:", Config.MYSQL.Data). It seems that you have a config curve that's all.No database selectedmeans you need to select a database. There must be something likeuser:password@tcp(127.0.0.1:3306)/hello, wherehellois the name of the database. Which is probably not. And it should be. - Ivan Black