C语言:获取某个mysql数据库中所有的表及其表中所有的字段名

时间:2021-12-14 13:52:11

程序简介:有时候我们想知道一个数据库中到底有哪些表,表中都有些什么字段。我写了一个小程序来实现这个功能。


思路:
1:连接数据库(废话)

2:获取数据库中所有的表,并将它们缓存下来。

3:对于每个表,就执行SQL命令select * from XXX,并将它们的表头输出(如果大家能想到更好的方法,望告知)。

上代码:

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

#define MYSQL_IP "127.0.0.1"
#define MYSQL_ACCOUNT "root"
#define MYSQL_PWD "aaaaaaa"
#define MYSQL_TABLE "mysql"

#define MAX_TABLE_COUNT 1024
#define MAX_NAME_LEN 256

//发生错误时,输出错误信息,关闭连接,退出程序
void error_quit(const char *str, MYSQL *connection)
{
fprintf(stderr, "%s : %d: %s\n",
str, mysql_errno(connection),
mysql_error(connection));
if (connection != NULL)
mysql_close(connection);
exit(1);
}

//将MYSQL的表字段类型变成字符串
char const *field_type_to_string(int type)
{
switch (type)
{
case MYSQL_TYPE_BIT: return "BIT";
case MYSQL_TYPE_BLOB: return "BLOB";
case MYSQL_TYPE_DATE: return "DATE";
case MYSQL_TYPE_DATETIME: return "DATETIME";
case MYSQL_TYPE_NEWDECIMAL: return "NEWDECIMAL";
case MYSQL_TYPE_DECIMAL: return "DECIMAL";
case MYSQL_TYPE_DOUBLE: return "DOUBLE";
case MYSQL_TYPE_ENUM: return "ENUM";
case MYSQL_TYPE_FLOAT: return "FLOAT";
case MYSQL_TYPE_GEOMETRY: return "GEOMETRY";
case MYSQL_TYPE_INT24: return "INT24";
case MYSQL_TYPE_LONG: return "LONG";
case MYSQL_TYPE_LONGLONG: return "LONGLONG";
case MYSQL_TYPE_LONG_BLOB: return "LONG_BLOB";
case MYSQL_TYPE_MEDIUM_BLOB: return "MEDIUM_BLOB";
case MYSQL_TYPE_NEWDATE: return "NEWDATE";
case MYSQL_TYPE_NULL: return "NULL";
case MYSQL_TYPE_SET: return "SET";
case MYSQL_TYPE_SHORT: return "SHORT";
case MYSQL_TYPE_STRING: return "STRING";
case MYSQL_TYPE_TIME: return "TIME";
case MYSQL_TYPE_TIMESTAMP: return "TIMESTAMP";
case MYSQL_TYPE_TINY: return "TINY";
case MYSQL_TYPE_TINY_BLOB: return "TINY_BLOB";
case MYSQL_TYPE_VAR_STRING: return "VAR_STRING";
case MYSQL_TYPE_YEAR: return "YEAR";
default: return "-Unknown-";
}
}

//输出表的信息
void print_table_info(const char *name, MYSQL_RES *my_res)
{
//获取表的列数
int rows = mysql_num_fields(my_res);
printf("table: %s\n", name);

//获取并输出表头
MYSQL_FIELD *my_field = mysql_fetch_fields(my_res);
int i;
for (i = 0; i < rows; i++)
{
printf("%d: %s(%s)\n", i + 1, my_field[i].name,
field_type_to_string(my_field[i].type));
}
printf("\n");
}


int main(int argc, char *argv[])
{
MYSQL *my_con;
MYSQL_RES *my_res;
MYSQL_ROW my_row;
int i, res;
char namebuf[MAX_TABLE_COUNT][MAX_NAME_LEN] = { 0 };
int count = 0;

my_con = malloc(sizeof(MYSQL));

//连接数据库
mysql_init(my_con);
my_con = mysql_real_connect(my_con, MYSQL_IP, MYSQL_ACCOUNT,
MYSQL_PWD, MYSQL_TABLE, 0, NULL, CLIENT_FOUND_ROWS);
if (NULL == my_con)
error_quit("Connection fail", my_con);
printf("Connection success\n");

//获取整个数据库中的所有表
res = mysql_query(my_con, "show tables;");
if (res != 0)
error_quit("Select fail", my_con);
my_res = mysql_store_result(my_con);
if (NULL == my_res)
error_quit("Get result fail", my_con);

//缓冲刚才查询的结果
while (1)
{
my_row = mysql_fetch_row(my_res);
if (NULL == my_row)
break;
if (my_row[0] == NULL)
printf("NULL\t");
else
strcpy(namebuf[count++], (char*)my_row[0]);
}
printf("tables count: %d\n\n", count);

for (i = 0; i < count; i++)
{
char tbuf[MAX_NAME_LEN] = { 0 };
snprintf(tbuf, MAX_NAME_LEN, "select * from %s", namebuf[i]);
//获取整个表的内容的指针
res = mysql_query(my_con, tbuf);
if (res != 0)
error_quit("Select fail", my_con);
my_res = mysql_store_result(my_con);
if (NULL == my_res)
error_quit("Get result fail", my_con);

print_table_info(namebuf[i], my_res);
}

//释放空间,关闭连接
mysql_free_result(my_res);
mysql_close(my_con);
free(my_con);

return 0;
}

以MYSQL表为示例,输出如下:

Connection success
tables count: 24

table: columns_priv
1: Host(STRING)
2: Db(STRING)
3: User(STRING)
4: Table_name(STRING)
5: Column_name(STRING)
6: Timestamp(TIMESTAMP)
7: Column_priv(STRING)

table: db
1: Host(STRING)
2: Db(STRING)
3: User(STRING)
4: Select_priv(STRING)
5: Insert_priv(STRING)
6: Update_priv(STRING)
7: Delete_priv(STRING)
8: Create_priv(STRING)
9: Drop_priv(STRING)
10: Grant_priv(STRING)
11: References_priv(STRING)
12: Index_priv(STRING)
13: Alter_priv(STRING)
14: Create_tmp_table_priv(STRING)
15: Lock_tables_priv(STRING)
16: Create_view_priv(STRING)
17: Show_view_priv(STRING)
18: Create_routine_priv(STRING)
19: Alter_routine_priv(STRING)
20: Execute_priv(STRING)
21: Event_priv(STRING)
22: Trigger_priv(STRING)

table: event
1: db(STRING)
2: name(STRING)
3: body(BLOB)
4: definer(STRING)
5: execute_at(DATETIME)
6: interval_value(LONG)
7: interval_field(STRING)
8: created(TIMESTAMP)
9: modified(TIMESTAMP)
10: last_executed(DATETIME)
11: starts(DATETIME)
12: ends(DATETIME)
13: status(STRING)
14: on_completion(STRING)
15: sql_mode(STRING)
16: comment(STRING)
17: originator(LONG)
18: time_zone(STRING)
19: character_set_client(STRING)
20: collation_connection(STRING)
21: db_collation(STRING)
22: body_utf8(BLOB)

table: func
1: name(STRING)
2: ret(TINY)
3: dl(STRING)
4: type(STRING)

table: general_log
1: event_time(TIMESTAMP)
2: user_host(BLOB)
3: thread_id(LONG)
4: server_id(LONG)
5: command_type(VAR_STRING)
6: argument(BLOB)

table: help_category
1: help_category_id(SHORT)
2: name(STRING)
3: parent_category_id(SHORT)
4: url(BLOB)

table: help_keyword
1: help_keyword_id(LONG)
2: name(STRING)

table: help_relation
1: help_topic_id(LONG)
2: help_keyword_id(LONG)

table: help_topic
1: help_topic_id(LONG)
2: name(STRING)
3: help_category_id(SHORT)
4: description(BLOB)
5: example(BLOB)
6: url(BLOB)

table: host
1: Host(STRING)
2: Db(STRING)
3: Select_priv(STRING)
4: Insert_priv(STRING)
5: Update_priv(STRING)
6: Delete_priv(STRING)
7: Create_priv(STRING)
8: Drop_priv(STRING)
9: Grant_priv(STRING)
10: References_priv(STRING)
11: Index_priv(STRING)
12: Alter_priv(STRING)
13: Create_tmp_table_priv(STRING)
14: Lock_tables_priv(STRING)
15: Create_view_priv(STRING)
16: Show_view_priv(STRING)
17: Create_routine_priv(STRING)
18: Alter_routine_priv(STRING)
19: Execute_priv(STRING)
20: Trigger_priv(STRING)

table: ndb_binlog_index
1: Position(LONGLONG)
2: File(VAR_STRING)
3: epoch(LONGLONG)
4: inserts(LONGLONG)
5: updates(LONGLONG)
6: deletes(LONGLONG)
7: schemaops(LONGLONG)

table: plugin
1: name(VAR_STRING)
2: dl(VAR_STRING)

table: proc
1: db(STRING)
2: name(STRING)
3: type(STRING)
4: specific_name(STRING)
5: language(STRING)
6: sql_data_access(STRING)
7: is_deterministic(STRING)
8: security_type(STRING)
9: param_list(BLOB)
10: returns(BLOB)
11: body(BLOB)
12: definer(STRING)
13: created(TIMESTAMP)
14: modified(TIMESTAMP)
15: sql_mode(STRING)
16: comment(BLOB)
17: character_set_client(STRING)
18: collation_connection(STRING)
19: db_collation(STRING)
20: body_utf8(BLOB)

table: procs_priv
1: Host(STRING)
2: Db(STRING)
3: User(STRING)
4: Routine_name(STRING)
5: Routine_type(STRING)
6: Grantor(STRING)
7: Proc_priv(STRING)
8: Timestamp(TIMESTAMP)

table: proxies_priv
1: Host(STRING)
2: User(STRING)
3: Proxied_host(STRING)
4: Proxied_user(STRING)
5: With_grant(TINY)
6: Grantor(STRING)
7: Timestamp(TIMESTAMP)

table: servers
1: Server_name(STRING)
2: Host(STRING)
3: Db(STRING)
4: Username(STRING)
5: Password(STRING)
6: Port(LONG)
7: Socket(STRING)
8: Wrapper(STRING)
9: Owner(STRING)

table: slow_log
1: start_time(TIMESTAMP)
2: user_host(BLOB)
3: query_time(TIME)
4: lock_time(TIME)
5: rows_sent(LONG)
6: rows_examined(LONG)
7: db(VAR_STRING)
8: last_insert_id(LONG)
9: insert_id(LONG)
10: server_id(LONG)
11: sql_text(BLOB)

table: tables_priv
1: Host(STRING)
2: Db(STRING)
3: User(STRING)
4: Table_name(STRING)
5: Grantor(STRING)
6: Timestamp(TIMESTAMP)
7: Table_priv(STRING)
8: Column_priv(STRING)

table: time_zone
1: Time_zone_id(LONG)
2: Use_leap_seconds(STRING)

table: time_zone_leap_second
1: Transition_time(LONGLONG)
2: Correction(LONG)

table: time_zone_name
1: Name(STRING)
2: Time_zone_id(LONG)

table: time_zone_transition
1: Time_zone_id(LONG)
2: Transition_time(LONGLONG)
3: Transition_type_id(LONG)

table: time_zone_transition_type
1: Time_zone_id(LONG)
2: Transition_type_id(LONG)
3: Offset(LONG)
4: Is_DST(TINY)
5: Abbreviation(STRING)

table: user
1: Host(STRING)
2: User(STRING)
3: Password(STRING)
4: Select_priv(STRING)
5: Insert_priv(STRING)
6: Update_priv(STRING)
7: Delete_priv(STRING)
8: Create_priv(STRING)
9: Drop_priv(STRING)
10: Reload_priv(STRING)
11: Shutdown_priv(STRING)
12: Process_priv(STRING)
13: File_priv(STRING)
14: Grant_priv(STRING)
15: References_priv(STRING)
16: Index_priv(STRING)
17: Alter_priv(STRING)
18: Show_db_priv(STRING)
19: Super_priv(STRING)
20: Create_tmp_table_priv(STRING)
21: Lock_tables_priv(STRING)
22: Execute_priv(STRING)
23: Repl_slave_priv(STRING)
24: Repl_client_priv(STRING)
25: Create_view_priv(STRING)
26: Show_view_priv(STRING)
27: Create_routine_priv(STRING)
28: Alter_routine_priv(STRING)
29: Create_user_priv(STRING)
30: Event_priv(STRING)
31: Trigger_priv(STRING)
32: Create_tablespace_priv(STRING)
33: ssl_type(STRING)
34: ssl_cipher(BLOB)
35: x509_issuer(BLOB)
36: x509_subject(BLOB)
37: max_questions(LONG)
38: max_updates(LONG)
39: max_connections(LONG)
40: max_user_connections(LONG)
41: plugin(STRING)
42: authentication_string(BLOB)