使用sql.Open进入SQLite3数据库连接

我是golang的新手,我很难连接到本地计算机上托管的SQLite3数据库。 我已经创建了SQLite3数据库,并通过一些我喜欢的教程进行了工作,但是它们没有用。 目前,我的代码基于另一个 post ,但是我仍然无法与数据库建立连接。 我相信我的问题在于我使用“ sql.Open”,因为即使咨询 SQL程序包。</ p>

代码可以正常运行,但报告</ p>

  打开数据库文件
错误当我尝试运行下面的代码</ p>

</ p>

code> </ pre>

=“ snippet” data-lang =“ js” data-hide =“ false” data-console =“ false” data-babel =“ false”>

\ n
 包main  

import(
“ database / sql”

“ fmt” \ r
_“ github.com/go-sql-driver/mysql"
_” github.com/mattn/go-sqlite3“

func main(){

db,err:= sql.Open(“ sqlite3”,“ myuser:mypassword @ / myDBname”)//不清楚用户和密码的要求
如果是err! = nil {
fmt.Println(err)
fmt.Println(“错误一跳 “)
return

}

延迟db.Close()
err = db.Ping()
如果err!= nil {
fmt.Println (err)
fmt.Println(“错误两次跳闸”)
返回
}

fmt.Println(“ Ping”)

返回

} </ code> </ pre>

</ div>

</ div>

我尚未设置用户名或密码 我正在使用的数据库,该数据库托管在本地计算机上。 我在“ sql.Open”中尝试了几种计算机用户名/密码的组合,但是没有用户名/密码,但是仍然遇到相同的问题。</ p>

我已经安装并导入了程序包code.google。 com / p / go-sqlite / go1 / sqlite3
,并且我的数据库与我的Go代码位于同一文件夹中。 </ p>

如何使用与SQLite数据库的连接? sql.Open命令在做什么?</ p>
</ div>

展开原文

原文

I am new to golang and I am a having a hard time connecting to an SQLite3 database hosted on the local machine. I have the SQLite3 database created and have worked through a few tutorials I have fond but they are not working. currently my code is based on another post but I am still unable to make a connection with my database. I believe my problem is in my use of "sql.Open" as I am not clear on the information that I need to provide even after consulting the sql package.

the code builds fine but reports

unable to open database file
error Two tripped

when I try to run the code below

package main

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

func main() {
       
    db, err := sql.Open("sqlite3", "myuser:mypassword@/myDBname") //not clear on what is needed for the user and password
    if err != nil {
        fmt.Println(err)
        fmt.Println("error one tripped")
        return
    }
    defer db.Close()
    err = db.Ping()
    if err != nil {
        fmt.Println(err)
        fmt.Println("error Two tripped")
        return
    }
    fmt.Println("Ping")
    
    return

}

I have not set a username or password for the database I am using, which is hosted on the local machine. I tried several combinations of my computer username/password and no username/password in "sql.Open" but I still have the same problem.

I have installed and imported package code.google.com/p/go-sqlite/go1/sqlite3 and my DB is in the same folder as my Go code.

How do I use make the connection to the SQLite Database? what am I doing wrong with the sql.Open command?

</div>

1个回答



对于没有用户名或密码的情况,只需将包含文件名的完整路径放入数据库。 </ p>

ex:</ p>

db,err:= sql.Open(“ sqlite3”,“ / user / home / workspace / myDBname。 db“)</ code> </ p>
</ div>

展开原文

原文

For the case when there is no username or password simply put in the full path to the DB including the file name.

ex:

db, err := sql.Open("sqlite3", "/user/home/workspace/myDBname.db")

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问