在Golang中,在函数内引用一个打开的数据库连接。

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

Referring to an open database connection inside a function - Golang

问题

我的主要函数打开了一个数据库连接:

func main() {
    db, err := sql.Open("sqlite3", "./house.db")
    checkErr(err)
    
    ...
}

然后,我想创建一个函数,允许我根据传递的结构体向数据库添加一行:

func addRow(row Room) error {
    stmt, err := db.Prepare("INSERT INTO Rooms (Name, Size, WindowCount, WallDecorationType, Floor) VALUES(?, ?, ?, ?, ?)")
    _, err = stmt.Exec(row.Name , row.Size , row.WindowCount , row.WallDecorationType , row.Floor)
    return err
}

但显然我不能这样做,因为addRow()函数不知道db是什么。

我该如何使这个函数工作?也许我应该在主函数之外打开数据库?

英文:

My main function opens a database connection:

func main() {
	db, err := sql.Open("sqlite3", "./house.db")
	checkErr(err)
    
    ...
}

Then, I want to create a function that allows me to add a row to the database based on a passed struct:

func addRow(row Room) error {
	stmt, err := db.Prepare("INSERT INTO Rooms (Name, Size, WindowCount, WallDecorationType, Floor) VALUES(?, ?, ?, ?, ?)")
	_, err = stmt.Exec(row.Name , row.Size , row.WindowCount , row.WallDecorationType , row.Floor)
	return err
}

But obviously I can't do that because the addRow() function has no idea what db is.

How would I make this function work? Should I perhaps, open the database outside of the main function?

答案1

得分: 18

根据你的应用程序工作方式的不同,你可以选择以下几种方式:

  1. db 保持为 全局变量
  2. db 作为 参数 传递
  3. addRoom 定义为一个 方法

对于 API 服务,我通常会创建一个 全局db,像这样:

var db *sql.DB

func main() {
    var err error
    db, err = sql.Open("sqlite3", "./house.db")
    checkErr(err)
    // 创建房间 Room{}
    err = addRoom(room)
    checkErr(err)
}

但你也可以将 db 作为 参数 传递:

func addRow(db *sql.DB, row Room) error

或者你可以创建一个包含连接属性的 结构体,并将 addRow 定义为一个方法:

type dbConn struct {
    db *sql.DB
}

func (conn dbConn) addRow(row Room) error

这本书籍有一些很好的示例。

英文:

Depending on how your application works, you can either

  1. keep the db global
  2. pass db as a parameter
  3. make addRoom a method

What I typically do for API services is create a global db, like this:

var db *sql.DB

func main() {
    var err error
    db, err = sql.Open("sqlite3", "./house.db")
    checkErr(err)
    // create room Room{}
    err = addRoom(room)
    checkErr(err)
}

But you can also pass db as a parameter:

func addRow(db *sql.DB, row Room) error

Or you can create a struct which keeps the connection as an attribute and makes addRow a method:

type dbConn struct {
    db *sql.DB
}

func (conn dbConn) addRow(row Room) error

This book has some nice examples.

huangapple
  • 本文由 发表于 2017年3月3日 22:40:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/42581697.html
匿名

发表评论

匿名网友

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

确定