C-language operations on MySQL database

Source: Internet
Author: User

Original: C-language operation on MySQL database

This is already a fairly old topic. But today I first use, put some of today's experience to write down, perhaps to some novice to bring some help, more importantly for their own future forget how to use and consult! We're 1. Header files: #include <stdio.h> #include <stdlib.h> #include <mysql/mysql.h>//This is required to be included, the following m       All of Ysql's operation functions are from here. 2. Define a MySQL variable: mysql mysql; here MySQL is a variable to connect to the MySQL database. In the subsequent operation of the MySQL database, we use this MySQL variable as the handle. 3. Define database parameters: Char host[32]= "localhost";
Char user[32]= "username";
Char passwd[32]= "PWD";
Char dbname[32]= "TestDB"; 4. Database operations 1). Initialize the database:
Mysql_init (&mysql);
2). Connect to the database:
Mysql_real_connect (&mysql,host,user,passwd,dbname,0,null,0);
When we operate, we can perform an if test on the above functions, and if the initialization or connection error occurs, make the appropriate prompt for debugging.
5. Operations on the database:
mysql_query (&mysql, "select * from TestDB where condition");
In practice, we will use a function to make it easier to work with certain variables in the program:
int sprintf (char *str, const char *format, ...);
This function is used to format our string and then assigns the variable to the first parameter in the format you gave it.
We use this method to make it easy to use our variables to manipulate the database. For example, we're going to do a database query, and we can use it like this:
sprintf (SQL, "select * from TestDB where username = '%s '", u_name);
Then use mysql_query (&mysql, SQL) to query.

As you have noticed, the second parameter in the sprintf function is a SQL statement that we are very familiar with, except that we used a format character like the output function at the right end of the condition, because we used the variable. Of course, when there is no variable, we can omit the third argument, but then the effect of using sprintf disappears.
You must be more familiar with SQL statements than I do, so the examples of deletions and modifications are listed.
6. To close a database connection:
Mysql_close (&mysql); Maybe everyone has used PHP to operate on MySQL. Perhaps it has also been found that in the C language, the operation of the MySQL database and PHP have a lot of similarities. Here I have a small piece of code, to end this document! :) I hope you will give us your advice. #include <stdio.h>
#include <mysql/mysql.h>
#include <stdlib.h>
#include <errno.h>
#include <syslog.h>
MySQL MySQL;
Main () {
Char host[32]= "localhost"; MySQL Host
Char user[32]= "username";//mysql user name
Char passwd[32]= "PWD"; MySQL pwd
Char dbname[32]= "TestDB";//mysql db



if (Mysql_init (&mysql) = = NULL)
{
Syslog (log_user| Log_info, "inital MySQL handle error\n");
return 1;
}
if (Mysql_real_connect (&mysql,host,user,passwd,dbname,0,null,0) = = NULL)
{
Syslog (log_user| Log_info, "Failed to connect to Database:error:%s\n", Mysql_error (&mysql));
return 1;
}
else Syslog (log_user| Log_info, "Connect to database: \ n");
printf ("Connected to the db!\n");
int a = Find_ps ();
printf ("The Num is:%d\n", a);
Db_close ();
return 0;} int Db_close () {
Mysql_close (&mysql);
return 0;
}
int Find_ps () {
Mysql_row M_row;
Mysql_res *m_res;
Char sql[1024],username[32];
int Res=1;
int *id; sprintf (SQL, "select * from TestDB where user_name = '%s '", u_name);
if (mysql_query (&mysql,sql)! = 0)
{
Syslog (log_user| Log_info, "Select Ps_info Error:%s\n", Mysql_error (&mysql));
return res;
}
M_res = Mysql_store_result (&mysql);
if (m_res==null)
{
Syslog (log_user| Log_info, "Select username Error:%s\n", Mysql_error (&mysql));
res = 3;
return res;
}
}
Mysql_free_result (M_res);
return res;} 7.       Compile: Don't forget to add the library location, otherwise the compilation will not pass! Gcc-l/usr/lib/mysql–lmysqlclient func.c-o func hehe, don't forget Oh!

C-language operations on MySQL database

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.