最近需要通过qt操作一个加密过的sqlite数据库,按照qt帮助中:QSqlDatabase::setPassword( constQString&password),设置好密码后调用open函数。但是每次open函数均调用成功,可到了调用sql语句进行查询等操作时却老是报 QSqlError::ConnectionError,百思不得其解,最终在http://blog.csdn.net/babafall/article/details/3862342寻找到了答案。
以下均为转载:
sqlite数据库默认没有加密功能,对一些需要对工程文件进行保密的场合产生了不便,本文以QT4.4.3为例, 对qt源代码中的sql模块进行修改,为qt集成的sqlite数据库添加了加密功能.
1. /wxsqlite3_prj/sqlite3目录 下的文件进行编译生成 sqlite3.lib
2 将 sqlite3.h 和 上一步骤生成的sqlite3.lib 拷贝到目录 ?:/Qt/4.4.3/src/plugins/sqldrivers/sqlite 中.
3 将 ?:/Qt/4.4.3/src/plugins/sqldrivers/sqlite 目录下的sqlite.pro文件修改为:
TARGET = qsqlite # 不使用qt自带的sqilte源码文件,而是使用外部生成的库 win32:LIBS += sqlite3.lib # HEADERS= ../../../sql/drivers/sqlite/qsql_sqlite.h SOURCES= smain.cpp / ../../../sql/drivers/sqlite/qsql_sqlite.cpp !system-sqlite:!contains( LIBS,.*sqlite.* ) { CONFIG(release,debug|release):DEFINES *= NDEBUG DEFINES += SQLITE_CORE SQLITE_OMIT_LOAD_EXTENSION SQLITE_OMIT_COMPLETE # INCLUDEPATH += ../../../3rdparty/sqlite # SOURCES += ../../../3rdparty/sqlite/sqlite3.c } else { LIBS *= $$QT_LFLAGS_SQLITE QMAKE_CXXFLAGS *= $$QT_CFLAGS_SQLITE } include(../qsqldriverbase.pri) 4 修改 ?:/Qt/4.4.3/src/sql/drivers/sqlite 目录下的文件 qsql_sqlite.cpp 需要修改的函数 bool QSQLiteDriver::open(),函数体修改为: bool QSQLiteDriver::open(const QString & db,const QString &,int,const QString &conOpts) { if (isOpen()) close(); if (db.isEmpty()) return false; if (sqlite3_open16(db.constData(),&d->access) == SQLITE_OK) { sqlite3_busy_timeout(d->access,qGetSqliteTimeout(conOpts)); setOpen(true); setOpenError(false); //添加加密功能 "Trucc"为加密密匙,5为密匙长度 sqlite3_key( d->access,"Trucc",5); return true; } else { setLastError(qMakeError(d->access,tr("Error opening database"), QSqlError::ConnectionError)); setOpenError(true); return false; } }
5 编译 ?:/Qt/4.4.3/src/plugins/sqldrivers/sqlite 下的工程,在目录 ?:/Qt/4.4.3/plugins/sqldrivers 中生成添加加密功能的sqlite 相应库文件.
以上为摘抄大神的解决方法,同时贴出qt源码中QSQLiteDriver::open,进行比较
/* SQLite dbs have no user name,passwords,hosts or ports. just file names. */ bool QSQLiteDriver::open(const QString & db,const QString &conOpts) { if (isOpen()) close(); if (db.isEmpty()) return false; bool sharedCache = false; int openMode = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,timeOut=5000; QStringList opts=QString(conOpts).remove(QLatin1Char(' ')).split(QLatin1Char(';')); foreach(const QString &option,opts) { if (option.startsWith(QLatin1String("QSQLITE_BUSY_TIMEOUT="))) { bool ok; int nt = option.mid(21).toInt(&ok); if (ok) timeOut = nt; } if (option == QLatin1String("QSQLITE_OPEN_READONLY")) openMode = SQLITE_OPEN_READONLY; if (option == QLatin1String("QSQLITE_ENABLE_SHARED_CACHE")) sharedCache = true; } sqlite3_enable_shared_cache(sharedCache); if (sqlite3_open_v2(db.toUtf8().constData(),&d->access,openMode,NULL) == SQLITE_OK) { sqlite3_busy_timeout(d->access,timeOut); setOpen(true); setOpenError(false); return true; } else { setLastError(qMakeError(d->access, QSqlError::ConnectionError)); setOpenError(true); return false; } }
下面补充下sqlite.lib的生成方法:首先去官网http://www.sqlite.org/download.html下载相应的包,我下在的为sqlite-dll-win32-x86-3071300.zip,解压后可以看到sqlite3.def和sqlite3.dll两个文件。
我安装过vs2008,打开命令行工具Visual Studio 2008 Command Prompt,键入命令LIB /out:aaasqlite3.lib /MACHINE:IX86 /DEF:xxxsqlite3.def 即可。(注:aaa为用户给定路径,xxx位sqlite3.def所在路径)。后来,测试一直暴连接错误,之后用查看工具查看dll才发下,该dll并未导出sqlite3_key函数,谁有导出过该函数的sqlite下载,希望给出地址,谢谢。 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|