C Connect Database MySQL

Source: Internet
Author: User

sudo apt-get install mysql-server mysql-client

Re-install the development package
Code:
sudo apt-get install Libmysqlclient15-dev

After installation, add header file in C code


Code:
#include <mysql.h>

Compile method:
Code:
GCC $ (mysql_config--cflags) xxx.c-o xxx $ (mysql_config--libs)


You can test it with the following code
Code:

#include <mysql.h>
#include <stdio.h>

Main () {
MYSQL *conn;
Mysql_res *res;
Mysql_row ROW;

char *server = "localhost";
Char *user = "root";
Char *password = "";
Char *database = "MySQL";

conn = Mysql_init (NULL);


if (!mysql_real_connect (conn, server,
User, password, database, 0, NULL, 0)) {
fprintf (stderr, "%s\n", MYSQL_ERROR (conn));
Exit (1);
}


if (mysql_query (conn, "Show Tables")) {
fprintf (stderr, "%s\n", MYSQL_ERROR (conn));
Exit (1);
}

res = MYSQL_USE_RESULT (conn);


printf ("MySQL Tables in MySQL database:\n");
while (row = mysql_fetch_row (res)) = NULL)
printf ("%s \ n", row[0]);


Mysql_free_result (RES);
Mysql_close (conn);
}

Http://blog.sina.com.cn/s/blog_494e45fe0100k9p8.html

C Connect Database MySQL

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.