C + + Access MySQL on Linux

Source: Internet
Author: User
Tags rowcount

1. Install the MySQL client

With the command:

Yum install-y mysql-server MySQL mysql-devel

This command contains the installation of the client and server

2. Visit MYSLQ

At the command line, enter:

mysql-h192.168.0.36-uroot-p123456

Appear:

Welcome to the MySQL Monitor. Commands End With; or \g.

Your MySQL Connection ID is 45099

Server VERSION:5.5.5-10.0.12-MARIADB MariaDB Server

Copyright (c) and/or, Oracle, its affiliates. All rights reserved.

Oracle is a registered trademark of the Oracle Corporation and/or its

Affiliates. Other names trademarks of their respective

Owners

On behalf of the Mysql Client Installation succeeded!

3. code Access

#include <iostream>#include<mysql/mysql.h>#include<string>using namespacestd;intMain () {MySQL mysql; Mysql_init (&MySQL); Mysql_real_connect (&mysql,"192.168.0.36","Root","123456","UC",3306Null0); stringsql ="INSERT INTO Sysuser (Orgid,useraccount) VALUES (1, ' java1 ');"; mysql_query (&MySQL, sql.c_str ()); Mysql_close (&MySQL); return 0;}

Compile:

g++-O test main.cpp-lmysqlclient-i/usr/include/mysql/-l/usr/lib64/mysql

Note: The path to- L is below lib64

4. Query Instance Code

#include <iostream>#include<string>#include<mysql/mysql.h>using namespacestd;intMain () {MySQL mysql; Mysql_res*result =NULL; Mysql_field*field =NULL; Mysql_init (&MySQL); Mysql_real_connect (&mysql,"192.168.0.36","Root","123456","UC",3306Null0); //Mysql_real_connect (&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);    stringsql ="select Id,useraccount from Sysuser;"; mysql_query (&MySQL, sql.c_str ()); Result= Mysql_store_result (&MySQL); intRowCount =mysql_num_rows (Result); cout<<"ROWCOUNT:"<< RowCount <<Endl; intFieldCount =mysql_num_fields (Result); cout<<"FieldCount:"<< FieldCount <<Endl;  for(inti =0; i < FieldCount; i++) {field=Mysql_fetch_field_direct (result,i); cout<<"Field Name:"<<field->name <<"\t\t"; } cout<<Endl; Mysql_row ROW=NULL; Row=mysql_fetch_row (Result);  while(NULL! =row) {      for(intI=0;i<fieldcount; i++) {cout<<"Row"<<i<<":"<<row[i] <<"\t\t"; } cout<<Endl; Row=mysql_fetch_row (Result); } mysql_close (&MySQL); return 0;}

C + + Access MySQL on Linux

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.