Access mysql database _ MySQL in C ++ in linux (ubuntu)

Source: Internet
Author: User
Tags mysql functions
Access mysql database ubuntu in linux (Ubuntu) C ++

  1. Install msyql on Ubuntu

    1. Install mysql database

      sudo apt-get install mysql-server
    2. Install the mysql client

      sudo apt-get install mysql-client
  2. Basic types of C APIs

    1. MYSQL

      1. This structure represents a database connection handle.

      2. Almost all MySQL functions use it.

      3. You should not try to copy the MYSQL structure. it is not guaranteed that such copying results will be useful.

    2. MYSQL_RES

      1. This structure indicates the query result of the returned row.

    3. MYSQL_ROW

      1. This is the "type security" representation of one row of data. It is currently implemented based on the array that counts byte strings.

      2. The row is obtained by calling mysql_fetch_row.

    4. Official document address

      1. http://dev.mysql.com/doc/refman/5.1/zh/apis.html#c
  3. Code

    1. Insert (delete) a piece of data into the table

      # Include
          
           
      # Include
           
            
      Using namespace std; int main (int argc, char * argv []) {// prepare the mysql access structure MYSQL mysql; mysql_init (& mysql); mysql_real_connect (& mysql, "192.168.16.114", // IP address of the database to be accessed "root", // username "root", // password "test", // 3306 of the database to be accessed, // The database port is NULL, // generally NULL 0 // generally 0); // insert string SQL = "insert into student value (1, 'jput ', 24, 'gzjd ') "; // delete // string SQL =" delete from student where id = 33 "; // execute the SQL statement mysql_query (& mysql, SQL. c_str (); // Close the database connection mysql_close (& mysql); return 0 ;}// compile // g ++ file. cpp-o target-lmysqlclient // execute //. /target // verify // success
           
          
    2. Update table content

      #include 
          
           #include 
           
            using namespace std;int main(int argc, char* argv[]){MYSQL mysql;mysql_init( &mysql );mysql_real_connect(&mysql,"192.168.16.114","root","root","test",3306,NULL,0);string sql = "update student set name = 'pj' where id = 2";mysql_query( &mysql, sql.c_str() );             mysql_close(&mysql);                return 0;}
           
          
    3. Call stored procedure

      #include 
          
           #include 
           
            using namespace std;int main(int argc, char* argv[]){MYSQL mysql;mysql_init( &mysql );mysql_real_connect(&mysql,"192.168.16.114","root","root","test",3306,NULL,0);string sql = "call myPorc();";int ret = mysql_query( &mysql, sql.c_str() );//      debug info//cout << mysql_error( &mysql );//cout << ret << endl;mysql_close(&mysql);return 0;}
           
          
    4. Query data table content

      # Include
          
           
      # Include
           
            
      Using namespace std; int main (int argc, char * argv []) {MYSQL mysql; mysql_init (& mysql); mysql_real_connect (& mysql, "192.168.16.114", "root ", "root", "test", 3306, NULL, 0); string SQL = "select * from student"; mysql_query (& mysql, SQL. c_str (); MYSQL_RES * result = NULL; result = mysql_store_result (& mysql); // you can obtain the number of entries of all data queried. int row_count = mysql_num_rows (result ); cout <"all data number:" <row_count <end L; // Obtain the number of fields and the field name int field_count = mysql_num_fields (result); cout <"field count:" <field_count <endl; // obtain the names of all fields MYSQL_FIELD * field = NULL; for (int I = 0; I <field_count; ++ I) {field = mysql_fetch_field_direct (result, I ); cout <field-> name <"/t" ;}cout <endl; // displays all data in the table MYSQL_ROW row = NULL; row = mysql_fetch_row (result ); while (NULL! = Row) {for (int I = 0; I <field_count; ++ I) {cout <row [I] <"/t" ;}cout <endl; row = mysql_fetch_row (result);} mysql_free_result (result); mysql_close (& mysql); return 0 ;}
           
          
    5. Obtain all tables in the specified database test.

      # Include
          
           
      # Include
           
            
      # Include
            
             
      # Include
             
              
      Using namespace std; int main (int argc, char * argv []) {// define a database connection handle MYSQL; // initialize the data handle mysql_init (& mysql ); // connect to the database mysql_real_connect (& mysql, "192.168.16.114", "root", "root", "test", 3306, NULL, 0 ); // query the database string SQL = "show tables;"; mysql_query (& mysql, SQL. c_str (); MYSQL_RES * result = NULL; result = mysql_store_result (& mysql); // Obtain the number of all data records queried by vector
              
               
      AllTable; MYSQL_ROW row = mysql_fetch_row (result); while (NULL! = Row) {allTable. push_back (row [0]); row = mysql_fetch_row (result);} for (vector
               
                 : Const_iterator cit = allTable. begin (); cit! = AllTable. end (); ++ cit) {cout <* cit <"/t" ;}cout <endl; mysql_free_result (result); mysql_close (& mysql ); return 0 ;}
               
              
             
            
           
          



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.