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

Source: Internet
Author: User
Tags mysql code
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: sudoapt-getinstallmysql

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

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

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
# Include

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

The content of the existing database and table is 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.