Connect to mysql database using C language

Source: Internet
Author: User

The operating system is Linux and requires the mysql client software development library libmysqlclient. This function library is one of the components of mysql. Generally, the header files are in the/usr/include/mysql path, and the library files are in the/usr/lib/mysql path. If you don't need to install the mysql-devel package (mysql-devel-5.1.47-4.el6.i686.rpm) [** @ **] # rpm-ivh after the mysql-devel-5.1.47-4.el6.i686.rpm is installed, you can go to the two paths to see if there is any, if not, find [** @ **] # find/-name mysql. h [** @ **] # find/-name mysqlclient to check the path. After installation, You can compile and connect the C source program. [** @ **] # Gcc-I/usr/include/mysql-L/usr/lib/mysql-lmysqlclient *. c can be run after compilation. Explanation: gcc gnu c/C ++ compiler-I specifies the path of the header file (uppercase I) -L specify the path of the library file-l link to the required dynamic link library (lower case L) for such a long compilation command, an environment variable [** @ **] # export LD_LIBRARY_PATH = $ LD_LIBRARY_PATH can be added to the dynamic link library in the make file: the/usrlib/mysql method is always: Add LD_LIBRARY_PATH = $ LD_LIBRARY_PATH:/usrlib/mysql in. the final source code in bashrc: [cpp] # include <stdio. h> # include <mysql. h> int main (int argc, char * argv []) {int I; MYSQL * conn; // connection to MySQL server MYSQL_RES * result ;/ /Result of SELECT query MYSQL_ROW row; // one record (row) of SELECT query // connect to MySQL conn = mysql_init (NULL); // mysql_options (conn, MYSQL_READ_DEFAULT_GROUP, "myclient"); if (mysql_real_connect (conn, "localhost", "root", "uranus", // change the username and password "mylibrary", 0, NULL, 0) = NULL) {fprintf (stderr, "sorry, no database connection... \ n "); return 1;} // only if utf8 output is needed mysql_qu Ery (conn, "set names 'utf8'"); // retrieve list of all publishers in mylibrary const char * SQL = "SELECT COUNT (titleID), publName \ FROM publishers, titles \ WHERE publishers. publID = titles. publID \ group by publishers. publID \ order by publName "; if (mysql_query (conn, SQL) {fprintf (stderr," % s \ n ", mysql_error (conn); fprintf (stderr, "Fehlernummer % I \ n", mysql_errno (conn); fprintf (stderr, "% s \ n ", SQL); return 1 ;}// process results result = mysql_store_result (conn); if (result = NULL) {if (mysql_error (conn) fprintf (stderr, "% s \ n", mysql_error (conn); else fprintf (stderr, "% s \ n", "unknown error \ n"); return 1 ;} printf ("% I records found \ n", (int) mysql_num_rows (result); // loop through all found rows while (row = mysql_fetch_row (result ))! = NULL) {for (I = 0; I <mysql_num_fields (result); I ++) {if (row [I] = NULL) printf ("[NULL] \ t"); else printf ("% s \ t", row [I]);} printf ("\ n ");} // de-allocate memory of result, close connection mysql_free_result (result); mysql_close (conn); return 0 ;}

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.