簡單的數據庫接口
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>---></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[0]+Command_insert[1]+Command_insert[3]+ID+","+"\""+NAME+"\""+","+TEMP+Command_insert[4]; // 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[0] + Command_search[1] + name + Command_search[1]; qDebug() << command; query.exec(command); while(query.next()) // wait for database finish the command just now { int val1 = query.value(0).toInt(); QString val2 = query.value(1).toString(); // Getting the content in the query! int val3 = query.value(3).toInt(); qDebug() << val1 << val2 << val3; ui->SearchRes->setText(query.value(0).toString()+query.value(1).toString()+query.value(2).toString()); } }
完成上述操作就可以編譯運行Qt程序了。
