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

相关文章

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

MySQL中的锁机制详解之全局锁,表级锁,行级锁

《MySQL中的锁机制详解之全局锁,表级锁,行级锁》MySQL锁机制通过全局、表级、行级锁控制并发,保障数据一致性与隔离性,全局锁适用于全库备份,表级锁适合读多写少场景,行级锁(InnoDB)实现高并... 目录一、锁机制基础:从并发问题到锁分类1.1 并发访问的三大问题1.2 锁的核心作用1.3 锁粒度分

MySQL数据库中ENUM的用法是什么详解

《MySQL数据库中ENUM的用法是什么详解》ENUM是一个字符串对象,用于指定一组预定义的值,并可在创建表时使用,下面:本文主要介绍MySQL数据库中ENUM的用法是什么的相关资料,文中通过代码... 目录mysql 中 ENUM 的用法一、ENUM 的定义与语法二、ENUM 的特点三、ENUM 的用法1

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

mysql中的服务器架构详解

《mysql中的服务器架构详解》:本文主要介绍mysql中的服务器架构,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、mysql服务器架构解释3、总结1、背景简单理解一下mysqphpl的服务器架构。2、mysjsql服务器架构解释mysql的架

MySQL之InnoDB存储引擎中的索引用法及说明

《MySQL之InnoDB存储引擎中的索引用法及说明》:本文主要介绍MySQL之InnoDB存储引擎中的索引用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录1、背景2、准备3、正篇【1】存储用户记录的数据页【2】存储目录项记录的数据页【3】聚簇索引【4】二