How to install MySQL to get mysql.h and establish C interface under Ubuntu
This article will explain in detail how to install MySQL under Ubuntu to get mysql.h to establish C interface. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Install MySQL under Ubuntu to get .h to establish C interface
It took a long time for C to operate MySQL successfully under Ubuntu. Write down the method here and save it for later use. Install MySQL first
Code:
Sudo apt-get install mysql-server mysql-client
Reinstall the development package
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)
You can test it with the following code
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 your 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)
}
This is the end of the article on "how to install MySQL under Ubuntu to get mysql.h to establish C interface". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.