go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.91k stars 3.93k forks source link

dsn parse error #4845

Closed nymbian closed 2 years ago

nymbian commented 2 years ago

When use sqlserver with password "E3pl2021#@!" report error: failed to initialize database, got error parse "sqlserver://sa:E3pl2021": invalid port ":E3pl2021" after host

dsn := "sqlserver://sa:E3pl2021#@!@" + host + ":1433?database=" + database

github-actions[bot] commented 2 years ago

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

cute-angelia commented 2 years ago

!!! 怎么解决

yuangu commented 2 years ago

我也遇到了,好像密码里有#就会有问题

I have also encountered it. It seems that there will be problems if there is # in the password。And The common special point is that all problems occur in MSSQL

The following is an example code:

    host:="sqlserver://sa:E3pl2021#@!@:localhost:1433"
    db, err = gorm.Open(sqlserver.Open(host), &gorm.Config{

        })

    if err != nil {
        log.Fatalf("Dbhost: %v, initDB failed:%s", host, err)
        return err
    }

@cute-angelia 解决方法就是在这个链接外面再套一个"" 比如上面的例子就变成

host:=“\"sqlserver://sa:E3pl2021#@!@:localhost:1433\""

新测有效