Operate MySQL Databases in C language, and perform operations such as connection, insertion, modification, and deletion.

Source: Internet
Author: User
Tags mysql functions

Many people use MySQL to develop some projects. Sometimes, for the sake of performance, we directly use C language to develop related modules, especially in our web applications, although PHP, JSP, and other scripts provide MySQL interfaces, it is clear that using C language directly has better security and performance, michael used to use this type of interface written in C language in multiple projects developed using PHP before compiling it into php for direct use by php scripts, I will not talk about this topic much. Next I will mainly talk about how to connect to the MySQL database with 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
# Include <windows. h>
# Endif
# Include <stdio. h>
# Include <stdlib. h>
# Include "mysql. h" // on my machine, the file is in/usr/local/include/mysql.
 
// Define the macros for database operations, or write them directly into the code without defining them.
# Define SELECT_QUERY "select username from tbb_user where userid = % d"
 
Int main (int argc, char ** argv) // char ** argv is equivalent to char * argv []
{
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; // string array that stores the query results of a row
Char qbuf [160]; // stores the query SQL statement string

If (argc! = 2) {// check the input parameters
Fprintf (stderr, "usage: mysql_select <userid> nn ");
Exit (1 );
}

Mysql_init (& mysql );
If (! (Sock = mysql_real_connect (& mysql, "localhost", "dbuser", "dbpwd", "9tmd_bbs_utf8", 0, NULL, 0 ))){
Fprintf (stderr, "Couldnt connect to engine! N % snn ", mysql_error (& mysql ));
Perror ("");
Exit (1 );
}

Sprintf (qbuf, SELECT_QUERY, atoi (argv [1]);
If (mysql_query (sock, qbuf )){
Fprintf (stderr, "Query failed (% s) n", mysql_error (sock ));
Exit (1 );
}

If (! (Res = mysql_store_result (sock ))){
Fprintf (stderr, "Couldnt get result from % sn", mysql_error (sock ));
Exit (1 );
}

Printf ("number of fields returned: % dn", mysql_num_fields (res ));

While (row = mysql_fetch_row (res )){
Printf ("Ther userid # % d s username is: % sn", atoi (argv [1]), (row [0] = NULL )&&(! Strlen (row [0])? "NULL": row [0]);
Puts ("query OK! N ");
}

Mysql_free_result (res );
Mysql_close (sock );
Exit (0 );
Return 0; // to be compatible with most compilers
}
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, based on your environment
Run the following command

./Mysql_select 1
The following result is returned:

Number of fields returned: 1
Ther userid #1 s username is: Michael
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, when I have time to sort out a common API description

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.