怎么用c++导出mysql数据库的数据

2025-05-22 22:51:10
推荐回答(2个)
回答(1):

1、用CAPI连接MySQL数据库有两个步骤:
1)初始化一个连接句柄
2)建立连接
所用到的函数如下:
MYSQL *mysql_init(MYSQL *connection); // 初始化连接句柄
//成功返回MySQL结构指针,失败返回NULL

MYSQL *mysql_real_connect(MYSQL *connection,
const char *server_host,
const char *sql_user_name,
const char *sql_password,
const char *db_name,
unsigned int port_number,
const char *unix_socket_name,
unsigned int flags); //建立连接
//成功返回MySQL结构指针,失败返回NULL
以下是完整实例:

#include
#include
#include
#include

using namespace std;

void mysql_err_function(MYSQL * connection);

int main()
{
//freopen("input.txt","r",stdin);

MYSQL * connection;
connection = mysql_init(NULL);

if (!connection)
{
cout << "mysql_init failed!" << endl;

回答(2):

通过执行select语句,得到数据后,写成逗号分隔的文本文件
~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~