douzhongju8780
2015-05-25 16:31
浏览 142

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

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>

图片转代码服务由CSDN问答提供 功能建议

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

代码可以正常运行,但报告

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

=“ 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”)
 
 
 
返回
  
 
 
}   
 
  
 
  
 
 
 
 

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

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

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douhui3760 2015-05-28 12:56
    已采纳

    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")

    点赞 打赏 评论

相关推荐 更多相似问题