C 代码连接阿里云 MySQL 数据库

更新时间: 2024-02-20 09:13:46作者: 网站编辑阅读量: 92

int main() {

MYSQL *conn;const char *host = "your_host";const char *user = "your_username";const char *password = "your_password";const char *sql = "SELECT * FROM your_table";MYSQL_RES *result;MYSQL_ROW row;conn = mysql_init(NULL);conn = mysql_real_connect(conn, host, user, password, NULL, 0, NULL, 0);if (conn == NULL) {    fprintf(stderr, "Error: %sn", mysql_error(conn));    return 1;}result = mysql_query(conn, sql);if (result == NULL) {    fprintf(stderr, "Error: %sn", mysql_error(conn));    return 1;}while ((row = mysql_fetch_row(result)) != NULL) {    // 处理每一行的数据}mysql_free_result(result);mysql_close(conn);return 0;

}

```

通过以上步骤和示例代码,你可以轻松地使用 C 代码连接阿里云 MySQL 数据库,并执行 SQL 查询。记得在实际使用时替换相应的主机名、用户名和密码。希望这篇文章对你有所帮助!

最新推荐

右侧广告图1右侧广告图2