如何编译sqlite3扩展-CSV虚拟表

Vij*_*y B 1 csv sqlite

我正在尝试使用sqlite3 的CSV 虚拟表扩展。我在 Mac (MacOS High Sierra 10.13.6) 上编译扩展的第一步遇到了困难。

我从此页面下载了 csv.c 的源代码。我还从这里获取了 sqlite 源代码合并。

我使用以下命令进行编译:

gcc -g -fPIC -dynamiclib csv.c -o csv.dylib
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误:

csv.c:115:3: error: no member named '__builtin___vsnprintf_chk' in 'struct sqlite3_api_routines'
  sqlite3_vsnprintf(CSV_MXERR, p->zErr, zFormat, ap);
  ^~~~~~~~~~~~~~~~~
/usr/include/sqlite3ext.h:437:53: note: expanded from macro 'sqlite3_vsnprintf'
#define sqlite3_vsnprintf              sqlite3_api->vsnprintf
                                       ~~~~~~~~~~~  ^
/usr/include/secure/_stdio.h:75:3: note: expanded from macro 'vsnprintf'
  __builtin___vsnprintf_chk (str, len, 0, __darwin_obsz(str), format, ap)
  ^
csv.c:115:21: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'const void *' [-Wint-conversion]
  sqlite3_vsnprintf(CSV_MXERR, p->zErr, zFormat, ap);
                    ^~~~~~~~~
csv.c:67:19: note: expanded from macro 'CSV_MXERR'
#define CSV_MXERR 200
                  ^~~
/usr/include/secure/_stdio.h:75:57: note: expanded from macro 'vsnprintf'
  __builtin___vsnprintf_chk (str, len, 0, __darwin_obsz(str), format, ap)
                                                        ^~~
/usr/include/secure/_common.h:39:54: note: expanded from macro '__darwin_obsz'
#define __darwin_obsz(object) __builtin_object_size (object, _USE_FORTIFY_LEVEL > 1 ? 1 : 0)
                                                     ^~~~~~
csv.c:568:5: error: use of undeclared identifier 'sqlite3_str'
    sqlite3_str *pStr = sqlite3_str_new(0);
    ^
csv.c:568:18: error: use of undeclared identifier 'pStr'
    sqlite3_str *pStr = sqlite3_str_new(0);
                 ^
csv.c:568:25: warning: implicit declaration of function 'sqlite3_str_new' is invalid in C99 [-Wimplicit-function-declaration]
    sqlite3_str *pStr = sqlite3_str_new(0);
                        ^
csv.c:571:5: warning: implicit declaration of function 'sqlite3_str_appendf' is invalid in C99 [-Wimplicit-function-declaration]
    sqlite3_str_appendf(pStr, "CREATE TABLE x(");
    ^
csv.c:571:25: error: use of undeclared identifier 'pStr'
    sqlite3_str_appendf(pStr, "CREATE TABLE x(");
                        ^
csv.c:581:29: error: use of undeclared identifier 'pStr'
        sqlite3_str_appendf(pStr, "%sc%d TEXT", zSep, iCol);
                            ^
csv.c:588:31: error: use of undeclared identifier 'pStr'
          sqlite3_str_appendf(pStr,"%s\"%w\" TEXT", zSep, z);
                              ^
csv.c:597:31: error: use of undeclared identifier 'pStr'
          sqlite3_str_appendf(pStr,"%sc%d TEXT", zSep, ++iCol);
                              ^
csv.c:603:25: error: use of undeclared identifier 'pStr'
    sqlite3_str_appendf(pStr, ")");
                        ^
csv.c:604:18: warning: implicit declaration of function 'sqlite3_str_finish' is invalid in C99 [-Wimplicit-function-declaration]
    CSV_SCHEMA = sqlite3_str_finish(pStr);
                 ^
csv.c:604:37: error: use of undeclared identifier 'pStr'
    CSV_SCHEMA = sqlite3_str_finish(pStr);
                                    ^
csv.c:643:27: error: use of undeclared identifier 'SQLITE_VTAB_DIRECTONLY'
  sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
                          ^
4 warnings and 10 errors generated.

Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Ant*_*nko 7

sqlite.org 上的 Richard Hipp发布了一个有关如何编译 SQLite 扩展的解决方案。

编译 CSV 扩展的脚本如下所示(基于https://www.sqlite.org/loadext.htmlhttps://github.com/sqlite/sqlite/blob/master/README.md):

wget https://www.sqlite.org/src/tarball/sqlite.tar.gz
tar xzf sqlite.tar.gz
mkdir bld
cd bld
../sqlite/configure
make
gcc -g -I. -fPIC -dynamiclib ../sqlite/ext/misc/csv.c -o csv.dylib
Run Code Online (Sandbox Code Playgroud)

测试脚本:

echo -e 'col_text,col_int\napples,3\noranges,5' > sample.csv
./sqlite3 '' '.load csv' 'CREATE VIRTUAL TABLE temp.t1 USING csv(filename="sample.csv");' 'SELECT * FROM t1;'
Run Code Online (Sandbox Code Playgroud)

就是这样。

您还可以尝试csv.c使用现有sqlite3安装进行编译。如果sqlite3使用 Homebrew 安装,则为:

curl -O https://raw.githubusercontent.com/sqlite/sqlite/master/ext/misc/csv.c
gcc -g -I/usr/local/opt/sqlite/include -fPIC -dynamiclib csv.c -o csv.dylib
/usr/local/opt/sqlite/bin/sqlite3 '' '.load csv'
Run Code Online (Sandbox Code Playgroud)

最后一行是在 Homebrew 中测试新编译的库sqlite3。默认值$ sqlite3来自 MacOS 捆绑包,有时已经过时。