Skip to content

no use default database in dsn #825

Closed
@chyroc

Description

@chyroc

Issue description

Tell us what should happen and what happens instead

Example code

package main

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
)

func getCurrentDatabase(db *sql.DB) string {
	var name sql.NullString
	if err := db.QueryRow(`SELECT DATABASE();`).Scan(&name); err != nil {
		panic(err)
	}

	if name.Valid {
		return name.String
	}
	return ""
}

func main() {
	db, err := sql.Open("mysql", "root:@tcp(127.0.0.1:3306)/account?charset=utf8mb4,utf8&parseTime=True&loc=UTC")
	if err != nil {
		panic(err)
	}

	fmt.Printf("database name: %s\n", getCurrentDatabase(db))

	if _, err = db.Exec("use account;"); err != nil {
		panic(err)
	}

	fmt.Printf("database name: %s\n", getCurrentDatabase(db))
}

Configuration

Driver version (or git SHA):
master: 749ddf1

Go version: run go version in your console
go version go1.10.3 darwin/amd64

Server version: E.g. MySQL 5.6, MariaDB 10.0.20
mysql Ver 8.0.11 for osx10.13 on x86_64 (Homebrew)

Server OS: E.g. Debian 8.1 (Jessie), Windows 10
macosx

above code print

database name: 
database name: account

but need:

database name: account
database name: account

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions