SqlSugar 连接 SQLite 问题
使用 SqlSugar 连接 SQLite 数据库时,本地调试正常,发布到 CentOS 7.9.2009 服务器上会报如下错误:
fail: System.Logging.FriendlyException[0]
中文提示 : 连接数据库过程中发生错误,检查服务器是否正常连接字符串是否正确,错误信息:The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception.
Exception has been thrown by the target of an invocation..
English Message : Connection open error . The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception.
Exception has been thrown by the target of an invocation.
SqlSugar.SqlSugarException: 中文提示 : 连接数据库过程中发生错误,检查服务器是否正常连接字符串是否正确,错误信息:The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception
出现原因可能是由于旧版本的 sqlite 存在问题,所以需要升级 sqlite 版本。
- wget 下载
> wget https://www.sqlite.org/2024/sqlite-autoconf-3470100.tar.gz
- 解压
> tar -zxvf sqlite-autoconf-3470100.tar.gz -C /opt/
- 进入解压目录
> cd /opt/sqlite-autoconf-3470100
- 检查安装环境
> ./configure --prefix=/usr/local/sqlite
此处如果出现验证不通过,可以尝试使用 yum 安装相关依赖,如 yum install -y gcc 等。
- 编译安装
> make && make install
安装完后可以看见如下提示
这段内容显示了 sqlite3 的安装路径:/usr/local/sqlite3/lib。
特别注意 add LIBDIR to the 'LD_LIBRARY_PATH' environment variable,这是 sqlite 建议添加环境变量。
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the 'LD_RUN_PATH' environment variable
during linking
- use the '-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to '/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
/usr/bin/mkdir -p '/usr/local/sqlite3/bin'
/bin/sh ./libtool --mode=install /usr/bin/install -c sqlite3 '/usr/local/sqlite3/bin'
libtool: install: /usr/bin/install -c sqlite3 /usr/local/sqlite3/bin/sqlite3
/usr/bin/mkdir -p '/usr/local/sqlite3/include'
/usr/bin/install -c -m 644 sqlite3.h sqlite3ext.h '/usr/local/sqlite3/include'
/usr/bin/mkdir -p '/usr/local/sqlite3/share/man/man1'
/usr/bin/install -c -m 644 sqlite3.1 '/usr/local/sqlite3/share/man/man1'
/usr/bin/mkdir -p '/usr/local/sqlite3/lib/pkgconfig'
/usr/bin/install -c -m 644 sqlite3.pc '/usr/local/sqlite3/lib/pkgconfig'
make[1]: 离开目录“/root/sqlite-autoconf-3470100”
- 添加环境变量,并重载配置文件
> echo "LD_LIBRARY_PATH=/usr/local/sqlite3/lib" >> /etc/profile
> source /etc/profile
- 验证安装
> sqlite3
SQLite version 3.47.1 2024-11-25 12:07:48
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .exit
> ls -l /usr/local/sqlite3/lib/*sqlite*
> ls -l /usr/local/sqlite3/include/*sqlite*
- 替换旧版本的 sqlite
# 把旧的sqlite3改个名字
> mv /usr/bin/sqlite3 /usr/bin/sqlite3_old
# 设置软链接
> ln -s /usr/local/sqlite3/bin/sqlite3 /usr/bin/sqlite3
> echo "/usr/local/sqlite3/lib" > /etc/ld.so.conf.d/sqlite3.conf
# 动态链接库管理命令
> ldconfig
- 验证版本
> sqlite3 --version
3.47.1 2024-11-25 12:07:48 b95d11e958643b969c47a8e5857f3793b9e69700b8f1469371386369a26e577e (64-bit)
支持完成 sqlite 的安装。
最终还需要将 libsqlite3.so 与 项目进行链接,以支持 SqlSugar 的使用。
在此之前需要检查下项目内是否已经存在 libe_sqlite3.so 文件,如果已经存在,则需要删除。
> rm -rf /xx/xx/libe_sqlite3.so
> ls -s /usr/local/sqlite3/lib/libsqlite3.so /xx/xx/libe_sqlite3.so
其中 /xx/xx/ 为项目路径。
操作完成后需要重新启动项目,即可使用 SqlSugar 连接 SQLite 数据库。