zoukankan      html  css  js  c++  java
  • vs整合MySQL和QT

    23:37:23 2019-08-12

    尝试用vs写一个程序整合MySQL和QT

    参考资料:https://blog.csdn.net/qq_35987486/article/details/84066304

    https://www.cnblogs.com/Enceladus/p/11197971.html

    先在vs中创建一个qt项目 然后手动写MySQL

    下面这个是手动配置 MySQL

     1 #include <stdio.h> 
     2 #include <stdlib.h> 
     3 #include<Windows.h>
     4 #include<mysql.h>
     5 #include<iostream>
     6 #pragma comment(lib, "libmysql.lib")
     7 
     8 int main(void)
     9 {
    10     MYSQL mysql, * sock; //声明MySQL的句柄 
    11     const char* host = "127.0.0.1"; //因为是作为本机测试,所以填写的是本地IP 
    12     const char* user = "root"; //这里改为你的用户名,即连接MySQL的用户名 
    13     const char* passwd = ""; //这里改为你的用户密码 
    14     const char* db = "text"; //这里改为你要连接的数据库的名字,一个数据可能有几张表
    15     unsigned int port = 3306; //这是MySQL的服务器的端口,如果你没有修改过的话就是3306。 
    16     const char* unix_socket = NULL; //unix_socket这是unix下的,我在Windows下,所以就把它设置为NULL 
    17     unsigned long client_flag = 0; //这个参数一般为0 
    18 
    19     const char* i_query = "select * from datastudent"; //查询语句,从那个表中查询,这里后面没有;
    20     
    21 
    22     MYSQL_RES* result; //保存结果集的
    23     MYSQL_ROW row; //代表的是结果集中的一行 
    24                    //my_ulonglong row;
    25 
    26     mysql_init(&mysql); //连接之前必须使用这个函数来初始化 
    27     mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "gbk");
    28     if ((sock = mysql_real_connect(&mysql, host, user, passwd, db, port, unix_socket, client_flag)) == NULL) //连接MySQL 
    29     {
    30         printf("fail to connect mysql 
    ");
    31         fprintf(stderr, " %s
    ", mysql_error(&mysql));
    32         exit(1);
    33     }
    34     else
    35     {
    36         fprintf(stderr, "connect ok!!
    ");
    37     }
    38 
    39     if (mysql_query(&mysql, i_query) != 0) //如果连接成功,则开始查询 .成功返回0
    40     {
    41         fprintf(stderr, "fail to query!
    ");
    42         exit(1);
    43     }
    44     else
    45     {
    46         if ((result = mysql_store_result(&mysql)) == NULL) //保存查询的结果 
    47         {
    48             fprintf(stderr, "fail to store result!
    ");
    49             exit(1);
    50         }
    51         else
    52         {
    53             while ((row = mysql_fetch_row(result)) != NULL) //读取结果集中的数据,返回的是下一行。因为保存结果集时,当前的游标在第一行【之前】 
    54             {
    55                 printf("name is %s	", row[0]); //打印当前行的第一列的数据 
    56                 printf("id is %s	
    ", row[1]); //打印当前行的第二列的数据
    57                 printf("math is %s	
    ", row[2]);
    58                 printf("province is %s	
    ", row[3]);
    59                 //row = mysql_num_row(result);
    60                 //printf("%lu
    ", mysql_num_row(result));
    61             }
    62         }
    63 
    64     }
    65     mysql_free_result(result); //释放结果集 
    66     mysql_close(sock); //关闭连接 
    67     system("pause");
    68     exit(EXIT_SUCCESS);
    69 
    70 }
    View Code

     把建的qt项目代码和这个写在一起就能跑了

    我用QT designer画好了界面 

    但MySQL的配置是在main函数中  qt改变界面上元素属性是需要用 ui指针的 

    为了在main函数中使用 我把.h文件里的ui指针 改为了public 这样可在main函数中访问到ui指针

    头文件 .h

     1 #pragma once
     2 #include <QtWidgets/QWidget>
     3 #include "ui_text.h"
     4 
     5 class text : public QWidget
     6 {
     7     Q_OBJECT
     8 
     9 public:
    10     text(QWidget *parent = Q_NULLPTR);
    11     Ui::textClass ui;
    12 };

     .cpp  没有修改

    main.cpp

     1 #include "text.h"
     2 #include <QtWidgets/QApplication>
     3 #include <stdio.h> 
     4 #include <stdlib.h> 
     5 #include<Windows.h>
     6 #include<mysql.h>
     7 #include<qtextcodec.h>
     8 #pragma comment(lib, "libmysql.lib")
     9 
    10 int main(int argc, char* argv[])
    11 {
    12     MYSQL mysql, * sock; //声明MySQL的句柄 
    13     const char* host = "127.0.0.1"; //因为是作为本机测试,所以填写的是本地IP 
    14     const char* user = "root"; //这里改为你的用户名,即连接MySQL的用户名 
    15     const char* passwd = ""; //这里改为你的用户密码 
    16     const char* db = "text"; //这里改为你要连接的数据库的名字,一个数据可能有几张表
    17     unsigned int port = 3306; //这是MySQL的服务器的端口,如果你没有修改过的话就是3306。 
    18     const char* unix_socket = NULL; //unix_socket这是unix下的,我在Windows下,所以就把它设置为NULL 
    19     unsigned long client_flag = 0; //这个参数一般为0 
    20 
    21     const char* i_query = "select * from datastudent"; //查询语句,从那个表中查询,这里后面没有;
    22 
    23 
    24     MYSQL_RES* result; //保存结果集的
    25     MYSQL_ROW row; //代表的是结果集中的一行 
    26                    //my_ulonglong row;
    27     QString name;
    28     QString id;
    29     QString score;
    30     QString province;
    31     mysql_init(&mysql); //连接之前必须使用这个函数来初始化 
    32     mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "UTF8");
    33     if ((sock = mysql_real_connect(&mysql, host, user, passwd, db, port, unix_socket, client_flag)) == NULL) //连接MySQL 
    34     {
    35         printf("fail to connect mysql 
    ");
    36         fprintf(stderr, " %s
    ", mysql_error(&mysql));
    37         exit(1);
    38     }
    39     else
    40     {
    41         fprintf(stderr, "connect ok!!
    ");
    42     }
    43 
    44     if (mysql_query(&mysql, i_query) != 0) //如果连接成功,则开始查询 .成功返回0
    45     {
    46         fprintf(stderr, "fail to query!
    ");
    47         exit(1);
    48     }
    49     else
    50     {
    51         if ((result = mysql_store_result(&mysql)) == NULL) //保存查询的结果 
    52         {
    53             fprintf(stderr, "fail to store result!
    ");
    54             exit(1);
    55         }
    56         else
    57         {
    58             while ((row = mysql_fetch_row(result)) != NULL) //读取结果集中的数据,返回的是下一行。因为保存结果集时,当前的游标在第一行【之前】 
    59             {
    60                 printf("name is %s	", row[0]); //打印当前行的第一列的数据 
    61                 printf("id is %s	
    ", row[1]); //打印当前行的第二列的数据
    62                 printf("math is %s	
    ", row[2]);
    63                 printf("province is %s	
    ", row[3]);
    64                 name = row[0];
    65                 id = row[1];
    66                 score = row[2];
    67                 province = row[3];
    68                 //row = mysql_num_row(result);
    69                 //printf("%lu
    ", mysql_num_row(result));
    70             }
    71         }
    72 
    73     }
    74     mysql_free_result(result); //释放结果集 
    75     mysql_close(sock); //关闭连接 
    76 
    77     QApplication a(argc, argv);
    78     text w;
    79     w.ui.name->setText(name);
    80     w.ui.id->setText(id);
    81     w.ui.score->setText(score);
    82     w.ui.province->setText(province);
    83     w.show();
    84     return a.exec();
    85 
    86 }

     字体为红的地方是 修改MySQL编码格式为 UTF-8

     

     运行结果:

  • 相关阅读:
    解决PKIX:unable to find valid certification path to requested target 的问题
    Linux 上的常用文件传输方式介绍与比较
    用VNC远程图形化连接Linux桌面的配置方法
    红帽中出现”This system is not registered with RHN”的解决方案
    linux安装时出现your cpu does not support long mode的解决方法
    CentOS SSH配置
    es6扩展运算符及rest运算符总结
    es6解构赋值总结
    tortoisegit安装、clon、推送
    es6环境搭建
  • 原文地址:https://www.cnblogs.com/57one/p/11343333.html
Copyright © 2011-2022 走看看