【php精品源码栏目提醒】:网学会员为广大网友收集整理了,QT下连接SQLite全过程详细记录 - 其它资料,希望对大家有所帮助!
QT 下连接 SQLite 全过程详细记录 下文介绍的内容都是基于 Linux RedHat 9.0 平台的。
一、sqlite-3.3.8 编译安装 请阅读在安装包里的 INSTALL 文件。
或者使用 PEAR installer with pear install sqlite。
SQLite 已经内置了,你不需要安装任何附加的软件(additional software)。
Windows users 可以下载 SQLite 扩展 DLL(
php_sqlite.dl)。
这里简单介绍一下: 假设你得到的是源代码 sqlite-3.3.8.tar.gz,这里将告诉你怎么编译它。
解压 sqlite-3.3.8.tar.gz 到 /home 目录下 For example: tar zxvf sqlite-3.3.8.tar.gz -C /home cd /home mkdir sqlite-3.3.8-ix86 cd /home/sqlite-3.3.8-ix86/ ../sqlite-3.3.8/configure --prefix/home/sqlite-3.3.8-ix86 编译并安装,然后生成帮助文档 make make install make doc 如果出现下列错误 ../sqlite-3.3.8/src/tclsqlite.c: In function DbUpdateHandler: ../sqlite-3.3.8/src/tclsqlite.c:333: warning: passing arg 3 of Tcl_ListObjAppendElementmakes pointer from integer without a cast ../sqlite-3.3.8/src/tclsqlite.c: In function tclSqlFunc: ../sqlite-3.3.8/src/tclsqlite.c:419: warning: passing arg 1 of Tcl_NewByteArrayObjdiscards qualifiers from pointer target type 这个都是 tcl 相关的错误可以先安装 ActiveTcl 以解决.假如你不需要 tcl 支持那么这个错误可以这样避免: cd /home/sqlite-3.3.8-ix86/ ../sqlite-3.3.8/configure --disable-tcl --prefix/home/sqlite-3.3.8-ix86 编译并安装,然后生成帮助文档 make make install make doc 不出意外将不会出现错误那么 Libraries have been installed in: /home/sqlite-3.3.8-ix86//lib 库文件已经生成在 /home/sqlite-3.3.8-ix86/lib 目录下 可执行文件 sqlite3 已经生成在 /home/sqlite-3.3.8-ix86/bin 目录下 下面创建一个新的数据库文件名叫zieckey.db 当然你可以使用不同的名字 来测试数据库. 直接输入: /home/sqlite-3.3.8-ix86/bin/sqlite3 test.db 如果出现下面字样表明编译安装已经成功了. SQLite version 3.3.8 Enter .help for instructions sqlite 二、使用 QT3 连接 SQLite rootlocalhost zieckey mkdir test-qt3-sqlite3 rootlocalhost zieckey cd test-qt3-sqlite3/打开 Designerrootlocalhost test-qt3-sqlite3 designer4 8357新建一个 C Project新建一个 Dialog在该 ialog 上放置一个 PushButton 和一个 LineEdit并设置相应的属性保存到 test-qt3-sqlite3 目录下新建一个 C Main-file main.cpp 再保存然后生成 .h.cpp 文件rootlocalhost test-qt3-sqlite3 uic -o mainform.h mainform.uirootlocalhost test-qt3-sqlite3 uic -i mainform.h -o mainform.cpp mainform.ui修改 .pro 文件,如下:SOURCES main.cpp mainform.cppHEADERS mainform.hunix UI_DIR .uiMOC_DIR .mocOBJECTS_DIR .objTEMPLATE appCONFIG qt warn_on releaseLANGUAGE CSQLITE_PATH/home/sqlite-3.3.8-ix86DEPENDPATH SQLITE_PATH/includeINCLUDEPATH SQLITE_PATH/includeLIBS -LSQLITE_PATH/libLIBS -lsqlite3TARGET test-sqlite.out编辑 mainform.h/ Form interface generated from reading ui file mainform.ui Created: 日 11 月 5 16:24:45 2006 by: The User Interface Compiler Id: qt/main.cpp 3.1.1 edited Nov 21 17:40 WARNING All changes made in this file will be lost/ifndef MAINFORM_Hdefine MAINFORM_Hincludeincludeinclude sqlite3.h //注意,这里一定要添加进来class QVBoxLayoutclass QHBoxLayout class QGridLayout class QLineEdit class QPushButton class MainForm : public QDialog Q_OBJECT public: MainForm QWidget parent 0 const char name 0 bool modal FALSE WFlags fl 0 MainForm QLineEdit showMsgLineEdit QPushButton openButton public slots: virtual void openDBSlot //注意,这里是新建的一个 SLOT protected: protected slots: virtual void languageChange endif // MAINFORM_H 编辑 mainform.cpp / Form implementation generated from reading ui file mainform.ui Created: 日 11 月 5 16:25:05 2006 by: The User Interface Compiler Id: qt/main.cpp 3.1.1 edited Nov 21 17:40 WARNING All changes made in this file will be lost/include mainform.hincludeincludeincludeincludeincludeincludeincludeinclude/ Constructs a MainForm as a child of parent with the name name and widget flags set to f. The dialog will by default be modeless unless you set modal to TRUE to construct a modal dialog./MainForm::MainForm QWidget parent const char name bool modal WFlags fl : QDialog parent name modal fl if name setName MainForm showMsgLineEdit new QLineEdit this showMsgLineEdit showMsgLineEdit-setGeometry QRect 150 230 350 21 openButton new QPushButton this openButton openButton-setGeometry QRect 150 70 91 30 languageChangeresize QSize600 480.expandedTominimumSizeHint // signals and slots connectionsconnect openButton SIGNAL clicked this SLOT openDBSlot / Destroys the object and frees any allocated resources/MainForm::MainForm// no need to delete child widgets Qt does it all for us/ Sets the strings of the subwidgets using the current language./void MainForm::languageChange setCaption tr A test Showing how to connect SQLite3 in QT3 openButton-setText tr Open DB void MainForm::openDBSlot //qWarning MainForm::openDBSlot: Not implemented yet sqlite3 dbNULL int rc rc sqlite3_openzieckey.db db //打开指定的数据库文件如果不存在将创建一个同名的数据库文件 if rc QString errMsgQString errMsgQString.sprintfCant open database: sn sqlite3_errmsgdb showMsgLineEdit-setTexterrMsgQString sqlite3_closedb else showMsgLineEdit-setText open zieckey.db successfullyn sqlite3_closedb //关闭数据库 main.cpp 如下,它不用做任何更改,如果我们是按照上面说的那样生成 main.cpp 的话 include include mainform.h int main int argc char argv QApplication a argc argv MainForm w w.show a.connect a SIGNAL lastWindowClosed a SLOT quit return a.exec 这一切做好了后,我们就可以开始编译了 rootlocalhost test-qt3-sqlite3 qmake rootlocalhost test-qt3-sqlite3 make 中间也许会有很多警告信息,这个不是太大问题,如果没有错误,那么我们可以运行了: rootlocalhost test-qt3-sqlite3 ./test-sqlite.out 也许会出现下面这样的错误: rootlocalhost test-qt3-sqlite3 ./test-sqlite.out ./test-sqlite.out: error while loading shared libraries: libsqlite3.so.0: cannot open sharedobject file: No such file or directory 这个好办: rootlocalhost qt3-sqlite3 exportLD_LIBRARY_PATHLD_LIBRARY_PATH:/home/sqlite-3.3.8-ix86/lib rootlocalhost qt3-sqlite3 ./test-sqlite.out 这样就好了。
看到运行的效果了没? 点击一下界面上的按钮,看看有什么反应? 应该是这样的: 那个标签条上显示: open zieckey.db successfully 说明:实际应用的时候,在 qt3 下根本就不需要显示的调用 uic 生成 .h .cpp 文件, 这里是方便行文才这样做的。
总结:这里我们知道了 sqlite3.3.8 的 Linux 环境下编译、QT Designer 的基本用法、QT编程的基本实现 最重要的是我们知道了怎么在 qt 下连接 sqlite。
本文纯粹是交流之作,仅作抛砖引玉之用。
还希望更多的高手们出来说说话阿。
欢迎交流 Qt 下直接使用 sqliteshiyan.hincludeincludeincludeincludeincludeclass shiyan : public QWidgetQ_OBJECTQPushButton charuQPushButton chaxunQLineEdit bianjiQSqlDatabase dbpublic:shiyanQWidget parent 0public slots:void charuzhivoid huoquzhishiyan.cppincludeincludeinclude shiyan.hshiyan::shiyanQWidget parent:QWidgetparentcharu new QPushButtoncharuchaxun new QPushButtonchaxunbianji new QLineEditabcQVBoxLayout layout new QVBoxLayoutlayout-addWidgetbianjilayout-addWidgetcharulayout-addWidgetchaxunsetLayoutlayoutdb QSqlDatabase::addDatabaseQSQLITEdb.setDatabaseName/home/zzh/123/zzhdb.openconnectcharuSIGNALclickedthisSLOTcharuzhiconnectchaxunSIGNALclickedthisSLOThuoquzhivoid shiyan::charuzhiifdb.isOpenQString zhi bianji-textQSqlQuery queryquery.prepareinsert into biao values:idquery.bindValue:idzhiquery.execvoid shiyan::huoquzhiQSqlQuery queryquery.execSELECT a FROM biaowhile query.next QString name query.value0.toStringbianji-setTextnamemain.cppincludeinclude shiyan.hint mainint argc char argvQApplication appargc argvshiyan widgetwidget.showreturn app.exec
上一篇:
eclipse插件开发之扩展项目特性
下一篇:
谈谈我国养老旅游产品开发策略