How to connect to MySQL using C language in Eclipse + CDT Environment

Source: Internet
Author: User

Operating System: Linux Fedora 11
Development Environment: Eclipse, CDT, and MySQL5.1.32 (the header file uses MySQL5.1.35)
Preparations:
1. Run the following command to view the MySQL version installed in Linux:
$ Rpm-qa mysql
2. Check whether the/usr/include file contains the mysql folder and the header file *. h In the folder. Run the following command:
$ Cd/usr/include/mysql
A. If the folder already exists, enter the following command:
$ Ls-l | more
If there is no mysql. h mysql_version.h plugin;
B. If the directory or folder is not displayed after the preceding command is executed, create a mysql directory in the/user/include/directory and execute the following command:
$ Sudo mkdir/usr/include/mysql
Extract mysql-5.1.35.zip to your working directory:/home/test/database/mysql/
Run the following command to switch to the following directory:
$ Cd/home/test/database/mysql/mysql-5.1.35
Run the following command in the preceding directory:
$./Configure
After the command is successfully executed, run the following command:
$ Make
After the command is successfully executed, run the following command:
$ Sudo cp-r include // usr/include/mysql/
Copy the header file *. h to the/usr/include/mysql/include directory.
3. Create a project and set the compiling and running environment for the project to be modified.
Create a project MySQLConn in Eclipse
Create a header file MySQLConn. h. The Code is as follows:
/*
* MySQLConn. h
*
* Created on: Jun 11,200 9
* Author: zhanglei
*/

# Ifndef MYSQLCONN_H _
# Define MYSQLCONN_H _

Int TestMySQLConn ();


# Endif/* MYSQLCONN_H _*/

Create a program file MySQLConn. c. The Code is as follows:
/*
* MySQLConn. c
*
* Created on: Jun 11,200 9
* Author: zhanglei
*/
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <mysql. h>

# Include "MySQLConn. h"

Int main (int agrc, char * argv [])
{
Printf ("MySQL Connection Test ...");

Return TestMySQLConn ();
}

Int TestMySQLConn ()
{
MYSQL * pConn;
// MYSQL_RES * pRes;
// MYSQL_ROW my_conn;
PConn = mysql_init (NULL );
If (NULL = pConn)
{
Printf ("mysql_init failed! ");
Return EXIT_FAILURE;
}

PConn = mysql_real_connect (pConn, "127.0.0.1", "root", "123", "test", 3306, NULL, 0 );

If (NULL = pConn)
{
Printf ("Connection failed! ");
}
Else
{
Printf ("Connection succeed! ");
}

Mysql_close (pConn );

Return EXIT_SUCCESS;
}

  • 1
  • 2
  • Next Page

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.