Connect to MySQL database with C language on Linux platform

Source: Internet
Author: User
Tags mysql client mysql in mysql version

  1. Install the Compilation tool----This installs BASIC programming tools such as Gcc/g++/gdb/make:

    sudo apt-get install build-essential

  2. Enter the command "sudo apt-get install MySQL", and then double-click the 2 tab key to view information about MySQL in your current software source.

  3. I choose to install:

    sudo apt-get install mysql-server mysql-client

  4. Enter the command "sudo apt-get install Libmysql", and then double-click the 2 tab key to view information about Libmysql in your current software source.

  5. Other programs when compiling the library and header files that are linked using MySQL, I choose to install Libmysqlclient-dev (choose to install the other, but not necessarily all can compile through):

    sudo apt-get install Libmysqlclient-dev

  6. To view the installed MySQL version:

    MySQL--version

    The location of the header files needed to connect to the MySQL client function library:

    Mysql_config--cflags

    Where to store the library files needed to connect to the MySQL client function library

    Mysql_config--libs

  7. The test code is as follows:

    Query operations

    #include <mysql/mysql.h>

    #include <stdio.h>

    #include <stdlib.h>

    #define HOST "localhost"

    #define USERNAME "User name"

    #define PASSWORD "Password"

    #define DATABASE "Publicresourcesdb"

    int main (void)

    {

    MySQL MySQL;

    Mysql_row ROW;

    Mysql_res *result;

    unsigned int num_fields;

    unsigned int i;

    Mysql_init (&mysql);

    if (!mysql_real_connect (&mysql, HOST, USERNAME, PASSWORD, DATABASE, 0, NULL, 0))

    {

    printf ("Connection failed,%s\n", Mysql_error (&mysql));

    }

    mysql_query (&mysql, "Set names UTF8");

    if (!mysql_query (&mysql, "select * from"))

    {

    result = Mysql_store_result (&mysql);

    if (!result)

    {

    Perror ("result error.");

    Exit (1);

    }

    Num_fields = Mysql_num_fields (result);

    while (row = mysql_fetch_row (result))

    {

    for (i=0; i<num_fields; i++)

    {

    printf ("%s\t", Row[i]);

    }

    printf ("\ n");

    }

    Mysql_free_result (result);

    }

    Mysql_close (&mysql);

    return 0;

    }

  8. Compile command:

    Gcc-o mysql_test mysql_test.c ' mysql_config--cflags--libs '

    (or Gcc-o mysql_test mysql_test.c-lmysqlclient)

    To run the command:

    ./mysql_test

    The results after the run are the same as the results of the MySQL Workbench query.

Connect to MySQL database with C language on Linux platform

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.