使用sql.Open连接Go SQLite3数据库

huangapple go评论91阅读模式
英文:

Go SQLite3 database connection using sql.Open

问题

我是新手,正在学习使用golang,并且在连接本地主机上的SQLite3数据库方面遇到了困难。我已经创建了SQLite3数据库,并且按照一些教程进行了尝试,但是它们都没有起作用。目前我的代码是基于另一个帖子,但我仍然无法连接到我的数据库。我认为问题出在我对"sql.Open"的使用上,即使在sql包的帮助下,我仍然不清楚需要提供哪些信息。

代码可以正常构建,但报告了以下错误:

无法打开数据库文件
错误二触发

当我尝试运行下面的代码时:

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") //不清楚需要为用户和密码提供什么信息
    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
}

我没有为我使用的数据库设置用户名或密码,该数据库托管在本地机器上。我尝试了几种组合,包括使用计算机的用户名/密码以及在"sql.Open"中不使用用户名/密码,但问题仍然存在。

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

我应该如何连接到SQLite数据库?我在"sql.Open"命令中做错了什么?

英文:

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

<!-- begin snippet: js hide: false -->

<!-- language: lang-html -->

package main

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

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

}

<!-- end snippet -->

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?

答案1

得分: 3

如果没有用户名或密码,只需将完整路径(包括文件名)放入数据库中。

例如:

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

英文:

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(&quot;sqlite3&quot;, &quot;/user/home/workspace/myDBname.db&quot;)

huangapple
  • 本文由 发表于 2015年5月26日 00:31:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/30442449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定