How to operate MySQL databases using C language

Source: Internet
Author: User
Tags mysql functions

We all know that using C language to operate MySQL databases is a very troublesome task. We need to perform operations related to connection, insertion, modification, and deletion, of course, many people will use the MySQL database to develop some projects.

Sometimes, for the sake of performance, we will directly use the C language to develop relevant modules, especially in our web applications, although PHP, JSP and other scripts provide MySQL interfaces, however, it is clear that the direct use of C language has better security and performance. Michael used to use this type of interface written in C language in multiple projects developed using PHP, and then compiled it into php, for php scripts to be used directly, this topic will not be discussed. Next we will mainly talk about how to connect to the MySQL database using C language in Linux, read the data returned in it, and compile it at the same time.

Most of the code here is referenced in the MySQL release package. c source file. You can also find the relevant code in it. The following code connects to the 9tmd_bbs_utf8 database on the local MySQL server, obtain the user name from the table tbb_user based on the input userid and print the user name to the terminal.

If defined (_ WIN32) | defined (_ WIN64) to support Compilation on windows Platforms

 
 
  1. #include <windows.h> 
  2. #endif  
  3. #include <stdio.h> 
  4. #include <stdlib.h> 
  5. #include "mysql.h" 

On my machine, the file is in/usr/local/include/mysql.

Define the macros for MySQL database operations, or write them directly into the code without defining them.

 
 
  1. Define SELECT_QUERY "select username from tbb_user where userid = % d"
  2. Int main (int argc, char ** argv) char ** argv is equivalent to char * argv []
  3. {

MYSQL mysql, * sock; defines the database connection handle, which is used by almost all MySQL Functions

MYSQL_RES * res; query result set, structure type

MYSQL_FIELD * fd; structure containing field information

MYSQL_ROW row; a string array that stores the query results of a row

Char qbuf [160]; stores query SQL statement strings

 
 
  1. If (argc! = 2) {// check the input parameters
  2. Fprintf (stderr, "usage: mysql_select <userid> \ n ");
  3. Exit (1 );
  4. }
  5. Mysql_init (& mysql );
  6. If (! (Sock = mysql_real_connect (& mysql, "localhost", "dbuser", "dbpwd", "9tmd_bbs_utf8", 0, NULL, 0 ))){
  7. Fprintf (stderr, "Couldn't connect to engine! \ N % s \ n ", mysql_error (& mysql ));
  8. Perror ("");
  9. Exit (1 );
  10. }
  11. Sprintf (qbuf, SELECT_QUERY, atoi (argv [1]);
  12. If (mysql_query (sock, qbuf )){
  13. Fprintf (stderr, "Query failed (% s) \ n", mysql_error (sock ));
  14. Exit (1 );
  15. }
  16. If (! (Res = mysql_store_result (sock ))){
  17. Fprintf (stderr, "Couldn't get result from % s \ n", mysql_error (sock ));
  18. Exit (1 );
  19. }
  20. Printf ("number of fields returned: % d \ n", mysql_num_fields (res ));
  21. While (row = mysql_fetch_row (res )){
  22. Printf ("Ther userid # % d's username is: % s \ n", atoi (argv [1]), (row [0] = NULL) &&(! Strlen (row [0])? "NULL": row [0]);
  23. Puts ("query OK! \ N ");
  24. }
  25. Mysql_free_result (res );
  26. Mysql_close (sock );
  27. Exit (0 );
  28. Return 0;

To be compatible with most compilers, add this line
}
When compiling, use the following command

Gcc-o mysql_select. /mysql_select.c-I/usr/local/include/mysql-L/usr/local/lib/mysql-lmysqlclient (-lz) (-lm) the following two options are optional, execute the following command when running according to your environment

./Mysql_select 1

The following result is returned:

 
 
  1. number of fields returned: 1  
  2. Ther userid #1 's username is: Michael  
  3. query ok ! 

Most of the above Code can be understood. If you do not understand it, please refer to the C language API documentation provided by MySQL. Each function has a detailed description, I have time to sort out a frequently used API description.

The above content is an introduction to using C language to operate MySQL databases.

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.