第14章 《数据》Page62~63页,获取结果字段

#include 
#include 

using namespace std;

char to_char(bool b){return b? 'Y' : 'N';}

int main()
{
    mysqlpp::Connection C;

    try
    {
        C.set_option(new mysqlpp::SetCharsetNameOption("gbk"));
        C.connect("d2school", "localhost"
                , "root", "mysql_d2school");

        auto Q = C.query("SELECT * FROM user");
        auto R = Q.store();

        cout << "表: " << R.table() << "\n";
        cout << "共有: " << R.num_fields() << "个字段\n";

        /* 临时关闭异常,测试指定名字字段的位置
           注意,ne的入参是R(结果对象),而不是C(连接对象)*/
        mysqlpp::NoExceptions ne(R);
        cout << "字段ID次序: " << R.field_num("ID") << '\t';
        cout << "字段name次序: " << R.field_num("name") << '\t';
        cout << "字段ABC次序: " << R.field_num("ABC") << endl;

        for(size_t i = 0; i < R.num_fields(); ++ i)
        {
            mysqlpp::Field const& F = R.field(i); //返回field对象

            cout << '(' << i + 1 << ')' << F.name() << "\n";
            cout << F.type().sql_name() << "\n";
            cout << "LENGTH: " << F.length() << "\n";

            cout << "PK: " << to_char(F.primary_key()) << "\t";
            cout << "AI: " << to_char(F.auto_increment()) << "\t";
            cout << "TIMESTAMP: " << to_char(F.timestamp()) << "\t";
            cout << "UK: " << to_char(F.unique_key()) << "\t";
            cout << "BLOB: " << to_char(F.blob_type()) << "\n";
            cout << string(48, '-') << endl;
        }
    }
    catch(mysqlpp::Exception const& e)
    {
        cerr << e.what() << endl;
    }

    return 0;
}

第14章 《数据》Page62~63页,获取结果字段_第1张图片

你可能感兴趣的:(《白话C++》学习,数据库,ubuntu,c++)