Operate MySQL databases in C language, and connect, insert, modify, and delete databases

Source: Internet
Author: User

The following code connects to the 9tmd_bbs_utf8 database on the local MySQL server. The user name is obtained from the tbb_user table based on the input userid and printed and output to the terminal.

The code is as follows: Copy code

# 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, "Couldn't 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, "Couldn't 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;

// Add this line to ensure compatibility with most compilers
}

When compiling, use the following command

The code is as follows: Copy code

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

The code is as follows: Copy code
./Mysql_select 1

The following result is returned:

The code is as follows: Copy code

Number of fields returned: 1
Ther userid #1's username is: Michael
Query OK!

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.