Connect to MySQL in C language in Windows

Source: Internet
Author: User

1. install libmysql In the MySQL directory. DLL is copied to c: \ windows \ system32 \ (my name is win7, but also to the: C: \ WINDOWS \ syswow64 directory );

2. Add the MySQL header file to the environment:

3. Add libmysql. lib to the vs 2010 Project.

4. Sample Code:

/*************************************** * ********: Yang Zhiyong ** Date: 2012-05-17 *** Email: ljy520zhiyong@163.com ** QQ: 929168233 *** file name: MySQL. c *** function: connect to MySQL and query ************************************ * ************/# include <stdio. h> # include <stdlib. h> # include <MySQL. h> // include the header file required by MySQL # include <winsock2.h> // socket. MySQL, * sock, is also required to connect to MySQL; // declare the MySQL handle int main (void) {const char * Host =" 127.0.0.1 "; // The local ipconst char * user =" root "is filled in because it is tested as a local machine; // change it to your user name here, that is, the username const char * passwd = "yangfamily" connecting to MySQL; // change the password here to const char * DB = "testmysql "; // The Name Of The database you want to connect to is unsigned int Port = 3306. // This is the port of the MySQL server. If you have not modified it, It is 3306. Const char * unix_socket = NULL; // unix_socket this is Unix. In Windows, set it to nullunsigned long client_flag = 0; // this parameter is generally 0 const char * I _query = "select * from test"; // query statement mysql_res * result; // Save the mysql_row row of the result set; // indicates a row in the result set mysql_init (& MySQL); // This function must be used before connection to initialize if (sock = mysql_real_connect (& MySQL, host, user, passwd, DB, port, unix_socket, client_flag) = NULL) // connection to MySQL {printf ("connection failed because :\ N "); fprintf (stderr," % s \ n ", mysql_error (& MySQL); exit (1) ;}else {fprintf (stderr," MySQL connection successful !! \ N ");} If (mysql_query (& MySQL, I _query )! = 0) // If the connection is successful, query {fprintf (stderr, "query failed! \ N "); exit (1) ;}else {If (result = mysql_store_result (& MySQL) = NULL) // Save the query result {fprintf (stderr, "An error occurred while saving the result set! \ N "); exit (1) ;}else {While (ROW = mysql_fetch_row (result ))! = NULL) // read the data in the result set and return the next row. Because the current cursor is in the first row [before] {printf ("name is % s \ t", row [0]) when saving the result set; // print the data in the first column of the current row printf ("Age is % s \ t \ n", row [1]); // print data in the second column of the current row} mysql_free_result (result); // release the result set mysql_close (sock); // close the connection to system ("pause "); exit (exit_success );}

If the following occurs during compilation:

Error lnk2019: An error similar to the external symbol _ mysql_init @ 4 that cannot be parsed. Make sure that libsql. Lib is added in step 3.

5. My test results:

Data in MySQL:

Data queried during programming:

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.