Today we will show you how to use C ++ to operate mysql1 in linux: 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 to view mysql service status:
Service mysqld status start the service:
Service mysqld start to connect to the database:
Mysql2: Install gcc toolkit rpm-ivh gcc * -- force -- nodeps force installation 3: Enter mysqlcreate table t1 (id int, name varchar (30); insert data into table t1:
1. cpp
# Include <iostream>
# Include <mysql/mysql. h>
# Include <string>
Using namespace std;
Main ()
{
MYSQL 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 );
}
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;
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;
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 ()
{
MYSQL 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 <rowcount <endl;
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;
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;
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;
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