鱼C论坛

 找回密码
 立即注册
查看: 1496|回复: 2

[已解决]c++打印数据库

[复制链接]
发表于 2023-3-16 17:19:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请问大佬们,这个我想打印数据库信息该怎么改写呢
  1. #include <stdio.h>
  2. #include <mysql.h>
  3. #include <string>
  4. #include <iostream>

  5. using namespace std;

  6. const char* host = "127.0.0.1";
  7. const char* user = "root";
  8. const char* pw = "123456a";
  9. const char* database_name = "爬虫";
  10. const int port = 3306;


  11. typedef struct Student {
  12.         int cid;
  13.         string captioon;
  14. }Student;

  15. int main(){
  16.         //初始化
  17.         MYSQL* con = mysql_init(NULL);
  18.         //设置编码
  19.         mysql_options(con,MYSQL_SET_CHARSET_NAME, "GBK");
  20.         //链接数据库
  21.         if (!mysql_real_connect(con, host, user, pw, database_name, port, NULL, 0)) {
  22.                 fprintf(stderr, "%s", mysql_error(con));
  23.                 return -1;
  24.         }

  25.         //定义sql语句
  26.         char sql[255];
  27.         sprintf(sql, "select * from class");

  28.         //执行sql语句
  29.         if (!mysql_query(con, sql)) {
  30.                 fprintf(stderr, "%s", mysql_error(con));
  31.                 return -1;
  32.         }

  33.         MYSQL_RES* res = mysql_store_result(con);

  34.         char tempStr[255];

  35.         MYSQL_ROW row;
  36.         while ((row = mysql_fetch_row(res))) {
  37.                 sprintf(tempStr, "%s", row[0]);
  38.                 printf(tempStr);



  39.         }

  40.         //关闭数据库
  41.         mysql_close(con);

  42.         return 0;
  43.        
  44. }
复制代码
最佳答案
2023-3-16 17:45:56
你好!你可以根据你的需求对代码进行一些修改,比如在打印行信息时,你可以将数据格式化为更易阅读的格式。以下是一个改进后的版本:

  1. #include <stdio.h>
  2. #include <mysql.h>
  3. #include <string>
  4. #include <iostream>

  5. using namespace std;

  6. const char* host = "127.0.0.1";
  7. const char* user = "root";
  8. const char* pw = "123456a";
  9. const char* database_name = "爬虫";
  10. const int port = 3306;

  11. typedef struct Student {
  12.     int cid;
  13.     string captioon;
  14. } Student;

  15. int main() {
  16.     // 初始化
  17.     MYSQL* con = mysql_init(NULL);
  18.     // 设置编码
  19.     mysql_options(con, MYSQL_SET_CHARSET_NAME, "utf8");
  20.     // 链接数据库
  21.     if (!mysql_real_connect(con, host, user, pw, database_name, port, NULL, 0)) {
  22.         fprintf(stderr, "%s\n", mysql_error(con));
  23.         return -1;
  24.     }

  25.     // 定义sql语句
  26.     char sql[] = "SELECT * FROM class";

  27.     // 执行sql语句
  28.     if (mysql_query(con, sql)) {
  29.         fprintf(stderr, "%s\n", mysql_error(con));
  30.         return -1;
  31.     }

  32.     MYSQL_RES* res = mysql_store_result(con);

  33.     MYSQL_ROW row;
  34.     MYSQL_FIELD *field;
  35.     unsigned int num_fields = mysql_num_fields(res);
  36.     while ((field = mysql_fetch_field(res))) {
  37.         cout << field->name << "\t";
  38.     }
  39.     cout << endl;

  40.     while ((row = mysql_fetch_row(res))) {
  41.         for (unsigned int i = 0; i < num_fields; ++i) {
  42.             if (row[i]) {
  43.                 printf("%s\t", row[i]);
  44.             } else {
  45.                 printf("NULL\t");
  46.             }
  47.         }
  48.         printf("\n");
  49.     }

  50.     // 释放结果集
  51.     mysql_free_result(res);

  52.     // 关闭数据库
  53.     mysql_close(con);

  54.     return 0;
  55. }
复制代码

注意:在这个版本中,我修改了字符编码设置为UTF-8,因为UTF-8兼容性更好,可以支持更多字符集。你可以根据你的实际情况进行调整。此外,我还添加了字段名的打印,以便更清楚地显示输出的数据。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-3-16 17:45:56 | 显示全部楼层    本楼为最佳答案   
你好!你可以根据你的需求对代码进行一些修改,比如在打印行信息时,你可以将数据格式化为更易阅读的格式。以下是一个改进后的版本:

  1. #include <stdio.h>
  2. #include <mysql.h>
  3. #include <string>
  4. #include <iostream>

  5. using namespace std;

  6. const char* host = "127.0.0.1";
  7. const char* user = "root";
  8. const char* pw = "123456a";
  9. const char* database_name = "爬虫";
  10. const int port = 3306;

  11. typedef struct Student {
  12.     int cid;
  13.     string captioon;
  14. } Student;

  15. int main() {
  16.     // 初始化
  17.     MYSQL* con = mysql_init(NULL);
  18.     // 设置编码
  19.     mysql_options(con, MYSQL_SET_CHARSET_NAME, "utf8");
  20.     // 链接数据库
  21.     if (!mysql_real_connect(con, host, user, pw, database_name, port, NULL, 0)) {
  22.         fprintf(stderr, "%s\n", mysql_error(con));
  23.         return -1;
  24.     }

  25.     // 定义sql语句
  26.     char sql[] = "SELECT * FROM class";

  27.     // 执行sql语句
  28.     if (mysql_query(con, sql)) {
  29.         fprintf(stderr, "%s\n", mysql_error(con));
  30.         return -1;
  31.     }

  32.     MYSQL_RES* res = mysql_store_result(con);

  33.     MYSQL_ROW row;
  34.     MYSQL_FIELD *field;
  35.     unsigned int num_fields = mysql_num_fields(res);
  36.     while ((field = mysql_fetch_field(res))) {
  37.         cout << field->name << "\t";
  38.     }
  39.     cout << endl;

  40.     while ((row = mysql_fetch_row(res))) {
  41.         for (unsigned int i = 0; i < num_fields; ++i) {
  42.             if (row[i]) {
  43.                 printf("%s\t", row[i]);
  44.             } else {
  45.                 printf("NULL\t");
  46.             }
  47.         }
  48.         printf("\n");
  49.     }

  50.     // 释放结果集
  51.     mysql_free_result(res);

  52.     // 关闭数据库
  53.     mysql_close(con);

  54.     return 0;
  55. }
复制代码

注意:在这个版本中,我修改了字符编码设置为UTF-8,因为UTF-8兼容性更好,可以支持更多字符集。你可以根据你的实际情况进行调整。此外,我还添加了字段名的打印,以便更清楚地显示输出的数据。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-3-16 18:14:13 | 显示全部楼层
isdkz 发表于 2023-3-16 17:45
你好!你可以根据你的需求对代码进行一些修改,比如在打印行信息时,你可以将数据格式化为更易阅读的格式。 ...

好的好的,感谢感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 17:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表