需要使用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/

qt实现-给SQLITE添加自定义函数(对某个字段进行加密)的更多相关文章

  1. qt实现-给SQLITE添加自定义函数

    需要使用sqlite里的password对某个字段进行加密,由于使用的sqlite是由QT封装好的QSqlDatabase,没有发现加载扩展函数的方法,所以自己实现了一个. 在网上也没找到相应的参考, ...

  2. KnockoutJS 3.X API 第七章 其他技术(6) 使用“fn”添加自定义函数

    有时,您可能会通过向Knockout的核心值类型添加新功能来寻找机会来简化您的代码. 您可以在以下任何类型中定义自定义函数: 因为继承,如果你附加一个函数到ko.subscribable,它将可用于所 ...

  3. delphi中formatFloat代码初探(在qt下实现floatformat的函数)

    由于项目需要,需要在qt下实现floatformat的函数.之前写过一个,但是写得不好.决定重新写一个,参考delphi xe2下的实现.把xe2下的相关代码都看了一遍,xe2的代码思路在这里贴出来. ...

  4. 关于QT中evaluateJavaScript()函数返回值的处理问题

    关于QT中evaluateJavaScript()函数返回值的处理问题 - 寒风问雪的专栏 - 博客频道 - CSDN.NET 关于QT中evaluateJavaScript()函数返回值的处理问题 ...

  5. Hadoop3集群搭建之——hive添加自定义函数UDTF (一行输入,多行输出)

    上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hadoop3集群搭建之——配置ntp服务 Hadoop3集群搭建之——hive安装 Hadoo ...

  6. Hadoop3集群搭建之——hive添加自定义函数UDTF

    上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hadoop3集群搭建之——配置ntp服务 Hadoop3集群搭建之——hive安装 Hadoo ...

  7. Hadoop3集群搭建之——hive添加自定义函数UDF

    上篇: Hadoop3集群搭建之——虚拟机安装 Hadoop3集群搭建之——安装hadoop,配置环境 Hadoop3集群搭建之——配置ntp服务 Hadoop3集群搭建之——hive安装 Hadoo ...

  8. dede添加自定义函数

    在dede安装目录下的include/extend.func.php添加自定义函数: /** * 获取文章第一张图片 */ function getFirstImg($arcId) { global ...

  9. Sqlite 常用函数推荐

    Sqlite 常用函数 1 .打开数据库: 说明:打开一个数据库,文件名不一定要存在,如果此文件不存在, sqlite 会自动创建.第一个参数指文件名,第二个参数则是定义的 sqlite3 ** 结构 ...

随机推荐

  1. TensorFlow 下 mnist 数据集的操作及可视化

    from tensorflow.examples.tutorials.mnist import input_data 首先需要连网下载数据集: mnsit = input_data.read_data ...

  2. View的绘制顺序

    1.写在 super.onDraw() 的下面 把绘制代码写在 super.onDraw() 的下面,由于绘制代码会在原有内容绘制结束之后才执行,所以绘制内容就会盖住控件原来的内容. 2.写在 sup ...

  3. 【records】10.24..10.30

    做的题越来越少了; 我是不是该学下网络流.

  4. Notepad++中删除连续的任意n行

    使用Notepad++里的行标记功能,可以删除指定的任意n行. 案例1,删除sample2.dat里的第201行到第10000行.方法如下: (1) 用户NotePad++打开sample2.dat, ...

  5. Android菜鸟的成长笔记(19)——Service的生命周期

    前面两篇文章介绍了关于Service的两种启动方式,简要总结如下: Context.startService() Context.bindService() 1. startService()的目的是 ...

  6. 同一性(identical)

    f(x)=x,表明 f(⋅) 为同一函数. A 与 B 具有 360° 的区别 A 向左转,再向右转 ⇒ A A 向左转,向左转,向后转 ⇒ A

  7. JSP页面中taglib的uri设置

    今天遇到这样一个问题,使用JAVA做了个WEB应用,其中用到一个自定义标签.该标签的class文件与tld(tld文件中,uri定义为:http://wallimn.iteye.com/myfuncs ...

  8. CUDA—使用GPU暴力破解密码

    GPU支持大规模的并行加速运算,胜在量上,CPU处理大量的并行运算显得力不从心,它是胜在逻辑上.利用显卡加速的应用越来越多,但如果说GPU即将或最终将替代CPU还有点言过其实,二者最终将优势互补,各尽 ...

  9. uwp - 解决使用EntityFramework时报错“unable to load dll 'sqlite3':the specified module could not be found”

    在使用uwp的ef过程中碰到一万个问题快折腾死我了,好在最后终于解决掉所有问题,但愿如此,因为在这之前先后发生不同的报错,不知道后面还会碰到新的问题不. 其中一个问题是这样的,生成能正常生成,但是启动 ...

  10. Mybatis 一对多 配置文件

    当一个Entity中包含的属性有对象和对象集合时,用mybatis映射时要在Entity中添加一个字段来唯一标识当前的Entity对象.否则查询的Entity集合中的对象会被覆盖掉. 如下一个POJO ...