从我的程序连接到mysql网络数据库
Connect to a mysql web database from my program
我试图从我的程序连接到mysql web数据库,但我一直得到:
Can't connect to MySQL server on 'mysql7.000webhost.com' (51)
我正在使用这个网站免费的mysql服务器:
000webhost.com
这是我的代码或我在谷歌上找到的开始学习的代码。
#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 : %sn", 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 : %sn", 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("%sn", row[0]);
/* clean up the database result set */
mysql_free_result(res);
/* clean up the database link */
mysql_close(conn);
return 0;
}
为什么它不连接?
如果您使用共享web主机,mysql服务器可能被配置为拒绝外部连接。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- C++MySQL C api用户输入行
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 通过网络、跨平台传递std::变体是否安全
- 如何在MNIST上提高网络运行的准确性
- Active Directory:从网络服务帐户下运行的Windows服务调用ADsOpenObject时失败
- 使用mysql c++连接器的未定义引用
- 无法在C++中建立与MySQL数据库的连接
- OpenCV 4.1.2 - 从网络摄像头获取帧并将其拆分
- mysql C++ connector [mysql-connector-c++-8.0.19-linux-glibc2
- 使用OpenCV和覆盆子上的多个网络摄像头拍摄延时摄影,出现多个V4L错误
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- Poco FindMySql.cmake 无法找到 MySQL 源代码
- 神经网络不学习.卡在50%
- MySQL C++连接器链接错误
- 如何绑定 C++ gRPC 客户端的网络接口
- 在本地网络中通过OpenCV(C++)实时流式传输图像
- 在不知道套接字的情况下关闭网络连接
- 从我的程序连接到mysql网络数据库