How to access MySQL database in C Language

Source: Internet
Author: User

How to access MySQL database in C Language

1. Add the header file path (include path in MySQL installation path)

2. Add a library file (copy libmysql. lib directly from the MySQL installation path)

3. programming Database Operations

Code

// AccessToMySQL. cpp: Specifies the entry point for the console application. // # Include "stdafx. h "# include <Windows. h> # include <mysql. h> # pragma comment (lib, "libmysql. lib ") MYSQL mysql; MYSQL_RES * result; MYSQL_ROW row; int main (void) {// init the mysql parametermysql_init (& mysql); // connect the database if (! Mysql_real_connect (& mysql, "127.0.0.1", "root", "111", "mytest", 3306, NULL, 0) {printf (mysql_error (& mysql )); printf ("\ nCannot access to the database !!! \ N "); system (" pause "); exit (-1);} // construct the query SQL statementschar * SQL =" select * from student where name = '"; char dest [100] = {""}; strcat (dest, SQL); printf ("Please enter the student name :"); char name [10] = {""}; gets (name); strcat (dest, name); strcat (dest ,"'"); // excute the SQL statementsif (mysql_query (& mysql, dest) {printf ("Cannot access the database with excuting \" % s \". ", dest); system (" pause "); exit (-1);} // deal with the resultresult = mysql_store_result (& mysql); if (mysql_num_rows (result )) {while (row = mysql_fetch_row (result) {printf ("% s \ t % s \ n", row [0], row [1], row [2]) ;}}// release the resourcemysql_free_result (result); mysql_close (& mysql); system ("pause"); return 0 ;}


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.