Need to use password in SQLite to encrypt a field, because the use of SQLite is a qsqldatabase by the QT package, did not find the way to load the extension function, so you implement a.
On the internet did not find the corresponding reference, the official documents to check their own to solve. This article is mainly about how SQLite loads external functions, and there is no implementation of the password function, I will write a good function generated a dynamic library, the program dynamically loaded.
#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 (). Tostds Tring () <<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;}
Database structure is also put out for reference
CREATE TABLE data (ID integer primary key,name text);
Support for libraries used by this program:
Qtcore,qtsql,sqlite3
Specific interface official documentation is described
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 a entry point to Initialize the extension. In C code, this information is supplied using the sqlite3_load_extension () API. See the documentation on this 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 with ". So". If you want to make your code portable, you can omit the suffix from the shared library filename and the appropriate suffi X would be is added automatically by the sqlite3_load_extension () interface.
However, because the default load_extension is off, you need to call sqlite3_enable_load_extension to open the extension feature
In Sqlite3 's shell, you can execute
sqlite3>. Load./password
To load a dynamic library function, but since the extension function is not stored in the database, it is loaded once every time you open the database to use the custom function.
Reference: https://www.sqlite.org/loadext.html
QT implementation-Adding custom functions to SQLite