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

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

时间: 2024-08-10 15:07:39

qt实现-给SQLITE添加自定义函数的相关文章

SQLite 常用函数

SQLite 有许多内置函数用于处理字符串或数字数据.下面列出了一些有用的 SQLite 内置函数,且所有函数都是大小写不敏感,这意味着您可以使用这些函数的小写形式或大写形式或混合形式.欲了解更多详情,请查看 SQLite 的官方文档: 序号 函数 & 描述 1 SQLite COUNT 函数SQLite COUNT 聚集函数是用来计算一个数据库表中的行数. 2 SQLite MAX 函数SQLite MAX 聚合函数允许我们选择某列的最大值. 3 SQLite MIN 函数SQLite MIN

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

由于项目需要,需要在qt下实现floatformat的函数.之前写过一个,但是写得不好.决定重新写一个,参考delphi xe2下的实现.把xe2下的相关代码都看了一遍,xe2的代码思路在这里贴出来.xe2下的代码在system.sysutils下,核心函数是InternalFloatToTextFmt.它有汇编的版本跟纯pascal的版本.汇编看不懂,参考的是纯pascal的版本. 整体流程是: 1)解析format格式化字符串 [delphi] view plain copy Result 

Sqlite 常用函数推荐

Sqlite 常用函数 1 .打开数据库: 说明:打开一个数据库,文件名不一定要存在,如果此文件不存在, sqlite 会自动创建.第一个参数指文件名,第二个参数则是定义的 sqlite3 ** 结构体指针(关键数据结构),这个结构底层细节如何,您不用管它. int sqlite3_open( const char *filename, /* Database filename (UTF-8) */ sqlite3 **ppDb /* OUT: SQLite db handle */ ); 返回

Qt信号槽中槽函数为虚函数的一些感想

有时候,在写connect的时候会去犹豫一个问题----我的槽函数到底需不需要为虚函数.这个问题在我每次写connect的时候我都会反问自己,因为确实,如果你不去深究里面的moc,你发现不了太多问题. 比如有这么一个demo. #include <QApplication> #include <QObject> #include <QDebug> class Test : public QObject { Q_OBJECT public: void onEmit() {

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

上篇: Hadoop3集群搭建之--虚拟机安装 Hadoop3集群搭建之--安装hadoop,配置环境 Hadoop3集群搭建之--配置ntp服务 Hadoop3集群搭建之--hive安装 Hadoop3集群搭建之--hbase安装及简单操作 Hadoop3集群搭建之--hive添加自定义函数UDF Hadoop3集群搭建之--hive添加自定义函数UDTF 上篇中,udtf函数,只有为一行输入,一行输出.udtf是可以一行输入,多行输出的. 简述下需求: 输入开始时间,结束时间,返回每个小时的

QT里使用sqlite的问题,好多坑

1. 我使用sqlite,开发机上好好的,测试机上却不行.后来发现是缺少驱动(Driver not loaded Driver not loaded),代码检查了又检查,发现应该是缺少dll文件(系统不提示,是自己使用 QMessageBox::warning(NULL, ("error"), database.lastError().text());后猜到的).于是自己千方百计的想办法,反复测试,就是不行.结果悲剧的发现,原来是多次设置路径错误,浪费一上午时间.应该直接在可执行exe

sqlite 时间函数及时间处理

SQLite分页显示:Select * From news order by id desc Limit 10 Offset 10这篇文章是根据 SQLite 官方 WIKI 里的内容翻译,如果有什么翻译不当的地方希望大家指出,毕竟我的英文水平实在很差. SQLite 包括以下五个时间函数: date(日期时间字符串, 修正符, 修正符, ……)time(日期时间字符串, 修正符, 修正符, ……)datetime(日期时间字符串, 修正符, 修正符, ……)julianday(日期时间字符串,

QT QObject::connect函数的学习

从Qobject(QObject.h)源码中可以看到QObject::connect的定义是这样的: [cpp] view plaincopy static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType = #ifdef qdoc Qt::AutoConnection #else #ifdef QT3_S

QT中的字符串处理函数

Fn 1 : arg 这个函数的具体声明不写了,它有20个重载,典型的示例代码如下: 1: #include <QtCore/QCoreApplication> 2: #include <iostream> 3: #include <stdio.h> 4: using namespace std; 5: 6: int main() 7: { 8: QString str = QString("Ggicci is %1 years old, and majors