dongqian9567 2019-02-14 05:00
浏览 512

使用事务进行sqlite查询?

I'm working on an assignment that has us using transactions using sqlite. This is the part I'm struggling with:

open the database and start a transaction. Call the function to read in all the zones with that transaction, then commit the transaction when it returns

Since I'm only querying the database and not inserting/updating, I don't think I need to do much in terms of setting up the transaction. This is what I'm working with

database, _ := sql.Open("sqlite3", "./world.db")
tx, _ := database.Begin()
rows, err := database.Query("SELECT id, name FROM zones")
if err != nil {
    tx.Rollback()
} else {
    defer tx.Commit()
}

However, since the transaction tx isn't being used in the query I have no clue how to pass it to a function, so I'm thinking my query is set up wrong. I would think I'd need to use the transaction in the query somehow, maybe something like tx.Query(), but I'm at a loss at the moment.

  • 写回答

2条回答 默认 最新

  • dongmin1166 2019-02-14 06:25
    关注

    You can simply use tx.Query. Review https://golang.org/pkg/database/sql/#Tx.Query.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题