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:
- sudo apt-get install MySQL-server MySQL-client
Reinstall the Development Kit
Code:
- sudo apt-get install libMySQLclient15-dev
After installing the MySQL database, 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);
- }
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.