目录Qt4访问sqlite数据库 (2)RedHat 9 Linux下在QT3.1中连接SQLite3全过程详细记录 (6)基于ARM-Linux的SQLite嵌入式数据库技术 (17)关于在qt中如何连接sqlite3数据库的问题 (23)SQLite 完整中文FAQ (32)C/C++中调用SQLITE3的基本步骤 (40)SQLite嵌入式数据库系统的研究与实现 (50)Qt4访问sqlite数据库/index.php/2008/09/qt-sqlite/sqlite简介sqlite 是一款轻量级的、基于文件的嵌入式数据库,2000年就已经诞生,经过7年多的发展,直到今天已经成为最流行的嵌入式数据库,包括google在内的公司在其桌面软件中亦使用sqlite 存储用户数据。
由此可以看出,已经没有任何理由去怀疑sqlite的稳定性了。
sqlite的优势1. 免配置,和access一样,只要把数据库文件通过ftp上传到服务器上就可以使用,不需要服务器的额外支持2. 备份方便,因为只是一个文件,只要复制一份该文件,就能备份整个数据库3. 虽然是轻量级数据库,但他支持最大2tb 的单个库文件。
4. 快,无与伦比的快。
经过实际测试,在几百万记录的情况下,sqlite的插入和查询速度和mysql 不分上下,快于sql server,10倍于access (但这并不意味着它可以替代sql server )用QT操作sqlite由于sqlite属于轻量级的数据库,不需要配置,不需要安装,也不需要管理员,所以也就没必要像操作mysql等数据库一样的设置主机,用户和密码了。
样例如下:1 2 3 4 5 6 7 8 9101112 #include <QApplication> #include <QTextEdit>#include <QString>#include <QSqlDatabase> #include <QSqlQuery>#include <QSqlError>1314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 #include <QSqlDriver>#include <QDateTime>#include <QDebug>int main(int argc, char*argv[]){QApplication app(argc, argv);QTextEdit display;display.resize(400, 160);display.show(); //下面进行数据库的设置QSqlDatabase db =QSqlDatabase::addDatabase("QSQLITE"); //使用sqlite数据库驱动db.setDatabaseName("test"); //我们之前建立的数据库bool ok = db.open(); //尝试连接数据库if(ok){//这里用text已经成功连上数据库QSqlQuery query; //新建一个查询的实例if(query.exec("select * from student"))//尝试列出student 表的所有记录{//本次查询成功int numRows =0; //询问数据库驱动,是否驱动含有某种特性57585960616263if(db.driver()->hasFeature(QSqlDriver::QuerySize)){numRows =query.size(); //如果支持结果影响的行数,那么直接记录下来}else{st(); //否则定位到结果最后,qt 文档说,这个方法非常慢numRows = query.at()+1;query.seek(-1);}QString name, age;display.append("================================= ==========");while(query.next()){//定位结果到下一条记录name = query.value(0).toString();age = query.value(1).toString();QString result = name +" "+ age;display.append(result);}display.append("================================= ==========");display.append(QString("totally %1 rows").arg(numRows));}else{//如果查询失败,用下面的方法得到具体数据库返回的原因QSqlError error = stError();display.append("From mysql database: "+ error.databaseText());}}else{//打开数据库失败,显示数据库返回的失败描述display.append("cannot open database.");display.append("Reason: "+ stError().databaseText());}QApplication::connect(&app, SIGNAL(lastWindowClose()), &app, SLOT(quit()));return app.exec();}RedHat 9 Linux下在QT3.1中连接SQLite3全过程详细记录/u/16292/showart_223940.html作者:zieckey(zieckey@)All Rights Reserved下文介绍的内容都是基于Linux RedHat 9.0 平台的。
1. 说明这里我们假设你已经编译好了sqlite的库文件:libsqlite3.a libsqlite3.so libsqlite3.so.0 libsqlite3.so.0.8.6 pkgconfig和可执行文件: sqlite3我们再假设你的sqlite3的安装目录在/usr/local/sqlite3 目录下。
如果不是,我们可以这样做,将你的安装文件复制到/usr/local/sqlite3 这个目录,这样我们好在下面的操作中更加统一,从而减少出错的概率例如:[root@localhost home]# cp -rf sqlite-3.3.8-ix86/ /usr/local/sqlite3 这里假设/usr/local/sqlite3/是你的安装目录,也就是说你的sqlite原来就是安装在这里这样之后,我们的sqlite3的库文件目录是:/usr/local/sqlite3/lib可执行文件sqlite3的目录是:/usr/local/sqlite3/bin头文件sqlite3.h 的目录是:/usr/local/sqlite3/include可以用ls命令查看下:[root@localhost sqlite]# ls /usr/local/sqlite3/liblibsqlite3.a libsqlite3.so libsqlite3.so.0 libsqlite3.so.0.8.6 pkgconfig二、使用QT3连接SQLite[root@localhost zieckey]# mkdir test-qt3-sqlite3[root@localhost zieckey]# cd test-qt3-sqlite3/打开Designer[root@localhost test-qt3-sqlite3]# designer&[4] 8357新建一个C++ Project新建一个Dialog在该dialog上放置一个PushButton 和一个LineEdit并设置相应的属性保存到test-qt3-sqlite3 目录下新建一个C++ Main-file (main.cpp )再保存然后生成*.h,*.cpp文件[root@localhost test-qt3-sqlite3]# uic -o mainform.h mainform.ui [root@localhost test-qt3-sqlite3]# uic -i mainform.h -o mainform.cpp mainform.ui注:这里的mainform.ui 是你的Dialog 的保存文件名字。
修改*.pro文件,如下:SOURCES += main.cpp mainform.cppHEADERS += mainform.hunix {UI_DIR = .uiMOC_DIR = .mocOBJECTS_DIR = .obj}TEMPLATE =appCONFIG += qt warn_on releaseLANGUAGE = C++SQLITE_PATH=/usr/local/sqlite3DEPENDPATH += $$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 <qvariant.h>#include <qdialog.h>#include "sqlite3.h" //注意,这里一定要添加进来class QVBoxLayout;class QHBoxLayout;class QGridLayout;class QLineEdit;class QPushButton;class MainForm : public QDialog{Q_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(); //注意,这里是新建的一个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.h"#include <qvariant.h>#include <qlineedit.h>#include <qpushbutton.h>#include <qlayout.h>#include <qtooltip.h>#include <qwhatsthis.h>#include <qimage.h>#include <qpixmap.h>/** 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).expandedT o(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("Can't open database: %s\n", sqlite3_errmsg(db) );showMsgLineEdit->setText(errMsgQString);sqlite3_close(db);}elseshowMsgLineEdit->setText( "open zieckey.db successfully!\n" );sqlite3_close(db); //关闭数据库}main.cpp如下,它不用做任何更改,如果我们是按照上面说的那样生成main.cpp的话#include <qapplication.h>#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();}这一切做好了后,我们就可以开始编译了.这里说明一下,在这之后,我们关心的只有四个文件:main.cpp mainform.h mainform.cpp *.pro[root@localhost test-qt3-sqlite3]# qmake[root@localhost test-qt3-sqlite3]# make中间也许会有很多警告信息,这个不是太大问题,如果没有错误,那么我们可以运行了:[root@localhost test-qt3-sqlite3]# ./test-sqlite.out也许会出现下面这样的错误:[root@localhost 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这个好办:[root@localhost qt3-sqlite3]# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/sqlite3/lib[root@localhost qt3-sqlite3]# ./test-sqlite.out这样就好了。