C + + Connection database entry and get column count, data

Source: Internet
Author: User

There is no specific release of the complete program, sharing two core functions:

Because the function used here is not the compiler's own library, you need to download the Mysql.h library or a local database, you can go to the bin to find, put in.

The premise, my own test database is Wampserve own MySQL, have tried to connect Sina Cloud, found very pit, it inside to put the code into its space to connect, not in the machine connected, the input parameters are all its provisions of the constant!

The first one is to connect to the database:

In the line with detailed comments, all my opinion, there is misunderstanding, to help point out.

For a brief introduction, all of them with an int return type are the result of a failure to connect to the database and return 0 to end the program.

Parameter input is the database address, port, the port is generally 3306, the database name, user name, password, call can be used.

1 intDatebase::init (Const Char* Host,intPortConst Char* Database,Const Char* User,Const Char*password) {2      This->mysql_res = NULL;//again, the reason is that the Init function can be used within any function, in order to avoid having a cache read before it causes an error, and the res is null before init.3My_bool opt_reconnect=1;//global "yes" to enable some features4 5Mysql_init (& ( This->conn));//Initialize, this is the function that the library must have, pass in the conn, store the cache in6 7Mysql_options (& ( This->conn), Mysql_set_charset_name, (Char*)"GBK");8     //library function, mysql_options set GBK character code, UTF8 will cause the data from the library to display garbled,9     //This function is very powerful, the second parameter is the selection function, here is the choice to set the character code, set the character code can also use the query command, but it is more troublesome. Ten          OneMysql_options (& ( This->conn), Mysql_opt_reconnect, (Char*) &opt_reconnect); A     //set when MySQL is abnormally disconnected, look at the second parameter, Reconne, is the re-connection. can automatically re-connect, how to measure out?  -     //The following real connect function, if found to be missing, will contact it -  the     if(!mysql_real_connect (& ( This->conn), host, user, password, database, port, NULL, client_multi_statements|client_found_rows)) { -         //This is the connection function, one of the core, a lot of optional parameters, here Finally, allow other operations, the connection failed to output all information -cout<Endl; -         return 0; +     } -     //set up transaction autocommit, 1 to enable auto-commit +Mysql_autocommit (& ( This->conn),1); A}

The following is successful after connecting to the database:

In particular, please make sure that you have a data sheet in MySQL before using this, otherwise it will go wrong!

A function used to get the column names of tables in the database, and to output all data in sequential, sequentially output column names.

Inside the same comment complete, still do not understand please leave a message! Please tell me the wrong message. Thank you!

Readers are asked to understand the functions inside, such as introduction:

mysql_query (,) This is the execution of the command, the successful return 0, the first parameter is a library cache variable, the second is a command string;

Mysql_store_result (), gets the result set, the parameter is a library cache variable, and the return value is the result set cache variable of the MySQL res type;

Mysql_fetch_fields (), gets the name of the column in the table, it returns an array of MySQL filed types, takes all the column names once, and outputs all with a single loop!

Mysql_num_fields (), gets the number of fields, that is, how many columns;

Mysql_fetch_row (), get a row of data, this is a line oh, but, get a row will automatically move back, so use while the best! The return value is a variable of the MySQL row type
1 voiddatebase::show_db () {//show the data in general2     inti,j;3Mysql_field *files;4Check ();//Call the CHEC function to check whether the user table exists or not5     //this->mysql_res=mysql_list_tables (& (This->conn), "user_1");6     if(Mysql_query (& ( This->conn),"select * from user_1;")!=0){//Selecting all data for the user table * represents all7cout<<"Error getting user table information! ";8     }9      This->mysql_res=mysql_store_result (& ( This-conn));TenFiles=mysql_fetch_fields ( This->mysql_res);//Get field name One      for(i=0; I<mysql_num_fields ( This->mysql_res); i++) {//Mysql_num_fields Returns the number of fields ACOUT&LT;&LT;STD::LEFT&LT;&LT;STD::SETW ( -);cout<<files[i].name;//to align the output, the SETW function is used to align the -     } -cout<<"\ n"; the      while(Mysql_row=mysql_fetch_row ( This->mysql_res)) {//get a row of data -           for(j=0; J<mysql_num_fields ( This->mysql_res); j + +){ -              if(Mysql_row[j]==null) {//prevent content from being empty, program crashes -cout<<"NULL"; +              } -              Else +COUT&LT;&LT;STD::LEFT&LT;&LT;STD::SETW ( -);cout<<Mysql_row[j]; A           } atcout<<"\ n"; -     } -Mysql_free_result (Mysql_res);//This is free memory . -}

C + + Connection database entry and get column count, data

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.