#include <sqlplus.hh>
#include <password.hh>
#include <iomanip>
#include <string>
using namespace std;
int main ()
{
Connection con("","","",password());
con.select_db("test");
Query q = con.query();
q << "DROP TABLE IF EXISTS t1;";
q.execute();
q << "CREATE TABLE t1 ("
<< "id int(6) unsigned zerofill"
<< " NOT NULL auto_increment,"
<< " l_name VARCHAR(20) NOT NULL,"
<< " f_name VARCHAR(20),"
<< " gpa FLOAT,"
<< " tdate DATE NOT NULL,"
<< " PRIMARY KEY (id) )";
q.execute();
string l_name[] = {"Smith","Wood","Amstrong",
"Urbanskiy","Mazepa"};
string f_name[] = {"Linda","Robert","Lu",
"Anatoliy","Ivan"};
float gpa[] = {3.2,3.5,3.6,3.4,2.5};
for ( int i=0; i < 5; i++ )
{
q << "INSERT INTO t1(l_name,f_name,gpa,tdate) VALUES('"
<< l_name[i] << "','" << f_name[i] << "',"
<< gpa[i] << ",NOW())";
// cout << q.preview() << endl;
q.execute();
}
q.reset();
q << "select * from t1 order by l_name";
Result res = q.store();
Row row;
Result::iterator It = res.begin();
for ( ; It != res.end(); It++ )
{
row = *It;
cout.setf(ios::left);
cout << setw(8) << row[0].c_str()
<< setw(12) << row[1].c_str()
<< setw(12) << row[2].c_str()
<< setw(5) << row[3].c_str()
<< row[4] << endl;
}
string s(15,'-');
cout << s << endl;
cout << res.size() << " rows effected"
<< endl << endl;
//-------------------------------------------------------
q << "select %2, %3 from %4:table where %1:wheref = %0q:what";
// select l_name, f_name from t1 where
q.parse();
q.def["table"] = "t1";
q.def[2] = "l_name";
q.def[3] = "f_name";
cout << "l_name = Amstrong : ";
res = q.store("Amstrong","l_name"); // what,wheref
row = res[0];
cout << row[0] << " " << row[1] << endl;
cout << "f_name = Anatoliy : ";
res = q.store("Anatoliy","f_name");
row = res[0];
cout << row[0] << " " << row[1] << endl;
cout << "id = 1 : ";
res = q.store(1,"id");
row = res[0];
cout << row[0] << " " << row[1] << endl;
cout << "gpa = 2.5 : ";
res = q.store(2.5,"gpa");
row = res[0];
cout << row[0] << " " << row[1] << endl;
con.close();
return 0;
}
OUTPUT:
// 000003 Amstrong Lu 3.62003-01-11
// 000005 Mazepa Ivan 2.5 2003-01-11
// 000001 Smith Linda 3.2 2003-01-11
// 000004 Urbanskiy Anatoliy 3.4 2003-01-11
// 000002 Wood Robert 3.5 2003-01-11
// ---------------
// 5 rows effected
//
// l_name = Amstrong : Amstrong Lu
// f_name = Anatoliy : Urbanskiy Anatoliy
// id = 1 : Smith Linda
// gpa = 2.5 : Mazepa Ivan