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

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

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

// c 获取每条记录的每个字段
#include 
#include 

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 
#include 
#include 
#include 
#include 

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语言,c++,mysql)