In Linux, C ++ accesses the mysql-art_hero-51cto technical blog

Source: Internet
Author: User

In Linux, C ++ accesses the mysql-art_hero-51cto technical blog

Access MySQL through C ++ in Linux

16:39:16

Tags: C ++ Linux database MySQL casual original works, can be reproduced, please be sure to mark in the form of Hyperlink during ReprintingArticleOriginal source, author information, and this statement. Otherwise, legal liability will be held. Http://curran.blog.51cto.com/2788306/533169

Today, we will show you how to use C ++ to operate MySql in Linux.1: Install the MySQL mounting disc: mkdir/CDROM
Mount/dev/HDC/CDROM
CD/CDROM/Server
Rpm-IVH perl-DBI-1.52-2.el5.i386.rpm
Rpm-IVH mysql-5.0.45-7.el5.i386.rpm mysql-bench-5.0.45-7.el5.i386.rpm mysql-devel-5.0.45-7.el5.i386.rpm
Rpm-IVH perl-DBD-MySQL-3.0007-2.el5.i386.rpm
Rpm-IVH mysql-server-5.0.45-7.el5.i386.rpm mysql-test-5.0.45-7.el5.i386.rpm

View MySQL service status: Service mysqld status

Start Service: Service mysqld start

connect to the database: mysql2: Install the GCC toolkit rpm-ivh gcc * -- force -- nodeps forced installation 3: Enter mysqlcreate table T1 (ID int , name varchar (30); data inserted in Table T1: 1.cpp< br> # include
# include
# include
using namespace STD;
main ()
{< br> MySQL;
mysql_init (& MySQL);
mysql_real_connect (& MySQL, "localhost" , "root" , "root" , "test" , 3306, null, 0);
string SQL = " insert into T1 (ID, name) values (1, 'java1'); ";
mysql_query (& MySQL, SQL. c_str ();
mysql_close (& MySQL );
}< br> G ++-O 1.out 1.cpp-lmysqlclient-I/usr/include/MySQL/-L/usr/lib/MySQL

Update Mysql Data. 1.cpp
# Include <iostream>
# Include <MySQL/MySQL. h>
# Include <string>

Using namespace STD;

Main ()
{
MySQL;
Mysql_init (& MySQL );
Mysql_real_connect (& MySQL, "Localhost" , "Root" , "Root" , "Test" , 3306, null, 0 );
String SQL = "Insert into T1 (ID, name) values (2, 'java2 '), (3, 'java3 ');" ;
Mysql_query (& MySQL, SQL. c_str ());
SQL = "Update T1 set name = 'java33' where id = 3 ;" ;
Mysql_query (& MySQL, SQL. c_str ());
Mysql_close (& MySQL );
}
G ++-O 1.out 1.cpp-lmysqlclient-I/usr/include/MySQL/-L/usr/lib/MySQL
./1.out MySQL stored procedure: mysql> delimiter //
> Create procedure p01 ()
> Begin
> Insert into T1 (ID, name) values (66, 'java66 ');
> End;
> //

Application stored procedure:

Mysql> delimiter //
> Create procedure p01 ()
> Begin
> Insert into T1 (ID, name) values (66, 'java66 ');
> End;
> // Trigger:
Create Table t2: mysql> delimiter //
> Create trigger tr1 after insert on T1 For Each row
> Begin
> Insert into T2 (ID, name) values ( New . ID, New . Name );
> End;
> //
> Delete from T1 where id = 66;
> //
> Delimeter; VI 3.cpp
# Include <iostream>
# Include <string>
# Include <MySQL/MySQL. h>

Using namespace STD;

Main ()
{
MySQL;
Mysql_init (& MySQL );
Mysql_real_connect (& MySQL,"Localhost","Root","Root", 3306, null, 0 );
String SQL ="Call p01 ();";
Mysql_query (& MySQL, SQL. c_str ());
Mysql_close (& MySQL );
}

G ++-O 3.out 3.cpp-lmysqlclient-I/usr/include/MySQL/-L/usr/lib/MySQL
./3. outmysql> delimeter;
> Select * from T1;
> Select * From T2;
> Check the total number of data in the table (66, 'java66'): VI 4.cpp
# Include <iostream>
# Include <string>
# Include <MySQL/MySQL. h>

Using namespace STD;

main ()
{< br> MySQL;
mysql_res * result = NULL;
mysql_init (& MySQL );
mysql_real_connect (& MySQL, "localhost" , " root ", " root ", " test ", 3306, null, 0);
string SQL = " select ID, name from T1; ";
mysql_query (& MySQL, SQL. c_str ();
result = mysql_store_result (& MySQL);
int rowcount = mysql_num_rows (result);
cout mysql_close (& MySQL );
}

G ++-O 4.out 4.cpp-lmysqlclient-I/usr/include/MySQL/-L/usr/lib/MySQL
./4. Total number of fields to be investigated: VI 5.cpp
# Include <iostream>
# Include <string>
# Include <MySQL/MySQL. h>

Using namespace STD;

Main ()
{
MySQL;
Mysql_res * result = NULL;
Mysql_init (& MySQL );
Mysql_real_connect (& MySQL, "Localhost" , "Root" , "Root" , 3306, null, 0 );
String STR ="Select ID, name from T1 ;" ;
Mysql_query (& MySQL, SQL. c_str ());
Result = mysql_store_result (& MySQL );
Int Rowcount = mysql_num_rows (result );
Cout <rowcount <Endl;
Int Fieldcount = mysql_num_fields (result );
Cout <fieldcount <Endl;
Mysql_close (& MySQL );
}

G ++-O 5.out 5.cpp-lmysqlclient-I/usr/include/MySQL/-L/usr/lib/MySQL
./5. Out to list specific field names: VI 6.cpp
# Include <iostream>
# Include <string>
# Include <MySQL/MySQL. h>

Using namespace STD;

Main ()
{
MySQL;
Mysql_res * result = NULL;
Mysql_field * field = NULL;
Mysql_init (& MySQL );
Mysql_real_connect (& MySQL, "Localhost" , "Root" , "Root" , "Test" , 3306, null, 0 );
String STR = "Select ID, name from T1 ;" ;
Mysql_query (& MySQL, SQL. c_str ());
Result = mysql_store_result (& MySQL );
Int Rowcount = mysql_num_rows (result );
Cout <rowcount <Endl;
Int Fieldcount = mysql_num_fields (result );
Cout <fieldcount <Endl;
For ( Int I = 0; I <fieldcount; I ++)
{
Field = mysql_fetch_field_direct (result, I );
Cout <field-> name < "\ T" ;
}
Cout <Endl;
Mysql_close (& MySQL );
}

G ++-O 6.out 6.cpp-lmysqlclient-I/usr/include/MySQL/-L/usr/lib/MySQL
./6. Out: All data in the table is displayed: VI 7.cpp
# Include <iostream>
# Include <string>
# Include <MySQL/MySQL. h>

Using namespace STD;

Main ()
{
MySQL;
Mysql_res * result = NULL;
Mysql_field * field = NULL;
Mysql_init (& MySQL );
Mysql_real_connect (& MySQL, "Localhost" , "Root" , "Root" , 3306, null, 0 );
String STR = "Select ID, name from T1 ;" ;
Mysql_query (& MySQL, SQL. c_str ());
Result = mysql_store_result (& MySQL );
Int Rowcount = mysql_num_rows (result );
Cout <rowcount <Endl;
Int Fieldcount = mysql_num_fields (result );
Cout <fieldcount <Endl;
For ( Int I = 0; I <fieldcount; I ++)
{
Field = mysql_fetch_field_direct (result, I );
Cout <field-> name < "\ T" ;
}
Cout <Endl;
Mysql_row ROW = NULL;
Row = mysql_fetch_row (result );
While (Null! = Row)
{
For ( Int I = 1; I <fieldcount; I ++)
{
Cout <row [I] < "\ T" ;
}
Cout <Endl;
Row = mysql_fetch_row (result );
}
Mysql_close (& MySQL );
}

G ++-O 7.out 7.cpp-lmysqlclient-I/usr/include/MySQL/-L/usr/lib/MySQL

./Out

This article is from the art_hero blog, please be sure to keep this source http://curran.blog.51cto.com/2788306/533169

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.