作者:zieckey( [email]zieckey@yahoo.com.cn[/email])
All Rights Reserved
   
下文介绍的内容都是基于 Linux RedHat 9.0 平台的。
1. 说明
这里我们假设你已经编译好了sqlite的库文件 :
libsqlite3.a  libsqlite3.la  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/lib
libsqlite3.a  libsqlite3.la  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.cpp
HEADERS += mainform.h
unix {
  UI_DIR = .ui
  MOC_DIR = .moc
  OBJECTS_DIR = .obj
}
TEMPLATE =app
CONFIG += qt warn_on release
LANGUAGE = C++
SQLITE_PATH=/usr/local/sqlite3
DEPENDPATH  += $$SQLITE_PATH/include
INCLUDEPATH += $$SQLITE_PATH/include
LIBS += -L$$SQLITE_PATH/lib
LIBS +=  -lsqlite3
TARGET  = 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_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.h"
#include <qvariant.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qp_w_picpath.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).expandedTo(minimumSizeHint()) );
    // signals and slots connections
    connect( 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);
    }
    else
  showMsgLineEdit->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 object
file: 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
这样就好了。
看到运行的效果了没?
点击一下界面上的按钮,看看有什么反应?
应该是这样的:
那个标签条上显示:   open zieckey.db successfully!

说明:实际应用的时候,在qt3下根本就不需要显示的调用 uic 生成 .h .cpp 文件,
这里是方便行文才这样做的。
总结:这里我们知道了QT Designer 的基本用法、QT编程的基本实现;
最重要的是我们知道了怎么在qt下连接sqlite。本文纯粹是交流之作,仅作抛砖引玉之用。
欢迎交流。