C登錄mysql數據庫的代碼如下:
#include#include #include #include int main(int argc, char *argv[]) { MYSQL *conn; MYSQL_RES *res; MYSQL_ROW row; char *server = "localhost"; char *user = "root"; char *password = "root123"; char *database = "test"; char *query = "select * from users"; conn = mysql_init(NULL); if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) { fprintf(stderr, "%s\n", mysql_error(conn)); exit(1); } if (mysql_query(conn, query)) { fprintf(stderr, "%s\n", mysql_error(conn)); exit(1); } res = mysql_use_result(conn); while ((row = mysql_fetch_row(res)) != NULL) { printf("%s %s\n", row[0], row[1]); } mysql_free_result(res); mysql_close(conn); return 0; }
以上代碼演示了如何使用C語言連接到MySQL數據庫并執行查詢語句。