0

I have a code that should create automatically database and table in mysql if they are not exist.

I can create table if I have DB, but I cannot create DB, because in connection string I should provide dbname.

db, err := sql.Open("mysql", "user:pass@tcp(localhost:3306)/dbname?charset=utf8")

Is it possible to do it with sql package?

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143

1 Answers1

0

Yes, just omit the database name from the url, and run a CREATE query. After that you can open a connection to your database.

Not_a_Golfer
  • 47,012
  • 14
  • 126
  • 92