linux qt 连接sqlite3,RedHat 9 Linux下在QT3.1中连接SQLite3全过程详细记录
作者: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
#include
#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
#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 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
#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。本文純粹是交流之作,僅作拋磚引玉之用。
歡迎交流。
總結
以上是生活随笔為你收集整理的linux qt 连接sqlite3,RedHat 9 Linux下在QT3.1中连接SQLite3全过程详细记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux scp移动文件夹,linux
- 下一篇: linux ubuntu安装 mono,