Question

I was expecting to see a short english sentence as output but i see hex value of it instread. I cannot find any information on this getblob function but i hear its what should be used for varchar columns. Before i used getString and it crashes the app, its funny though becuase it crashes after it successfully prints the sentence sometimes. However i cant have it crashing so i need to make it work with getblob, it returns an std::istream which i no nothing about. I experimented with converting istream to string but my lack of understanding of what an isteam is did not get me far.

#include <iostream>
#include <sstream>
#include <memory>
#include <string>
#include <stdexcept>

#include "cppconn\driver.h"
#include "cppconn\connection.h"
#include "cppconn\statement.h"
#include "cppconn\prepared_statement.h"
#include "cppconn\resultset.h"
#include "cppconn\metadata.h"
#include "cppconn\resultset_metadata.h"
#include "cppconn\exception.h"
#include "cppconn\warning.h"
#include "cppconn\datatype.h"   
#include "cppconn\parameter_metadata.h"
#include "mysql_connection.h"
#include "mysql_driver.h"

using namespace std;


int main()
{
    sql::mysql::MySQL_Driver *driver;
    sql::Connection *con;
    sql::Statement *stmt;

    // ...
    driver = sql::mysql::get_mysql_driver_instance();
    cout<<"Connecting to database...\n";
con = driver->connect("tcp://xx.xxx.xxx.xxx:3306", "xxxxx", "xxxxxx");


sql::ResultSet *res;
// ...
stmt = con->createStatement();
// ...
 con->setSchema("mrhowtos_main");
res = stmt->executeQuery("SELECT english FROM `eng` WHERE `ID` = 16;");
while (res->next()) {
      istream *stream = res->getBlob(1);

      string s; 
    getline(*stream, s); //crashes here - access violation
    cout << s << endl; 
}
cout<<"done\n";
delete res;
delete stmt;
delete con;
system("PAUSE");
    return 0;
}

UPDATE: crashes on getline

value of stream after getblob:

  • stream 0x005f3e88 {_Chcount=26806164129143632 } std::basic_istream > *
Was it helpful?

Solution

(This answered the first version of this question, which had a cout << stream << endl; statement.)


You're printing an istream *.

I suppose you would like to read from that istream and print what you read, e.g. with

string s; 
while (getline(*stream, s))
  cout << s << endl; 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top