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 \n");
31         fprintf(stderr, " %s\n", mysql_error(&mysql));
32         exit(1);
33     }
34     else
35     {
36         fprintf(stderr, "connect ok!!\n");
37     }
38 
39     if (mysql_query(&mysql, i_query) != 0) //如果连接成功,则开始查询 .成功返回0
40     {
41         fprintf(stderr, "fail to query!\n");
42         exit(1);
43     }
44     else
45     {
46         if ((result = mysql_store_result(&mysql)) == NULL) //保存查询的结果 
47         {
48             fprintf(stderr, "fail to store result!\n");
49             exit(1);
50         }
51         else
52         {
53             while ((row = mysql_fetch_row(result)) != NULL) //读取结果集中的数据,返回的是下一行。因为保存结果集时,当前的游标在第一行【之前】 
54             {
55                 printf("name is %s\t", row[0]); //打印当前行的第一列的数据 
56                 printf("id is %s\t\n", row[1]); //打印当前行的第二列的数据
57                 printf("math is %s\t\n", row[2]);
58                 printf("province is %s\t\n", row[3]);
59                 //row = mysql_num_row(result);
60                 //printf("%lu\n", 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

相关文章:

  • 2021-12-27
  • 2021-08-16
  • 2021-11-21
  • 2021-10-12
  • 2021-05-27
  • 2021-10-14
猜你喜欢
  • 2022-01-13
  • 2021-04-08
  • 2021-04-15
相关资源
相似解决方案