MySQL Forums
Forum List  »  Connector/C++

Re: Connector c++ 8 converting field Value for DATE to time_t
Posted by: Claude Mally
Date: August 23, 2021 12:48PM

#include <iostream>
#include <mysqlx/xdevapi.h>

using ::std::cout;
using ::std::endl;

int main() {
try {
using namespace ::mysqlx;

std::string session_uri { "**** my credentials ***" };

Session sess( session_uri );

std::string schema_name = sess.getDefaultSchemaName();
cout << "schema_name : " << schema_name << endl;

Schema db_schema = sess.getSchema( schema_name, true );

std::string table_name { "pet" };

Table pet_table = db_schema.getTable( table_name, true );
cout << "table " << table_name << " has " << pet_table.count() << " row(s). is_view=" << std::boolalpha << pet_table.isView() << std::endl;
// note MySql C++ connector fails to report those fieldnames, even when requested in the query
const std::string alternate_birthname { "strbirth" };
const std::string alternate_deathname { "strdeath" };

std::stringstream query_writer;
query_writer << "SELECT name, owner, species, sex, CAST(birth as char) as " << alternate_birthname << ", CAST(death as char) as " << alternate_deathname << " FROM " << table_name;

std::string query_str = query_writer.str();
cout << "query:" << query_str << endl;

SqlStatement query = sess.sql(query_str);

SqlResult res = query.execute();

Row pet;
while( (pet = res.fetchOne()) ) {
if( pet.isNull() ) {
cout << "null result" << endl;
} else {

int count = res.getColumnCount();
for( int column = 0; column < count; column++ ) {

const Column& current_column = res.getColumn( column );

const Value& field_value = pet[column];

const std::string& field_name = current_column.getColumnName();
Type field_type= current_column.getType();

std::string raw_string;

if( field_value.isNull() ) {
raw_string = "NULL";
} else {
std::stringstream byte_writer;
const bytes& raw_bytes = field_value.getRawBytes();
for( const auto one_byte : raw_bytes ) {
char temp[3];
snprintf( temp, sizeof(temp),"%02X", one_byte);
byte_writer << temp;
}
raw_string = byte_writer.str();
}

cout << "name: " << quoted(field_name) << " type " << field_type << " : " << field_value;

if( raw_string.length() ) {
cout << " raw=" << raw_string;
}

cout << endl;
}
}
}

} catch (const mysqlx::Error &err) {
cout << "ERROR: " << err << endl;
}
}




output:

schema_name : cma_test
table pet has 1 row(s). is_view=false
query:SELECT name, owner, species, sex, CAST(birth as char) as strbirth, CAST(death as char) as strdeath FROM pet
name: "name" type STRING : Puffball raw=5000750066006600620061006C006C00
name: "owner" type STRING : Diane raw=4400690061006E006500
name: "species" type STRING : hamster raw=680061006D007300740065007200
name: "sex" type STRING : f raw=6600
name: "" type STRING : 1999-03-30 00:00:00 raw=31003900390039002D00300033002D00330030002000300030003A00300030003A0030003000
name: "" type STRING : <null> raw=NULL

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: Connector c++ 8 converting field Value for DATE to time_t
533
August 23, 2021 12:48PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.