需要使用sqlite里的password对某个字段进行加密,由于使用的sqlite是由QT封装好的QSqlDatabase,没有发现加载扩展函数的方法,所以自己实现了一个。

在网上也没找到相应的参考,就自己查官方文档解决了。本篇文章主要是sqlite如何加载外部的函数,并没有password函数的实现,我将写好的函数生成了一个动态库,由程序动态加载。

#include <iostream>
#include <QString>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
#include <QtSql/QSqlDriver>
#include <QVariant>
#include <sqlite3.h>
#include <string.h>
using namespace std; void insert_database(QSqlDatabase& database,QString name)
{
QSqlQuery query(database);
if(!query.exec("insert into data(name) values(password('"+name+"') )"))
cout<<query.lastError().text().toStdString()<<std::endl;
else
database.commit();
} int main()
{
QSqlDatabase database=QSqlDatabase::addDatabase("QSQLITE");
database.setDatabaseName("data.db");
if(!database.open())
{
cout<<"open database failure"<<std::endl;
return 0;
}
QVariant handle=database.driver()->handle();
if(!handle.isValid())
{
cout<<"handle not valid"<<endl;
return 0;
}
sqlite3* sqlhandle=*static_cast<sqlite3**>(handle.data()); char * error=(char*)sqlite3_malloc(1024);
sqlite3_enable_load_extension(sqlhandle,1);
if(SQLITE_OK==sqlite3_load_extension(sqlhandle,"/home/quanwei/desktop/my-documents/code/qt/loadsqlitefunction/password.so",0,&error));
else
cout<<"error: "<<error<<std::endl;
sqlite3_free(error);
insert_database(database,"hello");
database.close();
return 0;
}

数据库结构也放出来参考

CREATE TABLE data(id integer primary key,name text);

此程序用到的库的支持:
QtCore,QtSql,sqlite3

具体接口官方文档有说明

Loading An Extension An SQLite extension is a shared library or DLL. To load it, you need to supply SQLite with the name of the file containing the shared library or DLL and an entry point to initialize the extension. In C code, this information is supplied using the sqlite3_load_extension() API. See the documentation on that routine for additional information. Note that different operating systems use different filename suffixes for their shared libraries. Windows use ".dll", Mac uses ".dylib", and most unixes other than mac use ".so". If you want to make your code portable, you can omit the suffix from the shared library filename and the appropriate suffix will be added automatically by the sqlite3_load_extension() interface.

不过由于默认load_extension是处于关闭状态,所以需要调用sqlite3_enable_load_extension打开扩展功能

在sqlite3的shell里可以通过执行

sqlite3> .load ./password

来加载一个动态库函数,但是由于扩展函数没有储存在数据库中,所以每次打开这个数据库中都要加载一次才能使用自定义函数。

参考:https://www.sqlite.org/loadext.html

http://www.quweiji.com/qt%E5%AE%9E%E7%8E%B0-%E7%BB%99sqlite%E6%B7%BB%E5%8A%A0%E8%87%AA%E5%AE%9A%E4%B9%89%E5%87%BD%E6%95%B0/

最新文章

  1. 专用服务器模式&amp;共享服务器模式
  2. 深入.NET平台的软件系统分成开发(1/6)
  3. $(window).height()获取浏览器高度不准
  4. IOS Quartz2D 通过UIColor生成图片
  5. Zabbix探索:工作时间的设置
  6. python笔记之bisect模块
  7. linux 文件和目录操作
  8. HTML5 进阶系列:文件上传下载
  9. CSV导出大量数据
  10. window.onload,document.ready
  11. 这应该是目前最快速有效的ASP.NET Core学习方式(视频)
  12. 如何深度复制一个javascript对象
  13. python学习日记(内置函数补充)
  14. Java JSONArray for循环 remove成员的一个好算法
  15. 我是这样手写 Spring 的(麻雀虽小五脏俱全)
  16. Python Flask学习
  17. Bean的加载过程
  18. Xcode 7.3 cannot create __weak reference in file using manual reference counting
  19. Intel超低功耗CPU的一些信息
  20. Beta发布--PSP DAILY软件功能说明书2.0

热门文章

  1. flume 自己定义 hbase sink 类
  2. Open virtual effects in Ubuntu 12.04LTS
  3. 如何:打开 IIS 管理器
  4. ios开发学习笔记(1)
  5. video标签 拖动 转自w3school
  6. android的快速开发框架集合
  7. OpenCV 开发环境环境搭建(win10+vs2015+opencv 3.0)
  8. JAVA处理XML
  9. MSSQL:修改tempdb设置增加DW性能
  10. HDU 2087 剪花布条(模式串在主串中出现的次数主串中子串不可重叠)