A/C + + connection query MySQL

Source: Internet
Author: User

1. [Code][c/c++] Code

#include <mysql/mysql.h>
#include <stdio.h>
#include <string.h>
int main ()
{
MYSQL mysql;//connecting the database variables
Mysql_res *res;//variables that store query results
Mysql_row ROW;
Char *query;
int t,r;
Char *server= "localhost";
Char *user = "root";
Char *password= "Test";
Char *database = "Test";
Mysql_init (&mysql); You must call this function to initialize a variable before connecting to the database
/**
* MySQL *mysql_init (MySQL *mysql)
* Incoming MySQL type pointer
* Returns an initialized mysql* handle
* Returns null in case of insufficient memory
*/
if (!mysql_real_connect (&mysql,server,user,password,database,0/*tcp IP Port */,null/*unix socket connection Type */,0/* Run as an ODBC database tag */))
{
printf ("Error connection to database:%s/n", Mysql_error (&mysql));
}else
printf ("connected.../n");
query = "SELECT * from T1";
printf ("Query execute:%s/n", query);
t = Mysql_real_query (&mysql,query, (unsigned int) strlen (query));
/**
*
* int stdcall mysql_real_query (mysql *mysql,const char *q, unsigned int length);
* Execute any SQL query statement to query.
* MySQL is the variable we used before, q is the SQL query statement, length is the size of this query statement.
*/
if (t)
{
printf ("Error making query:%s/n", Mysql_error (&mysql));
}else
printf ("[%s] made.../n", query);
res = Mysql_store_result (&mysql);
/**
* Mysql_store_result (MySQL *mysql)
* Retrieve all results immediately
*
* Mysql_use_result (MySQL *mysql)
* Initialize a row of rows to retrieve the result set
*/
while (row = mysql_fetch_row (res))
{
/**
* Mysql_row stdcall mysql_fetch_row (mysql_res *result);
* Retrieving rows
*/
For (T=0;t<=mysql_num_fields (res); t++)
{
printf ("%s", Row[t]);
}
printf ("/n");
}
printf ("mysql_free_result.../n");
Mysql_free_result (RES);
/**
* Mysql_free_result ()
* Release the memory used by it.
*/
Sleep (1);
Execute the Insert Query
query = "INSERT INTO T1 (id,name) VALUES (3, ' KUNP ')";
t = Mysql_real_query (&mysql,query, (unsigned int) strlen (query));
if (t) http://www.huiyi8.com/hunsha/hanshi/
{Korean wedding photos
printf ("Error making query:%s/n", query);
Mysql_error (&mysql);
}
else printf ("[%s] made.../n", query);
Mysql_close (&mysql);
/**
* void Mysql_close (MySQL *mysql)
* Incoming MySQL type pointer
* function, close a server connection and release the memory associated with the connection I
*/
return 1;
}
Compiling: gcc testsql.c-i/usr/include/mysql-l/usr/lib/mysql-lmysqlclient-o testsql
2. [Code][c/c++] Code
#include <stdlib.h>
#include <stdio.h>
#include <mysql/mysql.h>
int main (void) {
Mysql_res *res_ptr;
Mysql_row Sqlrow;
MYSQL *conn_ptr;
int result;//query Result
int itablerow,itablecol,i,j;
Conn_ptr = Mysql_init (NULL);
if (!conn_ptr)
{
fprintf (stderr, "Mysql_init failed!\n");
Return (-1);
}
Conn_ptr = Mysql_real_connect (conn_ptr, "127.0.0.1", "root", "123456", "MySQL", 3306,0,null);
if (conn_ptr)
{
printf ("Connection succeed!\n");
}
else{
printf ("Connection failed!\n");
Return (-2);
}
result = mysql_query (conn_ptr, "select * from Test.tbl_user");
if (result) {
printf (stderr, "Select Error%d:%s!\n", Mysql_errno (Conn_ptr), Mysql_error (conn_ptr));//
return exit_failure;
}
Res_ptr = Mysql_store_result (conn_ptr);//Collection
if (res_ptr)
{
Itablerow = mysql_num_rows (res_ptr);//Line
Itablecol = Mysql_num_fields (res_ptr);//Column
for (i=0; i<itablerow; i++)
{
Sqlrow = Mysql_fetch_row (res_ptr);
for (j=0; j<itablecol; j + +)
{
printf ("%-8s", Sqlrow[j]);//String left, right fill space
}
printf ("\ n");
}
Mysql_free_result (res_ptr);//After you complete all operations on the data, call this function to have the MySQL library clean up its assigned objects
Mysql_close (CONN_PTR);

printf ("Connection close!\n");
return 0;


Compilation: Gcc-o mydbcon-i/usr/include/mysql/-l/usr/lib/mysql-lmysqlclient mysql.c

Run:./mydbcon

A/C + + connection query MySQL

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.