dongyi1748 2018-08-29 12:49
浏览 105

从虚拟文件系统加载SQLite3数据库(afero)

Setting:

1) SQLite3 database file gets uploaded to a fileserver

2) Files get stored into virtual filesystem

3) I want to execute sql queries on that database

Currently I am using go-sqlite3 with a database file located in afero.


Question:

Is there are possbility to load a database file into go-sqlite3 with another source than a file from the os filesystem.


Current approach:

My current solution is to copy the file from afero into the os.TempDir() directory, but that fails with our ci and is no actual solution as it does not use the dedicated filesystem anymore.

  • 写回答

1条回答 默认 最新

  • doufuxing8691 2018-08-29 13:26
    关注

    After a little source diving I see this in sqlite3.go

    name := C.CString(dsn)
    defer C.free(unsafe.Pointer(name))
    rv := C._sqlite3_open_v2(name, &db,
        mutex|C.SQLITE_OPEN_READWRITE|C.SQLITE_OPEN_CREATE,
         nil)
    

    So this answers your question, the actual opening of the database happens "outside" Go and "inside" the sqlite3 library. If we check the function sqlite3_open_v2 we can see that it requires a file name:

    int sqlite3_open_v2(
      const char *filename,   /* Database filename (UTF-8) */
      sqlite3 **ppDb,         /* OUT: SQLite db handle */
      int flags,              /* Flags */
      const char *zVfs        /* Name of VFS module to use */
    );
    

    I think the only way here is to implement your own Sqlite VFS that somehow interacts with the afero abstractions.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法