c/c++| mysql | 获取查询每条查询结果的每个字段

2024-01-24 12:36

本文主要是介绍c/c++| mysql | 获取查询每条查询结果的每个字段,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原来 对于 mysql 面向 c/c++ 有两套API

#####################################################

// c 获取每条记录的每个字段
#include <mysql/mysql.h>
#include <iostream>int main() {MYSQL mysql;MYSQL_RES *result;mysql_init(&mysql);mysql_real_connect(&mysql, "localhost", "user", "password", "database", 3306, nullptr, 0);mysql_query(&mysql, "SELECT * FROM your_table");result = mysql_store_result(&mysql);if (result) {MYSQL_ROW row;while ((row = mysql_fetch_row(result))) {// 遍历当前行的每一列for (unsigned int i = 0; i < mysql_num_fields(result); ++i) {// 打印列值std::cout << "列 " << i << ": " << (row[i] ? row[i] : "NULL") << " ";}std::cout << std::endl;}mysql_free_result(result);} else {std::cerr << "查询失败:" << mysql_error(&mysql) << std::endl;}mysql_close(&mysql);return 0;
}

####################################################

//	c++ 获取每条记录的每个字段
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/statement.h>
#include <cppconn/resultset.h>
#include <cppconn/prepared_statement.h>int main() {sql::mysql::MySQL_Driver *driver;sql::Connection *con;sql::Statement *stmt;sql::ResultSet *res;try {// 创建 MySQL 连接driver = sql::mysql::get_mysql_driver_instance();con = driver->connect("tcp://127.0.0.1:3306", "username", "password");// 选择数据库con->setSchema("your_database");// 创建查询语句stmt = con->createStatement();res = stmt->executeQuery("SELECT * FROM your_table");// 遍历每一行while (res->next()) {// 获取当前行的每一列int col1 = res->getInt(1);  // 假设第一列是整数std::string col2 = res->getString(2);  // 假设第二列是字符串// 打印当前行的每一列std::cout << "Column 1: " << col1 << ", Column 2: " << col2 << std::endl;}delete res;delete stmt;delete con;} catch (sql::SQLException &e) {std::cerr << "SQL Exception: " << e.what() << std::endl;}return 0;
}

#############################################

#############################################
############这两套api 的区别	
中文简单回答一下:主要讲用了两套api 一个是面向编程 c 一个是面向对象 c++ 
一个是通过数组获取每个字段  一个是通过getInt() 方式获取每个字段  前提是需要知道每个字段的类型Library and API Used:The first example uses the MySQL C API, which is a lower-level C API provided by MySQL for interacting with MySQL databases in the C programming language.
The second example uses the MySQL C++ Connector (MySQL Connector/C++), which is a higher-level C++ API designed for C++ applications interacting with MySQL databases.
Data Types:In the first example using the MySQL C API, the data is retrieved as an array of strings (MYSQL_ROW), and each element of the array corresponds to a column value in the current row.
In the second example using the MySQL C++ Connector, the data is retrieved using specific methods like getInt and getString, and the types of the returned values depend on the actual data types of the columns in the result set.
Memory Management:In the MySQL C API example, memory management, including the allocation and deallocation of memory for the result set (MYSQL_RES) and row data (MYSQL_ROW), is done manually using functions like mysql_store_result, mysql_fetch_row, and mysql_free_result.
In the MySQL C++ Connector example, memory management is handled more automatically by the C++ Connector library, reducing the need for manual memory management.
Exception Handling:The MySQL C++ Connector example includes exception handling using try and catch, which allows the program to handle potential exceptions thrown by the MySQL C++ Connector library.
The MySQL C API example lacks exception handling, and error checking is done using return values, which may be less convenient for error handling in some scenarios.
Object-Oriented Approach:The MySQL C++ Connector example follows a more object-oriented approach, with objects like sql::Connection, sql::Statement, and sql::ResultSet. This aligns with C++ principles and makes the code more readable and maintainable.
In summary, the primary differences arise from the choice of library and API (C API vs. C++ Connector), the level of abstraction provided, and the programming paradigm employed (C vs. C++). The MySQL C++ Connector example offers a more modern and C++-friendly way of interacting with MySQL databases.#####################

参考 gpt
农夫山泉从不生产水,只是大自然的搬运工

这篇关于c/c++| mysql | 获取查询每条查询结果的每个字段的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/639712

相关文章

Python实现获取带合并单元格的表格数据

《Python实现获取带合并单元格的表格数据》由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,所以本文我们就来聊聊如何使用Python实现获取带合并单元格的表格数据吧... 由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,现将将封装成类,并通过调用list_exc

一文详解如何查看本地MySQL的安装路径

《一文详解如何查看本地MySQL的安装路径》本地安装MySQL对于初学者或者开发人员来说是一项基础技能,但在安装过程中可能会遇到各种问题,:本文主要介绍如何查看本地MySQL安装路径的相关资料,需... 目录1. 如何查看本地mysql的安装路径1.1. 方法1:通过查询本地服务1.2. 方法2:通过MyS

Mysql数据库中数据的操作CRUD详解

《Mysql数据库中数据的操作CRUD详解》:本文主要介绍Mysql数据库中数据的操作(CRUD),详细描述对Mysql数据库中数据的操作(CRUD),包括插入、修改、删除数据,还有查询数据,包括... 目录一、插入数据(insert)1.插入数据的语法2.注意事项二、修改数据(update)1.语法2.有

SQL Server中的PIVOT与UNPIVOT用法具体示例详解

《SQLServer中的PIVOT与UNPIVOT用法具体示例详解》这篇文章主要给大家介绍了关于SQLServer中的PIVOT与UNPIVOT用法的具体示例,SQLServer中PIVOT和U... 目录引言一、PIVOT:将行转换为列核心作用语法结构实战示例二、UNPIVOT:将列编程转换为行核心作用语

SQL 外键Foreign Key全解析

《SQL外键ForeignKey全解析》外键是数据库表中的一列(或一组列),用于​​建立两个表之间的关联关系​​,外键的值必须匹配另一个表的主键(PrimaryKey)或唯一约束(UniqueCo... 目录1. 什么是外键?​​ ​​​​2. 外键的语法​​​​3. 外键的约束行为​​​​4. 多列外键​

MySQL精准控制Binlog日志数量的三种方案

《MySQL精准控制Binlog日志数量的三种方案》作为数据库管理员,你是否经常为服务器磁盘爆满而抓狂?Binlog就像数据库的“黑匣子”,默默记录着每一次数据变动,但若放任不管,几天内这些日志文件就... 目录 一招修改配置文件:永久生效的控制术1.定位my.cnf文件2.添加核心参数不重启热更新:高手应

MySQL中SQL的执行顺序详解

《MySQL中SQL的执行顺序详解》:本文主要介绍MySQL中SQL的执行顺序,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql中SQL的执行顺序SQL执行顺序MySQL的执行顺序SELECT语句定义SELECT语句执行顺序总结MySQL中SQL的执行顺序

C++类和对象之初始化列表的使用方式

《C++类和对象之初始化列表的使用方式》:本文主要介绍C++类和对象之初始化列表的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C++初始化列表详解:性能优化与正确实践什么是初始化列表?初始化列表的三大核心作用1. 性能优化:避免不必要的赋值操作2. 强

MySQL中like模糊查询的优化方案

《MySQL中like模糊查询的优化方案》在MySQL中,like模糊查询是一种常用的查询方式,但在某些情况下可能会导致性能问题,本文将介绍八种优化MySQL中like模糊查询的方法,需要的朋友可以参... 目录1. 避免以通配符开头的查询2. 使用全文索引(Full-text Index)3. 使用前缀索

MySQL中的两阶段提交详解(2PC)

《MySQL中的两阶段提交详解(2PC)》:本文主要介绍MySQL中的两阶段提交(2PC),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录引言两阶段提交过程sync_binlog配置innodb_flush_log_at_trx_commit配置总结引言在Inn