[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
QT连mysql数据库,大家装的数据库普遍都是8.0的版本,64位的,这里直接用QMYSQL连接,发现缺少驱动,而且我们用的Qt
creater是32位,所以这里选择另一种方式去链接数据库通过QODBC(开放数据库连接),如果你们用的64的mingw,或者是msvc_2013,64位的,那么就可以不用这个方法,直接连就可以了
首先,在自己的数据库中找到这2个文件
放在目录下
点击控制面板找到这个图标
随后点击ODBC Data Sources (32-bit)
配置
点击Test可以出现下窗口则连接成功
下面进行代码检测
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("test"); if(!db.open()){ qDebug()<<"打开失败"; }else{
qDebug()<<"打开成功"; } QSqlQuery query(db); //关联数据库 query.exec("select * from
teacher"); //执行语句用的 while(query.next()) {
qDebug()<<query.value(0).toString()<<" " <<query.value(1).toString()<<" "
<<query.value(2).toString(); }
效果如下
如果是msvc 64位,或者是migw64的
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1"); db.setPort(3306);//是mysql服务器的端口号
db.setDatabaseName("test"); db.setUserName("root"); db.setPassword("123456");
if (!db.open()) { qDebug() << QString::fromLocal8Bit("打开失败"); return 0; } else{
qDebug() << QString::fromLocal8Bit("打开成功"); } QSqlQuery query(db); //关联数据库
query.exec("select * from 。。。。;"); //使用查询语句 while (query.next()) { /* 对查询结果进行输出
*/ }
小结:
里面的本机信号和之后的127.0.0.1之后会在网络编程篇章继续详解原因,有了数据库的帮助之后可以更好的处理许多问题。