【问题标题】:why does this output hex rather than a sentence? connector/c++为什么这个输出十六进制而不是一个句子?连接器/C++
【发布时间】:2013-09-04 14:54:02
【问题描述】:

我期待看到一个简短的英文句子作为输出,但我看到它的十六进制值 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 > *

【问题讨论】:

    标签: c++ mysql mysql-connector istream connector


    【解决方案1】:

    (这回答了这个问题的第一个版本,它有一个cout &lt;&lt; stream &lt;&lt; endl; 声明。)


    您正在打印istream *

    我想您想从 istream 中读取并打印您阅读的内容,例如与

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

    【讨论】:

    • 我用你的代码替换了我的 cout 行。它在与 getline 的行上因访问冲突而崩溃。我想是因为 s 可能没有初始化。
    • 不,应该没问题。您确定其余代码按预期工作吗?例如。 res-&gt;getBlob(1) 确实返回了一个产生查询结果的流?
    • 您是否应该在while 循环的连续迭代之间关闭流?试试delete stream;
    • getblob 之后流的值似乎没有失败。以下是它在 watch 变量中所说的: + stream 0x00ab3e88 {_Chcount=48198262359153488 } std::basic_istream > *
    • 即使我删除了while循环并且只使用了一次getline,它在调用getline时崩溃
    猜你喜欢
    • 1970-01-01
    • 2022-11-02
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多