C language: obtain all tables in a mysql database and all field names in the table.

Source: Internet
Author: User

Program Introduction: Sometimes we want to know which tables are in a database and what fields are in the table. I wrote a small program to implement this function. Idea: 1: connect to the database (nonsense) 2: Get all the tables in the database and cache them. 3: for each table, execute the SQL command select * from XXX and output their header (if you can think of a better method, I hope to tell you ). Code: [cpp] # include <stdio. h> # include <stdlib. h> # include <string. h> # include <mysql/mysql. h> # define MAX_COUNT 256 // when an error occurs, output the error message, close the connection, and exit the program 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);} int main (int argc, char * argv []) {MYSQL * my_con; MYSQL_RES * my_res; MYSQL_FIELD * my_field; MYSQL_ROW my_row; int rows, I, j, res; char namebuf [MAX_COUNT] [MAX_COUNT] = {0}; int count = 0; my_con = malloc (sizeof (MYSQL )); // connect to the database mysql_init (my_con); my_con = mysql_real_connect (my_con, "127.0.0.1", "root", "aaaaaaa", "test", 0, NULL, CLIENT_FOUND_ROWS ); If (NULL = my_con) error_quit ("Connection fail", my_con); printf ("Connection success \ n "); // obtain all the tables in the database. 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 ); // buffer the query result 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]);} for (I = 0; I <count; I ++) {char tbuf [100] = {0}; snprintf (tbuf, 100, "select * from % S ", namebuf [I]); // get the pointer res = mysql_query (my_con, tbuf) for the entire table content; 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 ); // obtain the number of columns in the table rows = mysql_num_fields (my_res); printf ("name: % s count: % d \ n", namebuf [I], rows ); // obtain and output the header my_field = mysql_fetch_fields (my_res); for (j = 0; j <rows; j ++) printf ("% s,", my_field [j]. name); printf ("\ n --------------------------------------- \ n");} // release space, close Connect mysql_free_result (my_res); mysql_close (my_con); free (my_con); return 0;} running example (input in red): qch @ LinuxMint ~ /Program/tcode $ gcc ctemp. c-o ctemp-lmysqlclientqch @ LinuxMint ~ /Program/tcode $./ctempConnection successname: class1 count: 4id, name, age, birthday ,-------------------------------------

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.