duankua3620 2017-08-19 23:20
浏览 90
已采纳

如何使sqlite出错而不是在golang中创建丢失的数据库文件?

I'm opening a connection to a sqlite database of the user's choosing in my golang program. If they supply a path to a non-existent file then I want to throw an error instead of creating the file.

I'm using the mattn/gp-sqlite3 driver. The documentation doesn't mention this capability as far as I can see.

This is the same as Make SQLite connection fail if database is missing? (deleted/moved) but for golang instead of C#. The golang driver seems to use a different format for describing connections to the C# driver so I don't know if the answers there translate to golang or not.

  • 写回答

1条回答 默认 最新

  • dtypj3308 2017-08-20 00:11
    关注

    This is not currently possible with the go-sqlite3 library. Looking at the source, you can see SQLITE_OPEN_CREATE is always passed to the open function:

    rv := C._sqlite3_open_v2(name, &db,
        C.SQLITE_OPEN_FULLMUTEX|
            C.SQLITE_OPEN_READWRITE|
            C.SQLITE_OPEN_CREATE,
        nil)
    

    I would suggest you open a ticket on the package to add such functionality.

    In the mean time, you could add the following code before your DB is created (keep in mind that a race condition exists, which is why you should still request this feature in the library itself):

    _, err := os.Stat("db.sqlite3")
    if os.IsNotExist(err) {
        panic("database doesn't exist")
    }
    // TODO: create db
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 pointnet2包安装
  • ¥20 射频功率问题,解答者有酬谢!
  • ¥80 构建降雨和积水的预测模型
  • ¥15 #Qt Transform setTransform()在鼠标拖动移动视角是一致在原地不动,无法变换视角(细微观察似乎视图有在原地抖动),无法变换视角(细微观察似乎视图有在原地抖动)
  • ¥50 如何利用无人机拍摄的数码照片测量鸟卵的长短径
  • ¥100 github贡献者给与奖励
  • ¥15 使用DS18B20+ESP8266获取温度数据返回-127.00
  • ¥15 odbc代码新增sqlserver数据源
  • ¥15 求一个USB指纹识别的驱动
  • ¥15 wps中工作表与工作表之间怎么汇总信息?