Install MySql in Ubuntu to obtain the C interface created by MySQL. h.

Source: Internet
Author: User
Tags mysql code

Install MySql in Ubuntu to obtain the C interface created by MySQL. h.

After a long time in Ubuntu, I finally succeeded in operating MySql in C. Here I will write down the method and save it for later use. First install MySQL code: sudo apt-Get install mysql-server mysql-client and then install the development package...



Install MySql in Ubuntu to obtain the C interface created by MySQL. h.

[Date: 2010-02-05] Source: Ubuntu community Author: intijk


After a long time in Ubuntu, I finally succeeded in operating MySql in C. Here I will write down the method and save it for later use. Install MySQL first

Code:

Sudo apt-Get install mysql-server mysql-Client


Reinstall the Development Kit

Code:

Sudo apt-Get install libmysqlclient15-dev


After installation, add the header file to the C code.


Code:

# Include <mysql. h>


Compilation Method:

Code:

GCC $ (mysql_config -- cflags) xxx. C-o XXX $ (mysql_config -- libs)



Use the following code to test

Code:

/* Simple C program that connects to MySQL database server */

# Include <mysql. h>

# Include <stdio. h>


Main (){

MySQL * conn;

Mysql_res * res;

Mysql_row row;


Char * Server = "localhost ";

Char * user = "root ";

Char * Password = "";/* change the password here */

Char * database = "MySQL ";


Conn = mysql_init (null );


/* Connect to database */

If (! Mysql_real_connect (Conn, server,

User, password, database, 0, null, 0 )){

Fprintf (stderr, "% s \ n", mysql_error (conn ));

Exit (1 );

}


/* Send SQL query */

If (mysql_query (Conn, "show tables ")){

Fprintf (stderr, "% s \ n", mysql_error (conn ));

Exit (1 );

}


Res = mysql_use_result (conn );


/* Output table name */

Printf ("MySQL tables in MySQL database: \ n ");

While (ROW = mysql_fetch_row (RES ))! = NULL)

Printf ("% s \ n", row [0]);


/* Close connection */

Mysql_free_result (RES );

Mysql_close (conn );

}

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.