The implementation method of C language connection MySQL database _c language

Source: Internet
Author: User
Tags rar
I also have a copy of the internet found here:/201205/other/c_link_mysql51.rar
C Connect MySql5.1 required files. rar
A good example comes with:
Copy Code code as follows:

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <winsock2.h>
#include <mysql/mysql.h>/* note to include this header file * *
#pragma comment (lib, "Libmysql")
/* Defines a number of macros required for a database connection * *
#define HOST "localhost"
#define USERNAME "Root"
#define PASSWORD "123456"
#define DATABASE "Test"
/* This function is used to execute the incoming SQL language.
void Exe_sql (char* sql) {
MYSQL my_connection; /* This is a database connection * *
int res; /* The return flag after executing the SQL language sentence * *
/* Initialize MySQL connection my_connection*/
Mysql_init (&my_connection);
/* Here is a function in the mysql.h, using the macros we defined previously to establish a MySQL connection, and
Returns a value that is not NULL to prove that the connection is successful * *
if (Mysql_real_connect (&my_connection, HOST, USERNAME, PASSWORD,
DATABASE, 0, NULL, client_found_rows) {/* Connection successful *
printf ("Database performs EXE_SQL connection success!") n ");
/* This sentence is to set the query code for UTF8, so support Chinese * *
mysql_query (&my_connection, "Set names UTF8");
/* The following sentence is to use the Mysql_query function to execute the SQL sentence we just passed in,
This returns an int value and, if 0, proves that the sentence is executed successfully.
res = mysql_query (&my_connection, SQL);
if (res) {//* Now represents a failed execution * *
printf ("Error:mysql_query!\n");
/* Do not forget to close the connection * *
Mysql_close (&my_connection);
else {/* now represents successful execution. *
/*mysql_affected_rows returns the number of rows affected after executing SQL * *
printf ("%d rows are affected!") \ n ",
Mysql_affected_rows (&my_connection));
/* Do not forget to close the connection * *
Mysql_close (&my_connection);
}
} else {
/* Database connection Failed/*
printf ("Database failed to perform EXE_SQL connection!") \ n ");
}
}
/* This function is used to execute the incoming SQL language and print out the results of the query.
void Query_sql (char* sql) {
MYSQL my_connection; /* This is a database connection * *
int res; /* The return flag after executing the SQL language sentence * *
Mysql_res *res_ptr; /* Pointer to query result/*
Mysql_field *field; /* Field structure pointer/*
Mysql_row Result_row; /* Query information returned by line * *
int row, column; /* The number of rows and columns returned by the query * *
int I, J; /* Only two variables to control the loop * *
/* Initialize MySQL connection my_connection*/
Mysql_init (&my_connection);
/* Here is a function in the mysql.h, using the macros we defined previously to establish a MySQL connection, and
Returns a value that is not NULL to prove that the connection is successful * *
if (Mysql_real_connect (&my_connection, HOST, USERNAME, PASSWORD,
DATABASE, 0, NULL, client_found_rows) {/* Connection successful *
printf ("Database query Query_sql connection succeeded!") \ n ");
/* This sentence is to set the query code for UTF8, so support Chinese * *
mysql_query (&my_connection, "Set names UTF8");
* The following sentence is using the mysql_query function to execute the SQL sentence we just passed in,
This returns an int value and, if 0, proves that the sentence is executed successfully.
res = mysql_query (&my_connection, SQL);
if (res) {//* Now represents a failed execution * *
printf ("Error:mysql_query!\n");
/* Do not forget to close the connection * *
Mysql_close (&my_connection);
else {/* now represents successful execution. *
/* Bring the results of the query to res_ptr*/
Res_ptr = Mysql_store_result (&my_connection);
* * If the result is not empty, print*/the result
if (res_ptr) {
/* Get the results of the number of rows and * *
Column = Mysql_num_fields (RES_PTR);
row = Mysql_num_rows (res_ptr) + 1;
printf ("Query to%lu row \ n", row);
/* Output results of the field name * *
for (i = 0; field = Mysql_fetch_field (res_ptr); i++)
printf ("%st", field->name);
printf ("\ n");
/* Output results by line * *
for (i = 1; i < row; i++) {
Result_row = Mysql_fetch_row (res_ptr);
for (j = 0; j < column; J + +)
printf ("%st", Result_row[j]);
printf ("\ n");
}
}
/* Do not forget to close the connection * *
Mysql_close (&my_connection);
}
}
}
int main (int argc, char *argv[]) {
/* Test to insert data into the inside * *
Char *query;
Char *exe = "INSERT into student values (' Lala ', ' hahhahah! ');";
Exe_sql (EXE);
* Test under the query * *
query= "SELECT * from student;";
Query_sql (query);
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.