生活随笔
收集整理的這篇文章主要介紹了
关于Qt的CRUD增删改查数据库那些事,带GUI图像界面
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
關于Qt的CRUD增刪改查數據庫那些事,帶GUI圖像界面
首先感謝CSDN平臺提供這樣強大的分享平臺. Qt Creator 的幾個常用快捷鍵必須要會,開發事半功倍, Ctrl 簡稱 C C + i 先 C + A再C + i 格式化代碼 F4 頭文件與源文件互相切換 F1獲取幫助的 C + alt + 向下箭頭 , 快速復制當前行代碼,跟Ec用法一樣 C + Shift + 向下箭頭或向上箭頭, 快速移動當前行代碼,跟Intellij idea快捷鍵 一樣 Shift + Delete 快速刪除當前行代碼,跟Intellij idea快捷鍵 一樣 直接先寫類,頭文件包含,讓編輯器自動給你創建 Alt + Enter ,跟Intellij idea快捷鍵 一樣 C+ F 查找,跟Ec用法一樣 C+B 編譯 C + R 運行 個人感覺QT Creator的提示功能不夠強大,對變量的提示不強大,對類的提示反應慢,之前有想過嘗試用VStadio 2017 或者CLion編譯器,但是QtCreator 原汁原味,它對UI設計很方便,無可否認,返回值類型和參數自己寫,java可以自動生成返回值類型,給你一個推薦的變量名,類型提示強大,變量提示強大,Qt 對代碼的報錯感覺也沒有java精準,java能夠精確到某一行. Qt報錯,有時候不會提示. Qt的版本不要裝的高,也不要低,高了你駕馭不了,對于初學者,低了跟不上時代, 目前最高版本5.14,低的版本2010左右發行的4.6.1 ,那時候還屬于Nokia公司,后來轉讓給Digia, 本人使用的是較為保守的版本 5.9.9, mysql 5.7.18-log 必須能打印的這樣的一句話,否則其他都不要繼續下去了
( "QSQLITE" , "QMYSQL" , "QMYSQL3" , "QODBC" , "QODBC3" , "QPSQL" , "QPSQL7" )
能夠看到 QMYSQL 首先畫UI界面 我說一下布局,姓名和lineEdit 首先水平布局,其他類似,右邊縱列的幾個功能鍵,ctrl按住,選中那幾個按鈕,進行豎直布局,選中整個Widget進行 柵格布局,想自適應,弄幾個彈簧,就可以自適應了。 數據庫的建表語句
create table stu
(
id
int ( 10 ) primary key auto_increment ,
` name
` varchar ( 255 ) ,
` stuid
` bigint ,
score
double
)
select * from stu
;
insert into stu
value ( NULL , '大軍' , 20141111088 , 99.5 ) ;
insert into stu
value ( NULL , '大刀' , 20141111011 , 88.4 ) ;
insert into stu
value ( NULL , '小紅' , 20141111088 , 94.3 ) ;
insert into stu
value ( NULL , '小劉' , 20141111077 , 92.4 ) ;
delete from stu
where id
= 2 ;
選擇UI界面,選中按鈕,右擊,轉到槽,找到click事件,單擊,槽函數自動給你創建好 其他的類似,qDebug一定用起來,打印信息的 核心代碼如下
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QSqlDatabase>
#include <QSqlError>
#include <QMessageBox>
#include <QSqlQuery> Widget
:: Widget ( QWidget
* parent
) : QWidget ( parent
) , ui ( new Ui
:: Widget
)
{ ui
- > setupUi ( this ) ; qDebug ( ) << QSqlDatabase
:: drivers ( ) ; QSqlDatabase db
= QSqlDatabase
:: addDatabase ( "QMYSQL" ) ; db
. setHostName ( "localhost" ) ; db
. setPort ( 3306 ) ; db
. setUserName ( "root" ) ; db
. setPassword ( "root" ) ; db
. setDatabaseName ( "demo" ) ; if ( ! db
. open ( ) ) { QMessageBox
:: warning ( this , "錯誤" , db
. lastError ( ) . text ( ) ) ; } QSqlQuery qu
; QString str
= "select * from user" ; qu
. exec ( str
) ; while ( qu
. next ( ) ) { qDebug ( ) << qu
. value ( 1 ) . toString ( ) ; qDebug ( ) << qu
. value ( 2 ) . toInt ( ) ; qDebug ( ) << qu
. value ( 3 ) . toString ( ) ; qDebug ( ) << qu
. value ( 4 ) . toString ( ) ; }
}
Widget
:: ~ Widget ( )
{ delete ui
;
} void Widget
:: on_pushButtonAdd_clicked ( )
{ QString name
= ui
- > lineEditName
- > text ( ) ; int stuId
= ui
- > lineEditStuId
- > text ( ) . toInt ( ) ; double score
= ui
- > lineEditScore
- > text ( ) . toDouble ( ) ; QString sql
= QString ( "insert into stu values(NULL,'%1', '%2', '%3')" ) . arg ( name
) . arg ( stuId
) . arg ( score
) ; QSqlQuery query
; if ( name
. isEmpty ( ) ) { QMessageBox
:: warning ( this , "error" , "請填寫姓名!" ) ; return ; } qDebug ( ) << stuId
; qDebug ( ) << score
; if ( stuId
== 0 ) { QMessageBox
:: warning ( this , "error" , "請填寫學號!" ) ; return ; } if ( score
== 0 ) { QMessageBox
:: warning ( this , "error" , "請填寫成績!" ) ; return ; } bool b
= query
. exec ( sql
) ; qDebug ( ) << b
; if ( b
) { ui
- > textEditArea
- > setText ( "數據插入成功" ) ; QMessageBox
:: information ( this , "success" , "數據插入成功" ) ; } else { ui
- > textEditArea
- > setText ( "數據插入失敗" ) ; QMessageBox
:: information ( this , "success" , query
. lastError ( ) . text ( ) ) ; }
} void Widget
:: on_pushButtonDele_clicked ( )
{ QString name
= ui
- > lineEditName
- > text ( ) ; if ( name
. isEmpty ( ) ) { QMessageBox
:: warning ( this , "error" , "請填寫姓名!" ) ; return ; } QSqlQuery query
; QString str
= QString ( "delete from stu where name = '%1'" ) . arg ( name
) ; bool s
= query
. exec ( str
) ; qDebug ( ) << s
<< "bbb" ; if ( s
) { ui
- > textEditArea
- > setText ( "刪除成功" ) ; QMessageBox
:: information ( this , "success" , QString ( "%1數據刪除成功" ) . arg ( name
) ) ; ui
- > lineEditName
- > setText ( "" ) ; } else { QMessageBox
:: warning ( this , "success" , QString ( "%1數據刪除失敗" ) . arg ( name
) ) ; ui
- > lineEditName
- > setText ( "" ) ; }
} void Widget
:: on_pushButtonModify_clicked ( )
{ QSqlQuery query
; QString updatename
= ui
- > lineEditName
- > text ( ) ; long number
= ui
- > lineEditStuId
- > text ( ) . toLong ( ) ; double score
= ui
- > lineEditScore
- > text ( ) . toDouble ( ) ; QString temp
= QString ( "update stu set stuid = '%1' , score = '%2' where name = '%3'" ) . arg ( number
) . arg ( score
) . arg ( updatename
) ; query
. exec ( temp
) ; ui
- > textEditArea
- > setText ( "修改成功" ) ;
} void Widget
:: on_pushButtonFind_clicked ( )
{ QString searchname
= ui
- > lineEditName
- > text ( ) ; if ( searchname
== NULL ) { ui
- > textEditArea
- > setText ( "請輸入需要查詢的人名" ) ; } else { QString str
= QString ( "select * from stu where name = '%1'" ) . arg ( searchname
) ; QSqlQuery query
; query
. exec ( str
) ; QString name
; int number
; double score
; while ( query
. next ( ) ) { number
= query
. value ( 0 ) . toInt ( ) ; name
= query
. value ( 1 ) . toString ( ) ; score
= query
. value ( 2 ) . toDouble ( ) ; } if ( name
== NULL ) { QString a
= QString ( "沒有叫%1的人,請重新輸入人名" ) . arg ( searchname
) ; ui
- > textEditArea
- > setText ( a
) ; ui
- > lineEditName
- > clear ( ) ; ui
- > lineEditStuId
- > clear ( ) ; ui
- > lineEditScore
- > clear ( ) ; } else { ui
- > lineEditName
- > setText ( name
) ; ui
- > lineEditStuId
- > setText ( QString ( ) . setNum ( number
) ) ; ui
- > lineEditScore
- > setText ( QString ( ) . setNum ( score
) ) ; ui
- > textEditArea
- > setText ( "查詢成功" ) ; } }
} void Widget
:: on_pushButtonFindAll_clicked ( )
{ QString name
[ 100 ] ; int number
[ 100 ] ; double score
[ 100 ] ; int i
= 0 ; QSqlQuery query
; query
. exec ( "select * from stu" ) ; while ( query
. next ( ) ) { number
[ i
] = query
. value ( 0 ) . toInt ( ) ; name
[ i
] = query
. value ( 1 ) . toString ( ) ; score
[ i
] = query
. value ( 2 ) . toDouble ( ) ; i
++ ; } ui
- > textEditArea
- > clear ( ) ; int j
= 0 ; for ( j
= 0 ; j
< i
; j
++ ) { QString str
= QString ( "學號:%1 姓名:%2 成績:%3" ) . arg ( number
[ j
] ) . arg ( name
[ j
] ) . arg ( score
[ j
] ) ; ui
- > textEditArea
- > append ( str
) ; }
}
總結
以上是生活随笔 為你收集整理的关于Qt的CRUD增删改查数据库那些事,带GUI图像界面 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。