代码之家  ›  专栏  ›  技术社区  ›  Tushar Saha

无效的DSN:网络地址未终止(缺少右大括号)

  •  -1
  • Tushar Saha  · 技术社区  · 8 年前

    我是新去和后端开发。我正在尝试使用mysql数据库和go创建rest api。

    func getUsers(w http.ResponseWriter, r *http.Request) {
        db, err := sql.Open("mysql", "root:mypassword@tcp(127.0.0.1:3306/test)")
        if err != nil {
            panic(err.Error())
        }
        results, err := db.Query("Select * from users")
        if err != nil {
            panic(err.Error())
        }
        for results.Next() {
            var user User
            err = results.Scan(&user.FirstName)
            if err != nil {
                panic(err.Error)
            }
            fmt.Println(user.FirstName)
        }
        //scores is an array that i have already created just to return dummy data
        json.NewEncoder(w).Encode(scores)
    }
    

    我得到这个错误:

    http: panic serving [::1]:54508: invalid DSN: network address not terminated (missing closing brace)
    goroutine 5 [running]:
    net/http.(*conn).serve.func1(0xc42009abe0)
            /usr/local/go/src/net/http/server.go:1726 +0xd0
    panic(0x129cea0, 0xc420010ec0)
            /usr/local/go/src/runtime/panic.go:505 +0x229
    main.getUsers(0x134bca0, 0xc42011e000, 0xc42011c200)
            /Users/tushar/go/src/github.com/tushar/jump/main.go:62 +0x33f
    net/http.HandlerFunc.ServeHTTP(0x1326230, 0x134bca0, 0xc42011e000, 0xc42011c200)
            /usr/local/go/src/net/http/server.go:1947 +0x44
    github.com/gorilla/mux.(*Router).ServeHTTP(0xc420110000, 0x134bca0, 0xc42011e000, 0xc42011c200)
            /Users/tushar/go/src/github.com/gorilla/mux/mux.go:162 +0xed
    net/http.serverHandler.ServeHTTP(0xc42008aea0, 0x134bca0, 0xc42011e000, 0xc42011c000)
            /usr/local/go/src/net/http/server.go:2694 +0xbc
    net/http.(*conn).serve(0xc42009abe0, 0x134bf60, 0xc420062240)
            /usr/local/go/src/net/http/server.go:1830 +0x651
    created by net/http.(*Server).Serve
            /usr/local/go/src/net/http/server.go:2795 +0x27b
    

    我不能理解这个问题。

    当没有数据库查询时,它可以正常工作。

    编辑1: 已编辑的SQL。打开可将右大括号更改为 root:mypassword@tcp(127.0.0.1:3306)/test 查询时工作正常

    但我在数据库中插入时会感到恐慌

    func insertUsers(w http.ResponseWriter, r *http.Request) {
        db, err := sql.Open("mysql", "root:mypassword@tcp(127.0.0.1:3306)/test")
        if err != nil {
            panic(err.Error())
        }
        insert, err := db.Query("Insert into users (personId,firstName,lastName) values(20,tushar,saha)")
        if err != nil {
            panic(err.Error)
        }
        defer insert.Close()
    }
    

    这是错误1

    2018/06/23 11:54:10 http: panic serving [::1]:54802: 0x126a8b0
    goroutine 19 [running]:
    net/http.(*conn).serve.func1(0xc4200aebe0)
            /usr/local/go/src/net/http/server.go:1726 +0xd0
    panic(0x129bb20, 0xc42016a020)
            /usr/local/go/src/runtime/panic.go:505 +0x229
    main.insertUsers(0x134bc80, 0xc42013c000, 0xc420138200)
            /Users/tushar/go/src/github.com/tushar/jump/main.go:50 +0x15d
    net/http.HandlerFunc.ServeHTTP(0x1326218, 0x134bc80, 0xc42013c000, 0xc420138200)
            /usr/local/go/src/net/http/server.go:1947 +0x44
    github.com/gorilla/mux.(*Router).ServeHTTP(0xc42012c000, 0x134bc80, 0xc42013c000, 0xc420138200)
            /Users/tushar/go/src/github.com/gorilla/mux/mux.go:162 +0xed
    net/http.serverHandler.ServeHTTP(0xc420095040, 0x134bc80, 0xc42013c000, 0xc420138000)
            /usr/local/go/src/net/http/server.go:2694 +0xbc
    net/http.(*conn).serve(0xc4200aebe0, 0x134bf40, 0xc42009a200)
            /usr/local/go/src/net/http/server.go:1830 +0x651
    created by net/http.(*Server).Serve
            /usr/local/go/src/net/http/server.go:2795 +0x27b
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   chuckx    8 年前

    DSN(即第二个)参数 sql.Open() 方法格式不正确。

    只有地址部分( 127.0.0.1:3306 )应该被括号包围。您还将数据库名称包含在括号中。

    请改为:

    db, err := sql.Open("mysql", "root:mypassword@tcp(127.0.0.1:3306)/test")
    

    这个 documented 格式为:

    [username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]
    

    回应“编辑1”

    err.Error() is a method.

    在插入查询的错误处理块中,没有调用该方法。相反,你正在打印它的内存位置。

    if err != nil {
        panic(err.Error)
    }
    

    如果您实际调用该方法(类似于 sql.open() ,您将打印出错误消息,该消息可以指出您的错误所在。

    if err != nil {
        panic(err.Error())
    }