MySQL- Руководство разработчика

Получение информации о полях


Следующий пример показывает, как получить некоторую базисную информацию относительно полей, включая имя поля и тип SQL. Файл назван fieldinfo1.cc. #include <iostream> #include <iomanip> #include <sqlplus.hh>

int main() { try { // its in one big try block Connection con(use_exceptions); con.connect("mysql_cpp_data"); Query query = con.query(); query << "select * from stock"; Result res = query.store(); cout << "Query: " <<query.preview() << endl; cout << "Records Found: "<< res.size() << endl <<endl; cout << "Query Info:\n"; cout.setf(ios::left); for (unsigned int i = 0; i < res.size(); i++) { cout << setw(2) << i << setw(15) << res.names(i).c_str() << setw(15) << res.types(i).sql_name() << setw(20) << res.types(i).name() << endl; } cout << endl; if (res.types(0) == typeid(string)) cout << "Field 'item' is of an sql type which most closely resembles a\n" << "the c++ string type\n"; if (res.types(1) == typeid(short int)) cout << "Field 'num' is of an sql type which most closely resembles a\n" << "the c++ short int type\n"; else if (res.types(1).base_type() == typeid(short int)) cout << "Field 'num' base type is of an sql type which most closely \n" << "resembles a the c++ short int type\n"; return 0; } catch (BadQuery er) { cerr << "Error: " << er.error << endl; return -1; } catch (BadConversion er) { cerr << "Error: Tried to convert \"" << er.data << "\" to a \"" << er.type_name << "\"." << endl; return -1; } }



Содержание раздела