




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 /homecd /homemkdir sqlite-3.3.8-ix86cd /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_ListObjAppendElement makes 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_NewByteArrayObj discards 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.8Enter .help for instructionssqlite二、使用QT3连接SQLiterootlocalhost zieckey# mkdir test-qt3-sqlite3rootlocalhost zieckey# cd test-qt3-sqlite3/打开Designerrootlocalhost test-qt3-sqlite3# designer&4 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 = C+SQLITE_PATH=/home/sqlite-3.3.8-ix86DEPENDPATH += $SQLITE_PATH/includeINCLUDEPATH += $SQLITE_PATH/includeLIBS += -L$SQLITE_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_H#define MAINFORM_H#include #include #include sqlite3.h /注意,这里一定要添加进来class QVBoxLayout;class QHBoxLayout;class QGridLayout;class QLineEdit;class QPushButton;class MainForm : public QDialogQ_OBJECTpublic:MainForm( QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags fl = 0 );MainForm();QLineEdit* showMsgLineEdit;QPushButton* openButton;public slots:virtual void openDBSlot(); /注意,这里是新建的一个SLOTprotected: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.h#include #include #include #include #include #include #include #include /* * 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 ) );languageChange();resize( QSize(600, 480).expandedTo(minimumSizeHint() );/ 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 *db=NULL;int rc;rc = sqlite3_open(zieckey.db, &db); /打开指定的数据库文件,如果不存在将创建一个同名的数据库文件if( rc )QString errMsgQString;errMsgQString.sprintf(Cant open database: %sn, sqlite3_errmsg(db) );showMsgLineEdit-setText(errMsgQString);sqlite3_close(db);else showMsgLineEdit-setText( open zieckey.db successfully!n );sqlite3_close(db); /关闭数据库main.cpp如下,它不用做任何更改,如果我们是按照上面说的那样生成main.cpp的话#include #include mainform.hint 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# qmakerootlocalhost 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 shared objectfile: No such file or directory这个好办:rootlocalhost qt3-sqlite3# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/sqlite-3.3.8-ix86/librootlocalhost qt3-sqlite3# ./test-sqlite.out这样就好了。看到运行的效果了没?点击一下界面上的按钮,看看有什么反应?应该是这样的:那个标签条上显示: open zieckey.db successfully!说明:实际应用的时候,在qt3下根本就不需要显示的调用 uic 生成 .h .cpp 文件,这里是方便行文才这样做的。总结:这里我们知道了sqlite3.3.8的Linux环境下编译、QT Designer 的基本用法、QT编程的基本实现;最重要的是我们知道了怎么在qt下连接sqlite。本文纯粹是交流之作,仅作抛砖引玉之用。还希望更多的高手们出来说说话阿。欢迎交流Qt下直接使用sqliteshiyan.h #include #include #include #include #include class shiyan : public QWidget Q_OBJECT QPushButton *charu; QPushButton *chaxun; QLineEdit *bianji; QSqlDatabase db; public: shiyan(QWidget *parent = 0); public slots: void charuzhi(); void huoquzhi(); ; shiyan.cpp #include #include #include shiyan.h shiyan:shiyan(QWidget *parent):QWidget(parent) charu = new QPushButton(charu); chaxun = new QPushButton(chaxun); bianji = new QLineEdit(abc); QVBoxLayout *layout = new QVBoxLayout; layout-addWidget(bianji); layout-addWidget(charu); layout-a
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年国企岗位笔试试题及答案
- 2025年电焊工考试题(含答案)
- 2025年广东教师招聘考试(教育政策法规)综合试题及答案
- 2025年职业卫生健康考试题库(附含答案)
- 2025年海南省海口市电工证考试题模拟试题初级电工培训试题及答案
- 2025年医师定期考核临床专业知识考试模拟题库及解析答案
- 新版分红激励协议参考样板6篇
- 2025年食检专业面试题目及答案
- 坚果种植大数据分析创新创业项目商业计划书
- 2025年安全员之A证考试题库及参考答案
- 2025新外研版初中英语七年级上全册课文翻译
- 检验科室内质控培训课件
- 桥梁拆除施工质量保证技术措施
- 人体工程学-第五章-人体工程学与室外环境设施设计
- 2025-2030年中国象棋行业发展分析及前景趋势与投资风险研究报告
- 2025-2030年中国冷链物流行业深度分析及发展前景与发展战略研究报告
- 抖音员工合同协议书模板
- 《王戎不取道旁李》教案
- 医学检验技术课件教学
- 胃肠肿瘤康复管理要点
- 专项安全施工方案监理
评论
0/150
提交评论