QT 连接数据库 入门实例(很好的一个例子)

http://konglingchun.is-programmer.com/posts/12676.html

(摘)Qt之数据库编程

孔令春 posted @ 2009年10月30日 17:33 in  Qt 技术 with tags  QtSql模块 , 3671 阅读

摘自:《C++ Gui Qt4编程》

        在Qt中,实现与数据库编程相关的模块是QtSql模块,该模块提供了一组与平台以及数据库种类无关的SQL数据库访问接口。此接口通过驱动程序与各种数据库进行通信。Qt桌面版提供的驱动程序如下:

驱动程序 数据库
QDB2

IBM DB2 7.1版以及更高版本

QOCI 甲骨文Oracle
QODBC ODBC(包括微软公司的SQL服务器)
QMYSQL MySQL
QPSQL PostgreSQL的7.3版以及更高级的版本

 

一、连接数据库

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool connDB()
{
     QSqlDatabase db = QSqlDatabase::addDatabase(dbDriver); //添加驱动
     db.setHostName(hostName); //设置主机名
     db.setDatabaseName(dbName); //设置数据库名
     db.setUserName(userName); //设置用户名
     db.setPassword(userPwd); //设置用户密码
 
     //发送连接
     if (!db.open())
     {
         qDebug << db.lastError();
         return false ;
     }
     return true ;
}

 二、操作数据库

?
1
2
3
4
5
6
7
8
9
10
11
12
bool queryDB( const QString &sql)
{
     QSqlQuery query;
     query.exec(sql);
     
     if (query.next()) //如果有记录则为真 否则退出判断
     {
         ...
         return true ;
     }
     return false ;
}

三、实例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
  *功能:一个登录小模块
  *软件环境:ubuntu 9.04, qt4.5, PostgreSQL8.3
  *main.cpp
*/
 
#include
#include
#include "login.h"
 
int main( int argc, char *argv[])
{
     QApplication app(argc, argv);
     QTextCodec::setCodecForTr(QTextCodec::codecForName( "utf-8" ));
 
     Login *login = new Login;
     login->show();
 
     return app.exec();
}

  

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
  *login.h
  *说明:登录主界面的头文件
*/
 
#ifndef LOGIN_H
#define LOGIN_H
 
#include
class QLineEdit;
class QPushButton;
 
class Login : public QDialog
{
     Q_OBJECT
 
public :
     Login(QWidget *parent = 0);
 
public slots:
     void logined();
 
private :
     QLineEdit *userNameLine;
     QLineEdit *userPwdLine;
 
     QPushButton *loginButton;
};
#endif

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
  *login.cpp
  *说明:登录主界面
*/
#include
#include "login.h"
#include "mysql.h"
 
Login::Login(QWidget *parent)
     :QDialog(parent)
{
     QLabel *userNameLabel = new QLabel(tr( "用户名:" ));
     userNameLine = new QLineEdit;
 
     QLabel *userPwdLabel = new QLabel(tr( "密码:" ));
     userPwdLine = new QLineEdit;
 
     loginButton = new QPushButton(tr( "登录" ));
     connect(loginButton, SIGNAL(clicked()), this , SLOT(logined()));
 
     QHBoxLayout *buttonLayout = new QHBoxLayout;
     buttonLayout->addStretch();
     buttonLayout->addWidget(loginButton);
 
     QGridLayout *mainLayout = new QGridLayout;
     mainLayout->addWidget(userNameLabel, 0, 0);
     mainLayout->addWidget(userNameLine, 0, 1);
     mainLayout->addWidget(userPwdLabel, 1, 0);
     mainLayout->addWidget(userPwdLine, 1, 1);
     mainLayout->addLayout(buttonLayout, 2, 1);
     //mainLayout->addWidget(loginButton, 2, 1);
     setLayout(mainLayout);
     setWindowTitle(tr( "登录" ));
}
 
void Login::logined()
{
     QString name = userNameLine->text();
     QString passwd = userPwdLine->text();
     QString sql = "select name, password from users where name = '"
         + name + "'and password ='" + passwd + "'" ;
 
     MySql mySql;
     if ( mySql.queryDB(sql) )
     {
         QMessageBox::information( this , tr( "登录成功" ),
                 tr( "登录成功!欢迎进入本系统!" ),
                 QMessageBox::Ok);
     }
     else
     {
         QMessageBox::warning( this , tr( "登录失败" ),
                 tr( "用户名或密码错误!是否重新登录?" ),
                 QMessageBox::Yes|QMessageBox::No);
 
     }
 
}

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
  *mysql.h
  *说明:封装的SQL连接
*/
 
#ifndef MYSQL_H
#define MYSQL_H
 
#include
 
class MySql : public QObject
{
public :
     MySql(QObject *parent = 0);
 
     bool connDB();
     bool queryDB( const QString &sql);
 
private :
     QString dbDriver;
     QString dbName;
     QString userName;
     QString userPwd;
     QString hostName;
     int hostPort;
};
#endif

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
  *mysql.cpp
  *说明:封装的SQL连接
*/
#include
#include
#include
#include "mysql.h"
 
MySql::MySql(QObject *parent)
     :QObject(parent)
{
     dbDriver= "QPSQL" ;  
     dbName= "mydb" ;
     userName= "konglingchun" ;
     userPwd= "klcstudy" ;
     hostName= "localhost" ;
     hostPort=5432;
     connDB();
}
 
bool MySql::connDB()

你可能感兴趣的:(数据库技巧,攻克QT)