c++ - 从我的程序连接到 mysql Web 数据库

标签 c++ mysql c xcode host

我正在尝试从我的程序连接到 mysql Web 数据库,但我不断收到:

Can't connect to MySQL server on 'mysql7.000webhost.com' (51) 

我正在使用这个网站的免费 mysql 服务器:

000webhost.com

这是我的代码或者是我在google中找到的开始学习的代码。

#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>

// just going to input the general details and not the port numbers
struct connection_details
{
    char *server;
    char *user;
    char *password;
    char *database;
};

MYSQL* mysql_connection_setup(struct connection_details mysql_details)
{
    // first of all create a mysql instance and initialize the variables within
    MYSQL *connection = mysql_init(NULL);

    // connect to the database with the details attached.
    if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)) {
        printf("Conection error : %s\n", mysql_error(connection));
        exit(1);
    }
    return connection;
}

MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
{
    // send the query to the database
    if (mysql_query(connection, sql_query))
    {
        printf("MySQL query error : %s\n", mysql_error(connection));
        exit(1);
    }

    return mysql_use_result(connection);
}

int main()
{
    MYSQL *conn;        // the connection
    MYSQL_RES *res; // the results
    MYSQL_ROW row;  // the results row (line by line)

    struct connection_details mysqlD;
    mysqlD.server = (char*)"mysql7.000webhost.com";  // where the mysql database is
    mysqlD.user =  (char*)"a1206305_test";      // the root user of mysql   
    mysqlD.password =  (char*)"testthis1"; // the password of the root user in mysql
    mysqlD.database =  (char*)"a1206305_test";  // the databse to pick

    // connect to the mysql database
    conn = mysql_connection_setup(mysqlD);

    // assign the results return to the MYSQL_RES pointer
    res = mysql_perform_query(conn, (char*) "show tables");

    printf("MySQL Tables in mysql database:\n");
    while ((row = mysql_fetch_row(res)) !=NULL)
        printf("%s\n", row[0]);

    /* clean up the database result set */
    mysql_free_result(res);
    /* clean up the database link */
    mysql_close(conn);

    return 0;
}

为什么无法连接?

最佳答案

如果您使用共享网络托管,mysql 服务器可能配置为拒绝外部连接。

关于c++ - 从我的程序连接到 mysql Web 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9585544/

相关文章:

c++ - 指向两种不同类型节点的指针

c++ - cmake 无法检测到 boost 多重安装

c++ - Libspotify 简单的 Hello World

c++ - 我该如何正确格式化?

mysql - SQL查询以获取指定月份的记录

mysql - 在 MySQL 中添加 last_update 字段是一个好习惯吗?

MySQL触发器不起作用

c - `round' c语言 undefined reference

c - 64 位整数的签名饱和加法?

c - C 中的 join() 或 implode()