drkenap147751 2017-12-10 23:10
浏览 595
已采纳

无法在golang中打开SQLite数据库:“无法打开数据库文件[已恢复]”-错误

I am just starting learning golang and am not sure if my error is conceptual or with the language.

It is peculiar, because the error only occurs, when unit-testing my code. If I "go run" everything works fine. As a sqlite-driver I use mattn/go-sqlite3.

Here is where the problem occurs:

    func dbExec(command *string) {
    db, err := sql.Open("sqlite3", dbPath) // Path and driver are set correcrtly
    defer db.Close()
    if err != nil { //No problem here
        panic(err)
    }
    _, err = db.Exec(*command)
    if err != nil { //Here the errorNo14 "Unable to open the database file" occurs
        panic(err)
    }
 }

So it seems to me, that the database file can be found, but it can't be open because of other limits. I have no concurrency in all of my code. I have only one test-method so far, so even if the testing would be done concurrently, it would so far be only one thread. Maybe someone has an idea! The problem seems to basic, there really is not much more to my code then this.

  • 写回答

1条回答 默认 最新

  • doushang9172 2017-12-10 23:26
    关注

    sql.Open usually does not perform any database operations, it merely initializes the driver (see sql.Open docs for details). The driver's Open() method is called in the background.

    This means that the first error is usually encountered on trying to perform an operation, in your case: the db.Exec.

    As for the actual error: if this is in tests it is likely that your path is not correct (eg: if the test database is in the test's sub-directory, the test runner's current directory will be different, I think). Try to switch to absolute paths (or paths computed at runtime).

    And a small nit: move defer db.Close() after the first error check.

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

报告相同问题?

悬赏问题

  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,