Привет Всем
Ниже есть работающая прога :)
Но дело в том, что Она на 1-н запрос...
Вопрос - как в mysql++ сделать запрос, что бы например как в php:
result_of_query["столбец1"]
result_of_query["столбец2"]
result_of_query["столбец3"]И что бы можно было обращаться по именам столбцов...
query << "SELECT `ip` AS ip, `group` AS g,`num` AS n,`bw_in` AS in_,`bw_in_name` AS inn_,`bw_out` AS out_,`bw_out_name` AS outn,`n_pipe_in` AS p_in,`n_pipe_out` AS p_out,`hour` AS h_,`activ` AS e,`id` FROM "<<" "<< TABLE_DEFAULT <<" "<<" WHERE ip='"<< ip<<"'";
То что оработает:#include "util.h"
#include <mysql++.h>
#include <iostream>
#include <iomanip>
using namespace std;
#define MY_DATABASE "bezlimit2"
#define TABLE_DEFAULT "def"
#define MY_HOST "localhost"
#define MY_USER "root"
#define MY_PASSWORD ""
int
main(int argc, char *argv[])
{
string ip;
ip=argv[1];
mysqlpp::Connection con(false);
con.connect(MY_DATABASE, MY_HOST, MY_USER, MY_PASSWORD);
mysqlpp::Query query = con.query();
//ХОЧУ такой запрос!!!
//query << "SELECT `ip` AS ip, `group` AS g,`num` AS n,`bw_in` AS in_,`bw_in_name` AS inn_,`bw_out` AS out_,`bw_out_name` AS outn,`n_pipe_in` AS p_in,`n_pipe_out` AS p_out,`hour` AS h_,`activ` AS e,`id` FROM "<<" "<< TABLE_DEFAULT <<" "<<" WHERE ip='"<< ip<<"'";
//А делаю только такой
query << "SELECT `num` AS n FROM "<<" "<< TABLE_DEFAULT <<" "<<" WHERE ip='"<< ip<<"'";
mysqlpp::Result res = query.store();// Display the result set
cout << "We have:" << endl;
if (res) {
char buf[100];
mysqlpp::Row row;
mysqlpp::Row::size_type i;
for (i = 0; row = res.at(i); ++i) {
cout << '\t' << utf8trans(row.at(0), buf, sizeof(buf)) << endl;
}
}
else {
cerr << "Failed to get item list: " << query.error() << endl;
return 1;
}return 0;
}
mysql++ 2.0.7
Нашел!!!
Вот схема:
DROP TABLE IF EXISTS `def`;
CREATE TABLE IF NOT EXISTS `def` (
`ip` varchar(48) NOT NULL default '192.168.0.1',
`group` int(12) NOT NULL default '0',
`num` int(10) NOT NULL default '500',
`bw_in` int(30) NOT NULL default '0',
`bw_in_name` varchar(10) NOT NULL default 'Kbit/s',
`bw_out` int(30) NOT NULL default '0',
`bw_out_name` varchar(12) NOT NULL default 'Kbit/s',
`n_pipe_in` int(8) NOT NULL default '0',
`n_pipe_out` int(8) NOT NULL default '0',
`hour` int(2) NOT NULL default '0',
`activ` char(1) NOT NULL default 'y',
`id` int(32) NOT NULL auto_increment,
PRIMARY KEY (`id`),
UNIQUE KEY `ip` (`ip`,`hour`,`activ`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
mysql> SELECT `num` AS n,`ip` AS ip, `group` AS g,`bw_in` AS in_ FROM def WHERE ip='192.168.0.3';
+-----+-------------+---+-----+
| n | ip | g | in_ |
+-----+-------------+---+-----+
| 500 | 192.168.0.3 | 0 | 0 |
+-----+-------------+---+-----+
1 row in set (0.00 sec)
Дальше код#include "util.h"
#include <mysql++.h>
#include <iostream>
#include <iomanip>
using namespace std;
#define MY_DATABASE "bezlimit2"
#define TABLE_DEFAULT "def"
#define MY_HOST "localhost"
#define MY_USER "root"
#define MY_PASSWORD "passwd"
int
main(int argc, char *argv[])
{
string ip;
ip=argv[1];mysqlpp::Connection con(false);
con.connect(MY_DATABASE, MY_HOST, MY_USER, MY_PASSWORD);
mysqlpp::Query query = con.query();
query << "SELECT `num`,`ip`,`group`,`bw_in` FROM "<<" "<< TABLE_DEFAULT <<" "<<" WHERE ip='192.168.0.3'";
mysqlpp::ResUse res = query.use();
if (res) {
cout.setf(ios::left);
cout << setw(10) << "num" <<
setw(48) << "ip" <<
setw(12) << "group" <<
setw(30) << "bw_in" << endl << endl;
mysqlpp::Row row;
while (row = res.fetch_row()) {
cout <<setw(10) << row["num"] <<
setw(48)<<row["ip"] <<
setw(12) <<row["group"] <<
setw(30) << row["bw_in"] << endl << endl;}
return 0;
}
else {
cerr << "Failed to get " << query.error() << endl;
return 1;
}return 0;
}
./simple2
num ip group bw_in
500 192.168.0.3 0 0Если кому интересно как компилировать примеры mysql++ 2.0.XX то вот что Я использую:
c=simple3
имя файла в examples, например simple3.cpp, но обязательно без .cpp
это где есть:
#include "util.h"#!/bin/sh
c=simple3
c++ -c -o util_util.o -I../lib -I/usr/include/mysql -I/usr/include/mysql++ -O2 -fno-strict-aliasing -pipe ./util.cpp
ar rcu libmysqlpp_util.a util_util.o
ranlib libmysqlpp_util.a
c++ -c -o $c.o -I../lib -I/usr/include/mysql -I/usr/include/mysql++ -O2 -fno-strict-aliasing -pipe ./$c.cpp
c++ -o $c $c.o -L/usr/lib/mysql -L. -L../lib -lmysqlpp_util -lmysqlclient -lmysqlpp -lzЕсли без:
#include "util.h"
где build=my
my - это файл my.cpp#!/bin/sh
build=my
c++ -c -o $build.o -I../lib -I/usr/include/mysql++ -I/usr/include/mysql -O2 -fno-strict-aliasing -pipe ./$build.cppc++ -o $build $build.o -L/usr/lib/mysql -L../lib -lmysqlclient -lmysqlpp -lz