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;
}