linux qt 连接sqlite3,RedHat 9 Linux下在QT3.1中连接SQLite3全过程详细记录
作者:zieckey([email]zieckey@yahoo.com.cn[/email])
All Rights Reserved
下文介紹的內(nèi)容都是基于 Linux RedHat 9.0 平臺(tái)的。
1. 說(shuō)明
這里我們假設(shè)你已經(jīng)編譯好了sqlite的庫(kù)文件 :
libsqlite3.a? libsqlite3.la? libsqlite3.so? libsqlite3.so.0? libsqlite3.so.0.8.6? pkgconfig
和可執(zhí)行文件 : sqlite3
我們?cè)偌僭O(shè)你的sqlite3的安裝目錄在 /usr/local/sqlite3 目錄下。
如果不是,我們可以這樣做,將你的安裝文件復(fù)制到 /usr/local/sqlite3 這個(gè)目錄,
這樣我們好在下面的操作中更加統(tǒng)一,從而減少出錯(cuò)的概率
例如:[root@localhost home]# cp -rf sqlite-3.3.8-ix86/ /usr/local/sqlite3
這里假設(shè) /usr/local/sqlite3/ 是你的安裝目錄,也就是說(shuō)你的sqlite原來(lái)就是安裝在這里
這樣之后,我們的sqlite3的庫(kù)文件目錄是:/usr/local/sqlite3/lib
可執(zhí)行文件 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
新建一個(gè)C++ Project
新建一個(gè) Dialog
在該dialog上放置一個(gè) PushButton 和一個(gè) LineEdit
并設(shè)置相應(yīng)的屬性
保存到 test-qt3-sqlite3 目錄下
新建一個(gè) 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"??????????? //注意,這里一定要添加進(jìn)來(lái)
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();???????? //注意,這里是新建的一個(gè)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" );
//這里是數(shù)據(jù)庫(kù)的訪問(wèn)
sqlite3 *db=NULL;
int rc;
rc = sqlite3_open("zieckey.db", &db); //打開指定的數(shù)據(jù)庫(kù)文件,如果不存在將創(chuàng)建一個(gè)同名的數(shù)據(jù)庫(kù)文件
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); //關(guān)閉數(shù)據(jù)庫(kù)
}
main.cpp如下,它不用做任何更改,如果我們是按照上面說(shuō)的那樣生成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();
}
這一切做好了后,我們就可以開始編譯了.
這里說(shuō)明一下,在這之后,我們關(guān)心的只有四個(gè)文件:
main.cpp? mainform.h?? mainform.cpp?? *.pro
[root@localhost test-qt3-sqlite3]# qmake
[root@localhost test-qt3-sqlite3]# make
中間也許會(huì)有很多警告信息,這個(gè)不是太大問(wèn)題,如果沒(méi)有錯(cuò)誤,那么我們可以運(yùn)行了:
[root@localhost test-qt3-sqlite3]# ./test-sqlite.out
也許會(huì)出現(xiàn)下面這樣的錯(cuò)誤:
[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
這個(gè)好辦:
[root@localhost qt3-sqlite3]# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/sqlite3/lib
[root@localhost qt3-sqlite3]# ./test-sqlite.out
這樣就好了。
看到運(yùn)行的效果了沒(méi)?
點(diǎn)擊一下界面上的按鈕,看看有什么反應(yīng)?
應(yīng)該是這樣的:
那個(gè)標(biāo)簽條上顯示:?? open zieckey.db successfully!
說(shuō)明:實(shí)際應(yīng)用的時(shí)候,在qt3下根本就不需要顯示的調(diào)用 uic 生成 .h .cpp 文件,
這里是方便行文才這樣做的。
總結(jié):這里我們知道了QT Designer 的基本用法、QT編程的基本實(shí)現(xiàn);
最重要的是我們知道了怎么在qt下連接sqlite。本文純粹是交流之作,僅作拋磚引玉之用。
歡迎交流。
總結(jié)
以上是生活随笔為你收集整理的linux qt 连接sqlite3,RedHat 9 Linux下在QT3.1中连接SQLite3全过程详细记录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux scp移动文件夹,linux
- 下一篇: linux ubuntu安装 mono,