dongpiao8821 2014-10-04 02:57
浏览 39
已采纳

golang数据库开放函数歧义

In go, you invoke a database open with

DB, err = sql.Open("sqlite3", "./bar.db")

This returns a database of type *sql.DB, the function and type are both members of database/sql.

However, in the sqlite3 driver package, there is another Open function which returns a *sqlite3.Conn type.

I've noticed that some of the functions defined in the sqlite3 driver package do not work on a database opened using the default database/sql.

Additionally, there are functions in the sqlite3 package that are similar to database/sql's functions, namely Query and Exec, which return different types.

database/sql contains a func Query that returns type *sql.Rows.

mxk/sqlite/sqlite3 and mattn/go-sqlite3 both have a Query function, which return completely different types.

I want to run functions such as mxk/sqlite/sqlite3's BusyTimeout on my database connection, but it isn't the correct type. Do I have to re-write all of my code to open the database connection with the sqlite3 driver instead of using database/sql's Open? What's the advantage of database/sql's Open function if you can't use any of the driver's functions with that generic connection?

  • 写回答

1条回答 默认 最新

  • doushi6932 2014-10-07 15:32
    关注

    Well, as the docs state...

    Database connections are created either by using this package directly or with the "sqlite3" database/sql driver. The direct interface, which is described below, exposes SQLite-specific features, such as incremental I/O and online backups. The driver is recommended when your application has to support multiple database engines.

    ... you would use the sqlite3 connection when you want the sqlite specific functions (your usecase) and for generic database connections you would use the database driver (your code).

    That is perfectly logical, since how would a generic driver handle functions that are specific to various databases? Runtime errors?

    So you have a choice: all sqlite functions -> sqlite conn or handle multiple sql databases -> generic driver.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能