c++ - 为什么这个输出十六进制而不是一个句子?连接器/C++

标签 c++ mysql mysql-connector istream connector

我期待看到一个简短的英文句子作为输出,但我看到它的十六进制值 instread。我找不到关于此 getblob 函数的任何信息,但我听说它应该用于 varchar 列。在我使用 getString 并使应用程序崩溃之前,这很有趣,因为它有时会在成功打印句子后崩溃。但是我不能让它崩溃,所以我需要让它与 getblob 一起工作,它返回一个 std::istream,我对此一无所知。我尝试将 istream 转换为字符串,但我对 isteam 是什么缺乏了解并没有让我走得更远。

#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;
}

更新:在 getline 上崩溃

getblob 后流的值:

  • 流 0x005f3e88 {_Chcount=26806164129143632 } std::basic_istream > *

最佳答案

(这回答了这个问题的第一个版本,其中有一个 cout << stream << endl; 语句。)


您正在打印 istream * .

我想你想阅读那个istream并打印您阅读的内容,例如与

string s; 
while (getline(*stream, s))
  cout << s << endl; 

关于c++ - 为什么这个输出十六进制而不是一个句子?连接器/C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18617150/

相关文章:

mysql - 在 SQL 中计算两个或多个时间戳之间的分钟数

c++ - 缓冲的 MySql 连接器 C++ 结果集

java - 领域对象和这些对象的容器的类设计

mysql - 根据另一个表自动将数据插入到表中

c++ - ADL 应该如何发挥作用?

mysql - 使用 JOIN 的 SQL 删除语句

mysql jdbc 连接失败 isValid

c++ - 连接MySQL时出现段错误

c++ - 使用 OpenMP 循环时的线程安全

c++ - XAudio2 - 仅播放音频的一部分