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?

  • one
    Config.MYSQL.Data this Config.MYSQL.Data log and show what's in it. - Ivan Black
  • @IvanBlack 2017/12/08 11:52:34 Error 1046: No database selected - massive
  • Before sql.Open(...) add the line log.Println("CONFIG:", Config.MYSQL.Data) . It seems that you have a config curve that's all. No database selected means you need to select a database. There must be something like user:password@tcp(127.0.0.1:3306)/hello , where hello is the name of the database. Which is probably not. And it should be. - Ivan Black
  • Apparently the configuration of the curve or loaded crooked. You need to see this value to answer something specific. - Ivan Black
  • @IvanBlack is really an output: {} but why config the curve? in data is root @ / formm everything should be imported normally. - massive

1 answer 1

The thing is

 MYSQL: data: root@/formm columns: api 

and

 } `yaml:"FTP"` 

Must be

 --- FTP: data: root@/formm columns: api 

with three lines or without, there is no difference. Or change the field name to FTP.