32bit的数据库安装包,qt位数要和数据库位数一样,如果有兼容问题(也就是最后一步无响应)就需要在配置exe的程序中使用兼任模式
    mysql-5.5.56-win32.msi

    数据库操作语句

    1. -- 创建
    2. create database info;
    3. -- 删除数据库info
    4. drop database info;
    5. -- 使用数据库 info
    6. use info;
    7. -- 创建表
    8. create table student(id int primary key auto_increment, name varchar(255), age int, score int);
    9. create table student(id int primary key, name varchar(255), age int, score int);
    10. -- 删除student
    11. drop table student;
    12. -- 插入数据
    13. insert into student(id, name, age, score) values(1, 'mike', 18, 59);
    14. insert into student(id, name, age, score) values(2, 'lucy', 25, 36);
    15. insert into student(id, name, age, score) values(1, 'Tom', 35, 25);
    16. -- 更新数据
    17. update student set score = 90 where id = 3;
    18. -- 查找数据
    19. select * from student;
    20. select * from set student where name = 'mike';
    21. -- 删除数据库
    22. delete from student where name = 'mike';

    .pro文件

    1. QT += core gui sql

    widget.h文件

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include <QWidget>
    4. namespace Ui {
    5. class Widget;
    6. }
    7. class Widget : public QWidget
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit Widget(QWidget *parent = 0);
    12. ~Widget();
    13. private slots:
    14. void on_remove_clicked();
    15. void on_scureDelete_clicked();
    16. void on_cannot_clicked();
    17. private:
    18. Ui::Widget *ui;
    19. };
    20. #endif // WIDGET_H

    widget.cpp文件

    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. #include <QSqlDatabase>
    4. #include <QDebug>
    5. #include <QMessageBox>
    6. #include <QSqlError>
    7. #include <QSqlQuery>
    8. #include <QVariantList>
    9. Widget::Widget(QWidget *parent) :
    10. QWidget(parent),
    11. ui(new Ui::Widget)
    12. {
    13. ui->setupUi(this);
    14. // 打印Qt支持的数据库驱动
    15. qDebug() << QSqlDatabase::drivers();
    16. // 添加MySql数据库
    17. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    18. // 连接数据库
    19. db.setHostName("127.0.0.1"); // 数据库服务器IP
    20. db.setUserName("root"); // 数据库用户名
    21. db.setPassword("123456"); //数据库密码
    22. db.setDatabaseName("test"); // 使用哪个数据库
    23. // 使数据库支持中文
    24. // query.exec("SET NAMES 'Latin1'");
    25. // 打开数据库
    26. if (!db.open()) // 数据库打开失败
    27. {
    28. QMessageBox::warning(this, "错误", db.lastError().text());
    29. return;
    30. }else {
    31. // while(query.next())
    32. // {
    33. // qDebug() << query.
    34. // }
    35. QSqlQuery query(db); // 以下执行相关QSL语句
    36. /************
    37. // 创建表
    38. query.exec("create table student(id int primary key auto_increment, name varchar(255), age int, score int);");
    39. ************/
    40. /************
    41. // 插入数据
    42. query.exec("insert into student(id, name, age, score) values(1, 'mike', 18, 59);");
    43. ************/
    44. /***********
    45. // 批量插入
    46. // odbc风格
    47. // 预处理语句 ?号相当于占位符
    48. query.prepare("insert into student(name, age, score) values(?, ?, ?);");
    49. // 给字段设置内容 list
    50. QVariantList nameList;
    51. nameList << "小明" << "小龙" << "小江";
    52. QVariantList ageList;
    53. ageList << 11 << 22 << 33;
    54. QVariantList scoreList;
    55. scoreList << 55 << 85 << 75;
    56. // 给字段绑定相应的值 按顺序绑定
    57. query.addBindValue(nameList);
    58. query.addBindValue(ageList);
    59. query.addBindValue(scoreList);
    60. // 执行预处理命令
    61. query.execBatch();
    62. ************/
    63. /***********
    64. // oracle风格
    65. // 占位符 : + 自定义名字
    66. query.prepare("insert into student(name, age, score) values(:name, :age, :score);");
    67. // 给字段设置内容 list
    68. QVariantList nameList;
    69. nameList << "小明2" << "小龙2" << "小江2";
    70. QVariantList ageList;
    71. ageList << 34 << 45 << 36;
    72. QVariantList scoreList;
    73. scoreList << 5 << 25 << 75;
    74. // 给字段绑定
    75. query.bindValue(":name", nameList);
    76. query.bindValue(":age", ageList);
    77. query.bindValue(":score", scoreList);
    78. // 执行预处理命令
    79. query.execBatch();
    80. ************/
    81. // 查数据
    82. query.exec("select * from student");
    83. while (query.next()) // 一行一行遍历
    84. {
    85. // 取出当前行的内容
    86. qDebug() << query.value(0).toInt()
    87. << query.value(1).toString()
    88. << query.value("age").toInt()
    89. << query.value("score").toInt();
    90. }
    91. }
    92. }
    93. Widget::~Widget()
    94. {
    95. delete ui;
    96. }
    97. // 删除按钮
    98. void Widget::on_remove_clicked()
    99. {
    100. // 获取行编辑内容
    101. QString name = ui->editName->text();
    102. // 删除的sq语句
    103. QString sql = QString("delete from student where name = '%1';").arg(name);
    104. // 开启一个事务
    105. qDebug() << QSqlDatabase::database().transaction(); // 获取当前操作的数据库 (database单例模式)
    106. QSqlQuery query;
    107. qDebug() << query.exec(sql);
    108. }
    109. // 确定删除按钮
    110. void Widget::on_scureDelete_clicked()
    111. {
    112. // 确定删除
    113. qDebug() << QSqlDatabase::database().commit();
    114. }
    115. // 取消按钮
    116. void Widget::on_cannot_clicked()
    117. {
    118. // 回滚、撤销
    119. qDebug() << QSqlDatabase::database().rollback();
    120. }

    image.png

    文件
    image.png
    source_file.zip