duanhongxian6982 2018-06-23 05:46
浏览 533
已采纳

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

I am new to go and backend development. I am trying to use a MySQL database with go to create a 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)
}

I get this error:

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

I am not able to understand the issue.

It works fine when there is no DB query.

EDIT 1: editted sql.Open to change closing braces to root:mypassword@tcp(127.0.0.1:3306)/test works fine when querying

but i get panic while inserting in db

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()
}

this is the error 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条回答 默认 最新

  • dongren1986 2018-06-23 06:14
    关注

    The DSN (i.e. second) argument to the sql.Open() method is malformed.

    Only the address portion (127.0.0.1:3306) is supposed to be surrounded by parentheses. You've included the database name within the parentheses as well.

    Try this instead:

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

    The documented format is:

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

    In response to "EDIT 1"

    err.Error() is a method.

    In your error handling block for the insert query, you're not calling the method. Instead you're printing out it's memory location.

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

    If you actually call the method (similar to how you have in the error handling block for the sql.Open()), you'll print out the error message which can point you towards what's wrong.

    if err != nil {
        panic(err.Error())
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行