forked from hholzgra/connector-c-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql_fetch_fields.c
More file actions
105 lines (88 loc) · 3.47 KB
/
mysql_fetch_fields.c
File metadata and controls
105 lines (88 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* Copyright (C) 2005, 2006 Hartmut Holzgraefe
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* MySQL C client API example: mysql_fetch_fields()
*
* see also http://mysql.com/mysql_fetch_fields
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <mysql.h>
int main(int argc, char **argv)
{
MYSQL *mysql = NULL;
mysql = mysql_init(mysql);
if (!mysql) {
puts("Init faild, out of memory?");
return EXIT_FAILURE;
}
if (!mysql_real_connect(mysql, /* MYSQL structure to use */
MYSQL_HOST, /* server hostname or IP address */
MYSQL_USER, /* mysql user */
MYSQL_PWD, /* password */
MYSQL_DB, /* default database to use, NULL for none */
0, /* port number, 0 for default */
NULL, /* socket file or named pipe name */
CLIENT_FOUND_ROWS /* connection flags */ )) {
puts("Connect failed\n");
} else {
mysql_query(mysql, "SET NAMES utf8");
if (mysql_query(mysql, "SELECT LPAD(i, 7, ' ') as t FROM test.f LIMIT 1")) {
printf("Query failed: %s\n", mysql_error(mysql));
} else {
MYSQL_RES *result = mysql_store_result(mysql);
if (!result) {
printf("Couldn't get results set: %s\n", mysql_error(mysql));
} else {
MYSQL_FIELD *fields;
fields = mysql_fetch_fields(result);
if (!fields) {
printf("Faild fetching fields: %s\n", mysql_error(mysql));
} else {
unsigned int i, num_fields = mysql_num_fields(result);
for (i = 0; i < num_fields; i++) {
printf("FIELD #%d\n", i);
printf(" %-20s %s\n", "Field name", fields[i].name);
#if MYSQL_VERSION_ID >= 40100
printf(" %-20s %s\n", "Original name", fields[i].org_name);
#endif
printf(" %-20s %s\n", "From table", fields[i].table);
printf(" %-20s %s\n", "Original name", fields[i].org_table);
printf(" %-20s %s\n", "Database", fields[i].db);
#if MYSQL_VERSION_ID >= 40100
printf(" %-20s %s\n", "Catalog", fields[i].catalog);
#endif
printf(" %-20s %s\n", "Default", fields[i].def);
printf(" %-20s %lu\n", "CREATE field length", fields[i].length);
printf(" %-20s %lu\n", "MAX field lengt", fields[i].max_length);
#if MYSQL_VERSION_ID >= 40100
printf(" %-20s %u\n", "Field name length", fields[i].name_length);
printf(" %-20s %u\n", "Original name length", fields[i].org_name_length);
printf(" %-20s %u\n", "Table name length", fields[i].table_length);
printf(" %-20s %u\n", "Original name length", fields[i].org_table_length);
printf(" %-20s %u\n", "DB name length", fields[i].db_length);
printf(" %-20s %u\n", "Catalog name length", fields[i].catalog_length);
printf(" %-20s %u\n", "Default length", fields[i].def_length);
#endif
/* TODO: decimals */
printf("\n");
}
}
}
}
}
mysql_close(mysql);
return EXIT_SUCCESS;
}