Operate MySQL database _ MySQL through C ++ code in Linux

Source: Internet
Author: User
Code: # export decstdlib # export destring # export deusrlocal#demysqlmysql. h: location of the mysql header file installed in usingnamespacestd; stringhost database address; stringuser username; add stri C and C

Code:

# Include
# Include
# Include "/usr/local/include/mysql. h" // location of the mysql header file installed
Using namespace std;
String host = "database address ";
String user = "user name ";

String pwd = "password ";
String dbname = "database ";
String SQL = "query statement ";
Unsigned int port = 3309; # port number
Int status;
Int main (){
MYSQL * mysql;
Mysql = mysql_init (0 );
MYSQL_RES * result;
MYSQL_ROW row;
If (mysql_real_connect (mysql, host. c_str (), user. c_str (), pwd. c_str (), dbname. c_str (), port, NULL, CLIENT_FOUND_ROWS) = NULL ){
Cout <"connect failure! "<Endl;
Return EXIT_FAILURE;
} Else {
Cout <"connect success! "<Endl;
}
Mysql_set_character_set (mysql, "gbk ");
Status = mysql_query (mysql, SQL. c_str ());
If (status! = 0 ){
Cout <"query failure! "<Endl;
}
Cout <"the status is:" <status <endl;
Result = mysql_store_result (mysql );
While (row = mysql_fetch_row (result )){
Cout <row [1] <"|" <row [2] <endl;
}
Mysql_free_result (result );
Mysql_close (mysql );
}

Compile:

G ++-o test. cpp-lmysqlclient-I/usr/local/include/mysql/-L/usr/local/lib/mysql

Which of the following are used to specify the path and library file path for mysql installation, which is related to the path for mysql installation on your machine.

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.