Connection
connection.cpp
#include <sqlplus.hh> // or <mysql++>
int main ()
{
Connection conn ("user1", // db_name
"localhost", // host_name
"user1", // user_login
"user111" // user_password
);
cout << "Connection \"OK\"" << endl;
conn.close();
return 0;
}
compile: g++ -g -o conn.exe connection.cpp -lsqlplus
$ ./conn.exe
OUTPUT:
// Connection "OK"
hide_password.cpp
Hiding 'password' from your program.
1. create file $HOME/.password
2. just one single line : your_real_password
3. chmod 400 $HOME/.password
( you have to have account on host
and $HOME/.my.cnf file )
#include <sqlplus.hh>
#include <fstream> // to read file $HOME/.password
int main ()
{
char pass[10];
ifstream In ("/home/ana/.password");
In >> pass;
In.close();
Connection conn ("user1","","",pass);
cout << "Connection \"OK\"" << endl;
conn.close();
return 0;
}
$ g++ -g -o hide_passw.exe hide_password.cpp -lsqlplus
$ ./hide_passw.exe
OUTPUT:
// Connection "OK"
use_header.cpp
Create shared library libpassword
#include <sqlplus.hh>
#include <password.hh> // user created shared library
int main ()
{
Connection conn ("user1","","",password());
// return from function
cout << "Connection \"OK\"" << endl;
conn.close();
return 0;
}
$ g++ -g -o hide_passw.exe use_header.cpp -lsqlplus
$ ./use_header.exe
OUTPUT:
// Connection "OK"
Data Process
query.cpp
#include <sqlplus.hh>
#include <password.hh>
int main ()
{
Connection conn ("user1","","",password());
Query q = conn.query(); // create query and
// bound to the 'conn'
q << "SELECT DATABASE()"; // query itself
cout << "Query : " << q.preview() << endl;
// returns query string
Result res = q.store(); // executes query and
// return result into 'res'
Row row = res[0]; // we need just one, first record
cout << endl << "The name of DATABASE is : "
<< row[0] << endl;
conn.close();
return 0;
}
g++ -o query.exe query.cpp -lsqlplus -lpassword
./query.exe
OUTPUT:
// Query : SELECT DATABASE()
//
// The name of DATABASE is : user1
insert_data.cpp
#include <sqlplus.hh>
#include <password.hh>
int main ()
{
Connection conn ("user1","","",password());
Query q = conn.query();
q << "DROP TABLE IF EXISTS table1";
q.execute(); // execute query
q << "CREATE TABLE table1 (ID VARCHAR(7) "
<< "NOT NULL PRIMARY KEY, "
<< "l_name VARCHAR(20) NOT NULL, "
<< "f_name VARCHAR(20))";
q.execute();
q << "INSERT INTO table1 VALUES ('ABC123',"
<< "'Frost','Robert')";
q.execute();
q << "INSERT INTO table1 (ID,l_name) VALUES "
<< "('ABD000','Smith')";
q.execute();
q << "show tables" << endl;
cout << "Query : " << q.preview() << endl;
Result res = q.store();
if ( !res.empty() )
{
Row row = res[0];
cout << "Table is : " << row[0] << endl;
}
conn.close();
return 0;
}
OUTPUT:
// Query : show tables
//
// Table is : table1
retrieve_data.cpp
#include <sqlplus.hh>
#include <password.hh>
#include <iomanip>
int main ()
{
Connection conn ("user1","","",password());
Query q = conn.query();
q << "SELECT * FROM table1";
cout << "Query : " << q.preview() << endl
<< endl;
Result res = q.store();
Row row;
Result::iterator Iter; // Random Access Iterator
cout << " ID L_NAME F_NAME" << endl;
cout << "==================================" << endl;
for ( Iter = res.begin(); Iter != res.end(); Iter++ )
{
row = *Iter;
cout.setf(ios::left);
cout << setw(10) << row[0].c_str()
<< setw(17) << row[1].c_str()
<< row[2] << endl;
}
cout << endl;
conn.close();
return 0;
}
OUTPUT:
// Query : SELECT * FROM table1
//
// ID L_NAME F_NAME
// ==================================
// ABC123 Frost Robert
// ABD000 Smith NULL
update.cpp
#include <sqlplus.hh>
#include <password.hh>
#include <iomanip>
int main ()
{
Connection conn ("user1","","",password());
Query q = conn.query();
q << "UPDATE table1 SET f_name='Linda' WHERE "
<< "l_name='Smith' AND ID='ABD000'";
cout << "Query : " << q.preview() << endl
<< endl;
q.execute();
q << "SELECT * FROM table1";
cout << "Query : " << q.preview() << endl
<< endl;
Result res = q.store();
Row row;
Result::iterator Iter;
cout << " ID L_NAME F_NAME" << endl;
cout << "==================================" << endl;
for ( Iter = res.begin(); Iter != res.end(); Iter++ )
{
row = *Iter;
cout.setf(ios::left);
cout << setw(10) << row[0].c_str()
<< setw(17) << row[1].c_str()
<< row[2] << endl;
}
cout << endl;
conn.close();
return 0;
}
OUTPUT:
// Query : UPDATE table1 SET f_name='Linda'
// WHERE l_name='Smith' AND ID='ABD000'
//
// Query : SELECT * FROM table1
//
// ID L_NAME F_NAME
// ==================================
// ABC123 Frost Robert
// ABD000 Smith Linda
Generating Query
run_time_query.cpp
#include <sqlplus.hh>
#include <password.hh>
#include <strstream> // for ostrstream
#include <cstdio> // for sprintf
#include <publib.h> // for stracat
using namespace std;
int main ()
{
// using MysqlQuery object
Connection conn("test","","",password());
Query q = conn.query();
Date d = "6/12/2002";
q << "SELECT * FROM table1 WHERE t_date='"
<< d << "'" << endl;
cout << "Query q: " << q.preview() << endl;
// generating Query using ostrstream object
int num = 25;
string f_name("Robert");
ostrstream *msg = new ostrstream;
*msg << "SELECT * FROM table1 WHERE f_name='"
<< f_name << "' LIMIT " << num << ends;
cout << "ostrstream: " << msg->str() << endl;
// using sprintf function
char *name = "Albert";
char buff[255];
sprintf(buff,"SELECT FROM table1 WHERE id=%d AND
name='%s'",num,name);
cout << "sprintf: " << buff << endl;
// using stracat function (publib.h)
sprintf(buff,"%d",num);
char *query = stracat("DELETE FROM table1 WHERE
name='", name, "' AND id=", buff,(char*)NULL);
cout << "publib: " << query << endl;
return 0;
}
$ g++ run_time_query.cpp -lsqlplus -lpassword -lpub
$ ./a.out
OUTPUT:
// Query q: SELECT * FROM table1 WHERE
// t_date='0006-00-00'
//
// ostrstream: SELECT * FROM table1 WHERE
// f_name='Robert' LIMIT 25
// sprintf: SELECT FROM table1 WHERE id=25 AND
// name='Albert'
// publib: DELETE FROM table1 WHERE
// name='Albert' AND id=25
Exception Hendling
exception.cpp
#include <sqlplus.hh>
#include <password.hh>
using namespace std;
int main ()
{
try
{
Connection conn(use_exceptions);
conn.connect("user3","","",password());
bool b = conn.connected();
if ( b )
cout << "OK" << endl;
else
cout << "Anreached string!" << endl;
conn.close();
}
catch ( BadQuery &err )
{
#ifdef USE_STANDARD_EXCEPTION
cerr << "Error: " << err.what() << endl;
cerr << "(BadQuery)" << endl;
#else
cerr << "Error: " << err.error << endl;
cerr << "(BadQuery)" << endl;
#endif
return -1;
}
catch ( BadConversion &err )
{
#ifdef USE_STANDARD_EXCEPTION
cerr << "Error: " << err.what() << endl
<< "retrieved data size: " << err.retrived
<< " actual data size: " << err.actual_size
<< endl;
cerr << "(BadConversion)" << endl;
#else
cerr << "Error: Tried to convert \""
<< err.data << "\" to a \""
<< err.type_name << "\"." << endl;
cerr << "(BadConversion)" << endl;
#endif
return -1;
}
#ifdef USE_STANDARD_EXCEPTION
catch ( exception &e )
{
cerr << "Error: " << e.what() << endl;
return -1;
}
#endif
return 0;
}
// Error: Access denied for user: 'ana@localhost'
// to database 'user3'
// (BadQuery)