Install MySQL database to obtain the procedure for establishing the C interface for MySQL. h

Source: Internet
Author: User

This article mainly describes how to install MySQL database to obtain MySQL. h. The actual operation process for establishing the C interface starts with the installation of the MySQL database, which involves the description of the actual application code. The following describes the specific content of the article.

Install MySQL first

Code:

 
 
  1. sudo apt-get install MySQL-server MySQL-client 

Reinstall the Development Kit

Code:

 
 
  1. sudo apt-get install libMySQLclient15-dev 

After installing the MySQL database, add the header file to the C code.

Code:

 
 
  1. #include < mysql .h> 

Compilation Method:

Code:

 
 
  1. gcc $(mysql_config --cflags) xxx.c -o xxx $(mysql_config --libs) 

Use the following code to test

Code:

 
 
  1. /* Simple C program that connects to MySQL Database server*/  
  2. #include <mysql.h> 
  3. #include <stdio.h> 
  4. main() {  
  5. MYSQL *conn;  
  6. MYSQL_RES *res;  
  7. MYSQL_ROW row;  
  8. char *server = "localhost";  
  9. char *user = "root";  
  10. char *password = "";   

Change the password here

 
 
  1. char *database = "mysql";  
  2. conn = mysql_init(NULL);  
  3. /* Connect to database */  
  4. if (!mysql_real_connect(conn, server,  
  5. user, password, database, 0, NULL, 0)) {  
  6. fprintf(stderr, "%s\n", mysql_error(conn));  
  7. exit(1);  
  8. }  
  9. /* send SQL query */  
  10. if (mysql_query(conn, "show tables")) {  
  11. fprintf(stderr, "%s\n", mysql_error(conn));  
  12. exit(1);  
  13. }  
  14. res = mysql_use_result(conn);  
  15. /* output table name */  
  16. printf("MySQL Tables in mysql database:\n");  
  17. while ((row = mysql_fetch_row(res)) != NULL)  
  18. printf("%s \n", row[0]);  
  19. /* close connection */  
  20. mysql_free_result(res);  
  21. mysql_close(conn);  
  22. }  

The content of the existing database and table is output. The above content is an introduction to installing the MySQL database to obtain the C interface created by MySQL. h. I hope you will get some benefits.

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.