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.

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

报告相同问题?

悬赏问题

  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决