英文:
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
根据你的应用程序工作方式的不同,你可以选择以下几种方式:
- 将
db
保持为 全局变量 - 将
db
作为 参数 传递 - 将
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
- keep the
db
global - pass
db
as a parameter - 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论