作者: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全过程详细记录相关推荐

  1. linux qt执行adb,类豌豆荚: Linux Mint实测QtADB安卓管理客户端

    虽说安卓是 Linux 的衍生物,但 Linux 下的安卓手机管理软件却几乎是一片荒漠. 犹记得去年初,薄荷叶小编刚开始刷机折腾我的 HTC G11 时,也不得不用 Virtualbox 安装虚拟 W ...

  2. 红帽linux安装显卡驱动,RedHat Enterprise Linux 6 安装ATI显卡驱动

    说说在RedHat Enterprise Linux 6 下安装ATI显卡驱动的过程: su -   /*切换到root用户*/ yum update  /*查看一下yum更新*/ yum insta ...

  3. linux qt 找不到 lgl,Linux Qt cannot find -lGL错误完美解决方案(亲测有效)

    http://c.biancheng.net/view/3901.html 对于很多 Linux 发行版本,Qt 安装完成后如果直接编译或者运行项目,会出现"cannot find -lGL ...

  4. skyeye linux qt,在ARM9上安装Linux,利用SkyEye模拟器及U-BOOT引导

    提示:本人在2008年进行Linux@ARM实验,在ARM9上安装Linux.利用SkyEye模拟器及U-BOOT引导.这是实验报告.以及实验过程中留下的记录,按日期倒序排列. 本文以Creative ...

  5. linux+磁带机检查,Redhat Enterprise Linux磁带机简单操作方法

    这里可看到,磁带上有两个文件同名的文件写入.这个文件的两次备份大小和时间可以相同,也可以不相同. 注意:在磁带上如果相同文件写入了多次,在恢复时候会比较麻烦,需要先将磁带卷至文件所存储的地方,而后读取 ...

  6. 英雄联盟一直连接服务器win10,Win10系统下玩lol提示“无法连接服务器”怎么解决?...

    Win10系统下玩lol提示"无法连接服务器"怎么解决?很多伙伴在Win10系统运行lol游戏时都会提示error,最近一位伙伴反馈的提示为"连接失败:无法连接服务器,请 ...

  7. 连接超时这种异常怎么处理比较好_Golang中SSH.NewSession超时问题记录

    背景 项目中使用http://golang.org/x/crypto/ssh来建立SSH连接,最近使用过程中功能发生异常,使用pprof查找时,发现会有大量的业务goroutine卡在client.N ...

  8. linux qt搜狗输入法用不,Ubuntu14.04下搜狗输入法不支持Qt5环境

    问题环境确认: 系统平台Ubuntu14.04/ 安装了搜狗输入法/ 安装Qt5.4.1 一般自定义下载来Qt软件包,安装之后,都会有这个问题,在Qt环境下或Qt Creator下不能切换输入法,也不 ...

  9. svn linux中文语言包,Redhat Enterprise linux配置svn客户端及常用命令

    一,安装客户端: 1.平台: Red Hat Enterprise Linux releases 4 and 5 CentOS releases 4 and 52.软件: CollabNetSubve ...

最新文章

  1. vue-router使用next()跳转到指定路径时会无限循环
  2. Bootstrap进度条
  3. Python爬虫(十三)_案例:使用XPath的爬虫
  4. ASP.NET系统退出(移除Session 、清除浏览器缓存)
  5. 什么是WebSocket,它与HTTP有何不同?
  6. 冰点文库下载V2绿色版,无需积分自由下载百度,mbalib,豆丁,畅享,hp009,max.book118 文档...
  7. 腾讯:干掉头条,先拿抖音开刀!
  8. 贺利坚老师汇编课程18笔记:栈的操作SS:SP
  9. eclipse最有用快捷键整理
  10. 金字塔c_FPN特征金字塔网络解读
  11. python笔记:太困了,读取并显示按行业分类的股票数据提提神
  12. 20170306 小兵的觉悟就是好好吃饭
  13. 减脂单吃全麦面包太乏味?全麦面包的N种吃法合集
  14. linux link/symlink/unlink 硬连接和软连接介绍
  15. 【IDEA】单独运行一个类的方法
  16. 正斜杠(/)与反斜杠(\)总结
  17. Codeforces Round #767 (Div. 2)题解
  18. [算法课]全面翻新计划!第十二周全解
  19. 研究生阶段的研究方向以及一些想法
  20. 5G基站基带架构设计之总体篇

热门文章

  1. 机器学习——标准化/归一化的目的、作用和场景
  2. LED芯片,应用品,蓝宝石衬底,集成电路,UV
  3. 2021年大数据ELK(十七):Elasticsearch SQL 订单统计分析案例
  4. 启动MySQL:net start mysql出现问题+本地Mysql忘记密码的修改方法
  5. python 字符串拼接
  6. 20190226-利用序列化完成小型记账程序
  7. FLINK源代码调试方式
  8. Solr定时重建索引和增量更新
  9. 2022-2028年中国半导体硅片行业深度调研及投资前景预测报告
  10. Unit05: 创建和访问数组 、 数组的常用方法_1