#include "mysql.h" #include #include void test_fields(char* sql, MYSQL* conn) { printf("SQL: %s\n", sql); if (mysql_real_query(conn, sql, strlen(sql))) printf("Error: %s", mysql_error(conn)); MYSQL_RES* res; res = mysql_store_result(conn); if (res) { int cnt = mysql_num_fields(res); for (int i = 0; i < cnt; i++) { MYSQL_FIELD* fld; fld = mysql_fetch_field(res); printf("Name: %s, Org_table: %s, Table: %s\n", fld->name, fld->org_table, fld->table); } mysql_free_result(res); } } int main() { MYSQL_RES* res; MYSQL* conn; conn = mysql_init(NULL); conn = mysql_init(NULL); conn = mysql_real_connect(conn, "127.0.0.1", "root", "", "test", 4040, NULL, CLIENT_MULTI_STATEMENTS | CLIENT_MULTI_RESULTS); char *query = "drop table if exists table1; drop view if exists view1; CREATE TABLE table1 (ID INTEGER); CREATE VIEW view1 AS SELECT ID FROM table1;"; mysql_real_query(conn, query, strlen(query)); while (0 == mysql_next_result(conn)) { res = mysql_store_result(conn); mysql_free_result(res); } char sql1[] = "SELECT * FROM view1"; char sql2[] = "SELECT * FROM view1 vw"; char sql3[] = "SELECT * FROM table1"; char sql4[] = "SELECT * FROM table1 tt"; test_fields(sql1, conn); printf("---------------\n"); test_fields(sql2, conn); printf("---------------\n"); test_fields(sql3, conn); printf("---------------\n"); test_fields(sql4, conn); mysql_close(conn); return 0; }