利用qt对数据库进行操作

之前我也用过mysql数据库,那个时候我还没有接触过qt,现在我们可以通过图形界面的形式对数据库进行操作。

一、简单的mysql的语法

每条语句都以分号结尾。

创建一个数据库

格式为:create database 数据库名;

例:create database test;

创建一个表

格式为:create table 表名(内容 类型);

例:create table student(number int , name char(32), score double);

向表中插入信息

格式为:insert into 表名 values(表中的内容);

例:insert into student values(1, 'xiaoming', 99);

从表格中删除信息

格式为:delete from 表名 where 列名 = 条件;

例:delete  from student where name = 'xiaoming';

查询表中的内容

格式为:select 列名 from 表名 where 列名 = 条件;

例:select score from student where name = 'xiaoming';

我们可以用这样的语句查询所有的信息

select * from student;

修改表中的数据

格式为:update 表名 set 列名 = 新内容 where 列名 = 条件;

例:update student set score = 66 where name = 'xiaoming';

从数据库中删除一个表

格式为:drop table 表名;

例:drop table student;

二、使用qt创建图形界面对数据库进行操作

首先我们先用qt creator 创建一个项目,这里我们使用图形界面,所以要勾选创建图形界面,新建完成之后我们需要将其部好局,如下图所示。


我们如果要使用数据库就需要在pro 文件中加上这句话 QT += sql。

首先我们需要自己创建一个数据库,然后利用qt和数据库进行连接。


    
    
  1. db = QSqlDatabase::addDatabase( "QMYSQL"); //a
  2. db.setHostName( "127.0.0.1"); //b
  3. db.setDatabaseName( "test"); //c
  4. db.setUserName( "root"); //d
  5. db.setPassword( "123456"); //e

以上的过程就是和数据库进行连接,a表示我们需要使用的是哪种数据库

b是设置主机名,c是设置用户名,表示我们使用哪个数据库, d是设置用户名,e是设置密码,这个密码是和数据库登录时候的密码一致。如果不一致,我们就不能成功和数据库进行连接。

如果连接成功我们就可以通过以上的按钮对数据库进行操作

 插入信息到数据库

右击插入转到槽,只要在这个槽函数中编写插入数据库的函数就行了。


    
    
  1. QString namestr = ui->lineEditName->text();
  2. int num = ui->lineEditNumber->text().toInt();
  3. double score = ui->lineEditScore->text().toDouble();
  4. QString str = QString( "insert into student(num, name, score) values('%1', '%2', '%3')").arg(num).arg(namestr).arg(score);
  5. QSqlQuery query;
  6. query.exec(str);
  7. ui->textEdit->setText( "插入成功");
以上的代码首先就是获取三个行编辑框中的内容并且将它们转换为相应的数据类型。然后我们使用数据库语句,将这些信息存到数据库中,如果存入成功,下方的文本编辑框会显示插入成功。

删除数据库中的信息

我们首先需要从界面中获取需要删除的人名,然后将利用数据库的语句将这条信息从数据库中删除。右击删除转到槽,我们只要在这个槽函数中编辑以上所说的功能就行了。


    
    
  1. QString name = ui->lineEditName->text(); //从行编辑框中获取需要删除的人名
  2. QString str = QString( "delete from student where name = '%1'").arg(name);
  3. query.exec(str);
  4. ui->textEdit->setText( "删除成功");

这个就是从数据库中删除我们希望删除的信息,删除完成之后下方的文本编辑框就会提示相应的信息。

修改数据库中的内容

这个步骤和删除的步骤差不多,首先需要从界面中获取需要修改的信息,然后利用数据库语句根据条件修改信息


    
    
  1. //从界面获取我们需要的信息
  2. QString updatename = ui->lineEditName->text();
  3. int number = ui->lineEditNumber->text().toInt();
  4. double score = ui->lineEditScore->text().toDouble();
  5. temp = QString( "update student set num = '%1' , score = '%2' where name = '%3'").arg(number).arg(score).arg(updatename);
  6. query.exec(temp); //执行修改信息的操作
  7. ui->textEdit->setText( "修改成功");

和之前的一样,我们在修成完成之后,会在下方的文本编辑框中显示修改成功的字样。

根据条件查询数据库的信息

我这里写的就是根据一个人的名字来查询他的所有信息,包括名字,学号,成绩,然后让其显示在相应的行编辑中。


    
    
  1. QString searchname = ui->lineEditName->text();
  2. QString str = QString( "select *from student where name = '%1'").arg(searchname);
  3. QSqlQuery query;
  4. query.exec(str);
  5. QString name;
  6. int number;
  7. double score;
  8. while (query.next())
  9. {
  10. number = query.value( 0).toInt();
  11. name = query.value( 1).toString();
  12. score = query.value( 2).toDouble();
  13. }
  14. ui->lineEditName->setText(name);
  15. ui->lineEditNumber->setText(QString().setNum(number));
  16. ui->lineEditScore->setText(QString().setNum(score));
  17. ui->textEdit->setText( "查询成功");

对数据库查询的操作是这样的,数据库会根据数据库语句一行一行的查询,查询到复合条件的内容就会停止,我们只要将数据库中的查询出来的东西转为相应的数据类型就行了,然后将它们显示在相应的行编辑框中就行了,同样的如果查询成功就会在下方的文本编辑框中显示查询成功的字样。

三、以下是我的代码和界面

下面的代码我加了一些出错处理,以及提示信息。


    
    
  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QTextCodec>
  4. #include <QMessageBox>
  5. #include <QDebug>
  6. Widget::Widget(QWidget *parent) :
  7. QWidget(parent),
  8. ui( new Ui::Widget)
  9. {
  10. ui->setupUi( this);
  11. qDebug() << QSqlDatabase::drivers(); //打印qt支持的数据库类型
  12. setWindowTitle( "SQL"); //设置窗口的标题
  13. QTextCodec::setCodecForLocale(QTextCodec::codecForLocale()); //设置显示中文
  14. db = QSqlDatabase::addDatabase( "QMYSQL");
  15. db.setHostName( "127.0.0.1");
  16. db.setDatabaseName( "test");
  17. db.setUserName( "root");
  18. db.setPassword( "123456");
  19. if(!db.open()) //如果数据库打开失败,会弹出一个警告窗口
  20. {
  21. QMessageBox::warning( this, "警告", "数据库打开失败");
  22. }
  23. else
  24. {
  25. ui->textEdit->setText( "数据库打开成功");
  26. }
  27. // QString str = "create table student(num int, name varchar(32), score double);";
  28. // QSqlQuery query;
  29. // query.exec(str);
  30. }
  31. Widget::~Widget()
  32. {
  33. delete ui;
  34. }
  35. void Widget::on_pushButtonInsert_clicked()
  36. {
  37. QString namestr = ui->lineEditName->text();
  38. int num = ui->lineEditNumber->text().toInt();
  39. double score = ui->lineEditScore->text().toDouble();
  40. if(namestr == NULL || num == 0 || ui->lineEditScore == NULL) //插入信息的时候需要输入完整的信息
  41. {
  42. ui->textEdit->setText( "请输入完整的信息");
  43. }
  44. else
  45. {
  46. QString str = QString( "insert into student(num, name, score) values('%1', '%2', '%3')").arg(num).arg(namestr).arg(score);
  47. QSqlQuery query;
  48. query.exec(str); //执行插入操作
  49. ui->lineEditName->clear();
  50. ui->lineEditNumber->clear();
  51. ui->lineEditScore->clear();
  52. ui->textEdit->setText( "插入成功");
  53. }
  54. }
  55. void Widget::on_pushButtonDelete_clicked()
  56. {
  57. QString name = ui->lineEditName->text(); //从行编辑框中获取需要删除的人名
  58. if(name == NULL)
  59. {
  60. ui->textEdit->setText( "请输入需要删除的人的名字"); //删除的时候需要输入姓名
  61. }
  62. else
  63. {
  64. //从数据库中查询是否有这个人
  65. QSqlQuery query;
  66. QString temp = QString( "select * from student where name = '%1'").arg(name);
  67. query.exec(temp);
  68. QString deletename;
  69. while (query.next())
  70. {
  71. deletename = query.value( 1).toString();
  72. }
  73. if(deletename == NULL)
  74. {
  75. QString a = QString( "没有叫%1的人,删除失败").arg(name);
  76. ui->textEdit->setText(a);
  77. ui->lineEditName->clear();
  78. ui->lineEditNumber->clear();
  79. ui->lineEditScore->clear();
  80. }
  81. else
  82. {
  83. QString str = QString( "delete from student where name = '%1'").arg(name);
  84. query.exec(str); //删除信息
  85. ui->lineEditName->clear();
  86. ui->lineEditNumber->clear();
  87. ui->lineEditScore->clear();
  88. ui->textEdit->setText( "删除成功");
  89. }
  90. }
  91. }
  92. void Widget::on_pushButtonSearch_clicked()
  93. {
  94. QString searchname = ui->lineEditName->text();
  95. if(searchname == NULL)
  96. {
  97. ui->textEdit->setText( "请输入需要查询的人名");
  98. }
  99. else
  100. {
  101. //从数据库中查询是否有这么一个人
  102. QString str = QString( "select *from student where name = '%1'").arg(searchname);
  103. QSqlQuery query;
  104. query.exec(str);
  105. QString name;
  106. int number;
  107. double score;
  108. while (query.next())
  109. {
  110. number = query.value( 0).toInt();
  111. name = query.value( 1).toString();
  112. score = query.value( 2).toDouble();
  113. }
  114. if(name == NULL)
  115. {
  116. QString a = QString( "没有叫%1的人,请重新输入人名").arg(searchname);
  117. ui->textEdit->setText(a);
  118. ui->lineEditName->clear();
  119. ui->lineEditNumber->clear();
  120. ui->lineEditScore->clear();
  121. }
  122. else
  123. {
  124. ui->lineEditName->setText(name);
  125. ui->lineEditNumber->setText(QString().setNum(number));
  126. ui->lineEditScore->setText(QString().setNum(score));
  127. ui->textEdit->setText( "查询成功");
  128. }
  129. }
  130. }
  131. void Widget::on_pushButtonSearchAll_clicked()
  132. {
  133. QString name[ 100]; //用来存储从数据库中找出来的信息
  134. int number[ 100];
  135. double score[ 100];
  136. int i = 0;
  137. QSqlQuery query;
  138. query.exec( "select * from student"); //查询所有的信息
  139. while(query.next())
  140. {
  141. number[i] = query.value( 0).toInt();
  142. name[i] = query.value( 1).toString();
  143. score[i] = query.value( 2).toDouble();
  144. i++;
  145. }
  146. ui->textEdit->clear();
  147. int j = 0;
  148. for(j = 0; j < i; j++) //将这些信息都显示在下方的文本编辑框中
  149. {
  150. QString str = QString( "学号:%1 姓名:%2 成绩:%3").arg(number[j]).arg(name[j]).arg(score[j]);
  151. ui->textEdit->append(str);
  152. }
  153. }
  154. void Widget::on_pushButtonUpdate_clicked()
  155. {
  156. //从界面获取我们需要的信息
  157. QString updatename = ui->lineEditName->text();
  158. int number = ui->lineEditNumber->text().toInt();
  159. double score = ui->lineEditScore->text().toDouble();
  160. if(updatename == NULL || number == 0 || ui->lineEditScore->text() == NULL)
  161. {
  162. ui->textEdit->setText( "请输入需要修改的人的学号,姓名以及成绩");
  163. }
  164. else
  165. {
  166. QString temp = QString( "select * from student where name = '%1'").arg(updatename);
  167. QSqlQuery query;
  168. query.exec(temp); // 查询信息
  169. QString a;
  170. while (query.next())
  171. {
  172. a = query.value( 1).toString();
  173. }
  174. if(a == NULL)
  175. {
  176. QString b = QString( "没有名叫%1的人,修改失败").arg(updatename);
  177. ui->textEdit->setText(b);
  178. ui->lineEditName->clear();
  179. ui->lineEditNumber->clear();
  180. ui->lineEditScore->clear();
  181. }
  182. else
  183. {
  184. temp = QString( "update student set num = '%1' , score = '%2' where name = '%3'").arg(number).arg(score).arg(updatename);
  185. query.exec(temp);
  186. ui->textEdit->setText( "修改成功");
  187. ui->lineEditName->clear();
  188. ui->lineEditNumber->clear();
  189. ui->lineEditScore->clear();
  190. }
  191. }
  192. }
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值