Use of ubuntu C ++ to connect to MySQL Databases and databases

Source: Internet
Author: User

Today, in Ubuntu, when I use eclipse to connect to the MySQL database, the mysql_init () undefined error always occurs. Later I found that I needed to add a static Link Library to the project. The following describes how to connect eclipse + CDT to MySQL using C ++.

First, install mysql. Enter sudo apt-getinstall mysql-server mysql-client in the terminal (or install it in the Ubuntu Software Center ). At the same time, you also need to install the Development Kit (c api does not appear in Ubuntu as in windows, and you need to continue to install the API). Install the Development Kit command:
Sudo apt-Get install libmysqlclient15-dev

2. Install eclipse and configure eclipse for the C/C ++ development environment. See http://blog.csdn.net/linrulei11/article/details/7298093

Third, after eclipse + CDT is built, you can create a C ++ project and enter the following testCode.

# Include <MySQL/MySQL. h>
# Include <stdio. h>

Int main (){
MySQL * conn;
Mysql_res * res;
Mysql_row row;
Char * Server = "localhost"; // database address
Char * user = "root"; // database username
Char * Password = "KK"; // Database Password
Char * database = "MySQL"; // database name used
Conn = mysql_init (null );

If (! Mysql_real_connect (Conn, server,
User, password, database, 0, null, 0 )){
Fprintf (stderr, "% s \ n", mysql_error (conn ));
Return-1;
}

If (mysql_query (Conn, "show tables ")){
Fprintf (stderr, "% s \ n", mysql_error (conn ));
Return-1;
}
Res = mysql_use_result (conn );

Printf ("MySQL tables in MySQL database: \ n ");
While (ROW = mysql_fetch_row (RES ))! = NULL)
Printf ("% s \ n", row [0]);

Mysql_free_result (RES );
Mysql_close (conn );
Return 0;
}

The project structure is as follows:

After Ctrl + B builds the project, nine errors will occur. All interface functions that cannot recognize MySQL. At this time, add "Project-> properties-> C/C ++ build-> Settings-> gcc C linker-> libraries" in eclipse to mysqlclient. Build again and there will be no errors. After running, the result is also output.

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.