简单的数据库接口

 Step1:首先完成整个UI界面的额设计:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>313</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>320</x>
<y>220</y>
<width>51</width>
<height>27</height>
</rect>
</property>
<property name="text">
<string>Insert</string>
</property>
</widget>
<widget class="QTextEdit" name="textEdit">
<property name="geometry">
<rect>
<x>80</x>
<y>220</y>
<width>51</width>
<height>31</height>
</rect>
</property>
</widget>
<widget class="QTextEdit" name="textEdit_2">
<property name="geometry">
<rect>
<x>140</x>
<y>220</y>
<width>101</width>
<height>31</height>
</rect>
</property>
</widget>
<widget class="QTextEdit" name="textEdit_3">
<property name="geometry">
<rect>
<x>250</x>
<y>220</y>
<width>61</width>
<height>31</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>100</x>
<y>200</y>
<width>31</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>ID</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>170</x>
<y>200</y>
<width>65</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>NAME</string>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>260</x>
<y>200</y>
<width>65</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>TEMP</string>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>20</x>
<y>110</y>
<width>171</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>10</pointsize>
<italic>true</italic>
<underline>false</underline>
<strikeout>false</strikeout>
</font>
</property>
<property name="cursor">
<cursorShape>BlankCursor</cursorShape>
</property>
<property name="text">
<string>Designed by : mm1994uestc</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_5">
<property name="geometry">
<rect>
<x>0</x>
<y>50</y>
<width>171</width>
<height>51</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>16</pointsize>
<italic>true</italic>
<underline>false</underline>
<strikeout>false</strikeout>
</font>
</property>
<property name="cursor">
<cursorShape>BlankCursor</cursorShape>
</property>
<property name="text">
<string>MySQL Control</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_6">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>321</width>
<height>51</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>28</pointsize>
<italic>true</italic>
<underline>false</underline>
<strikeout>false</strikeout>
</font>
</property>
<property name="cursor">
<cursorShape>BlankCursor</cursorShape>
</property>
<property name="text">
<string>Qt interface Demo!</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_7">
<property name="geometry">
<rect>
<x>20</x>
<y>220</y>
<width>51</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>Insert:</string>
</property>
</widget>
<widget class="QLabel" name="label_8">
<property name="geometry">
<rect>
<x>20</x>
<y>160</y>
<width>51</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>Search:</string>
</property>
</widget>
<widget class="QTextEdit" name="SearchInput">
<property name="geometry">
<rect>
<x>80</x>
<y>160</y>
<width>101</width>
<height>31</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_9">
<property name="geometry">
<rect>
<x>100</x>
<y>140</y>
<width>65</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>NAME</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>320</x>
<y>160</y>
<width>51</width>
<height>27</height>
</rect>
</property>
<property name="text">
<string>Search</string>
</property>
</widget>
<widget class="QLabel" name="SearchRes">
<property name="geometry">
<rect>
<x>230</x>
<y>160</y>
<width>81</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>Search Res</string>
</property>
</widget>
<widget class="QLabel" name="label_11">
<property name="geometry">
<rect>
<x>180</x>
<y>160</y>
<width>65</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>22</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>---&gt;</string>
</property>
</widget>
<widget class="QLabel" name="label_10">
<property name="geometry">
<rect>
<x>220</x>
<y>50</y>
<width>110</width>
<height>110</height>
</rect>
</property>
<property name="text">
<string> Cover</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

界面的设计如右图所示

我们能够分析功能,在search中填入内容之后,点击search按钮就会根据程序中定义使用的mysql数据库的数据库以及表进行数据的查询工作,并将查询的结果输出到右侧的lable当中。

Step2:根据上述分析的MySQ_UI的功能设计,完成槽函数的定义和实现:

首先需要添加sql的核心模块(关于sql模块的安装参考我的另一篇博客:http://www.cnblogs.com/uestc-mm/p/8920525.html):

QT += core gui sql

下面首先是mainwindow.h文件的内容,包含了头文件:#include <QSqlDatabase>、#include <QSqlQuery>这两个MySQL数据库需要使用到的头文件;

声明定义了SLOT槽函数:void on_pushButton_clicked(bool checked)、void on_pushButton_2_clicked()

定义了私有成员变量db:  QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL")

#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow>
#include <QSqlDatabase>
#include <QDebug>
#include <QSqlQuery> namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked(bool checked);
void on_pushButton_2_clicked();
private:
Ui::MainWindow *ui;
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); // define the db via
};
#endif // MAINWINDOW_H

接着在mainwindow.cpp文件中对变量db进行初始化和MySQL数据库进行连接, 并实现对应的槽函数:

void MainWindow::on_pushButton_clicked(bool checked)
{
QString ID,NAME,TEMP;
ID = ui->textEdit->toPlainText(); // Get the input content
NAME = ui->textEdit_2->toPlainText();
TEMP = ui->textEdit_3->toPlainText();
qDebug() << "ID:" << ID << " " << "NAME:" << NAME << " " << "TEMP:" << TEMP; qDebug() << "Aviliable SQL drivers!";
QStringList drivers = QSqlDatabase::drivers(); //Getting the support databases,if do not have the database driver,we can not use this databse
foreach(QString driver,drivers)
qDebug() << driver; if(!db.open()) // Connect to the database
{
qDebug() << "failed to connect to the mysql database!";
}
else
{
qDebug() << "Mysql is successfully Opened!";
} QSqlQuery query(db); //Create a new query for db,use query to change the content QString Command_insert[] = {"insert into ","GanSu","BeiJing"," values(",")"};
QString Command = "";
Command = Command_insert[]+Command_insert[]+Command_insert[]+ID+","+"\""+NAME+"\""+","+TEMP+Command_insert[]; // Combine the command qDebug() << Command; query.exec(Command); // excute the command to control the database
} void MainWindow::on_pushButton_2_clicked()
{
if(!db.open())
{
qDebug() << "failed to connect to the mysql database!";
}
else
{
qDebug() << "Mysql is successfully Opened!";
} QSqlQuery query(db);
QString name,command;
QString Command_search[] = {"select * from GanSu where name=","'"}; name = ui->SearchInput->toPlainText();
qDebug() << name;
command = Command_search[] + Command_search[] + name + Command_search[]; qDebug() << command; query.exec(command); while(query.next()) // wait for database finish the command just now
{
int val1 = query.value().toInt();
QString val2 = query.value().toString(); // Getting the content in the query!
int val3 = query.value().toInt();
qDebug() << val1 << val2 << val3; ui->SearchRes->setText(query.value().toString()+query.value().toString()+query.value().toString());
}
}

完成上述操作就可以编译运行Qt程序了。

9、Qt Project之简单的数据库接口的更多相关文章

  1. Qt Project的持续集成方案

    作者:齐亮链接:http://www.zhihu.com/question/24314354/answer/27547787来源:知乎著作权归作者所有,转载请联系作者获得授权. PETER HARTM ...

  2. 在CentOS下面编译WizNote Qt Project

    编译环境 CentOS 64位 Desktop 版本:6.5 编译前的准备: CentOS的用户默认没有root权限,如果当前用户没有权限root,则可以在终端里面执行下面的命令: su root s ...

  3. Qt WebKit and HTML5 geolocation | Qt Project forums | Qt Project

    Qt WebKit and HTML5 geolocation | Qt Project forums | Qt Project Qt WebKit and HTML5 geolocation   I ...

  4. DOM Traversal Example | Documentation | Qt Project

    DOM Traversal Example | Documentation | Qt Project DOM Traversal Example

  5. 关于VS2017+Qt5.6.3(msvc2015_64)联合编程Qt project settings界面没有ok,cancel选项的问题

    如题,我在项目开发的过程中,需要添加数据库模块SQL,然后发现VS上QT project settings选项中不能修改添加的模块,也就是对应QT creator中的在.pro文件中添加一句:QT + ...

  6. QT 选择对话框简单示例

    QT 选择对话框简单示例 部分代码: pDialog->addSeparator(); QAction *pmb2 = pDialog->addAction(QString::fromLo ...

  7. qt project settings被禁用解决方案

    转载请注明出处:http://www.cnblogs.com/dachen408/p/7422707.html qt project settings被禁用点击不了: 解决方案:需要点击该项目(或者项 ...

  8. 使用QT实现一个简单的登陆对话框(纯代码实现C++)

    使用QT实现一个简单的登陆对话框(纯代码实现C++) 效果展示 使用的QT控件 控件 描述 QLable 标签 QLineEdit 行文本框 QPushButton 按扭 QHBoxLayout 水平 ...

  9. 使用qt制作一个简单的计算器

    前言:今天使用qt制作了一个很简单的计算器,觉得挺有意思的,所以在这里跟大家分享一下. 这里先跟大家说说使用到的函数: 一.槽连接函数 connect(信号发送者,发送的信号,信号接收者,信号接收者的 ...

随机推荐

  1. 对mysql数据库中字段为空的处理

    数据库中字段为空的有两种:一种为null,另一种为空字符串.null代表数值未知,空字符串是有值得,只是为空.有时间我们想把数据库中的数据以excel形式导出时 如果碰到字段为空的,为空的字段会被后面 ...

  2. 最短路径之Floyd-warshall算法

    哇咔咔,最喜欢这种算法了,算法简单,暴力解决: 可惜数据大点就解决不了问题了: 输入的数据是 第一行第一个数是city的数量n,第二个是路径数t, 接下来n行为a至b的距离 4 81 2 2 1 3 ...

  3. 【python】python为何多线程无法切换

    写了一个kafka传输消息,celery发布任务的脚本. 有四个线程,分别读取不同的kafka队列信息 问题是,只有第一个线程会启动,剩下的三个线程连start都运行不了. 而且这个问题不是一开始就发 ...

  4. kali linux 安装wps office

    1.下载wps for linux 版本 wget http://kdl.cc.ksosoft.com/wps-community/download/6757/wps-office_10.1.0.67 ...

  5. python之路第二天

    为何要有操作系统 为了让程序员更轻松的完成命令电脑工作而存在的,控制硬件,服务于软件. 操作系统的位置 操作系统位于软件和硬件之间.操作系统由内核(运行于内核态,控制硬件)和系统调用(运行于用户态,为 ...

  6. CF1065D

    如果不喜欢过长代码的看官,请移步其他题解... 这题其实思想极其简单: 棋盘问题常见的算法都比较暴力,常用的有搜索和状压dp 而这道题显然没啥能状压的,所以我们考虑搜索 但是仅仅搜索是不够的,因为有极 ...

  7. h5 video标签的使用

     标签的布置 <video src="1.mp4" poster="1.jpg" id="vid" controls> 你的浏览 ...

  8. Python中对文件和目录的操作

    用到的核心模块有:os   shutil 文件的创建:f = open("文件名", "w")  注:如果涉及到乱码问题需要在后面加上encoding=&quo ...

  9. easyui实现背景图片半透明状态,悬浮在大背景之上

    首先是查找素材,使用AI将所需要的图案画出来,切记将图案的背景设置为所需要的透明状态.项目使用的是easyui架构 为啥加两个背景图呢,因为这样的布局最开始是给一个矩形框加上的背景图片,若是还使用矩形 ...

  10. lisp : set 与setq 函数

    在Lisp中,如果我们希望对一个变量赋值,可以使用set函数,用法如下: (set ‘my-value "my string") 上面的代码是对变量my-value进行赋值,值是& ...