Access the MySQL database in C language in Ubuntu

Source: Internet
Author: User
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-servermysql-client and then install the Development Kit code: sudoapt-getinstalllibmysqlclient15-dev can use the following code to test the code:/* SimpleCprogramtha

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

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


Compile:


Method 1: Run gcc test. c-o test-I/user/include/mysql and the system prompts that mysql. h cannot be found.

I tried it and prompted that mysql. h could not be found.
Analysis:
/User/include/mysql: All. H files, which should be compiled
/User/lib/mysql: it is a static library and a dynamic library. www.linuxidc.com should correspond to the above. h file for the connection phase.

Method 2: gcc test. c-o test-L/user/lib/mysql-lmysqlclient succeeded!

Method 3: gcc test. c-o test $ (mysql_config -- cflags) $ (mysql_config -- libs)

Or: gcc test. c-o test $ (mysql_config -- cflags -- libs) is successful!

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.