当我想使用准备好的语句插入字符串时,我在mysql c++连接器上遇到问题,他在数据库中减少了我的字符串(以长文本保存)。因为要保存长文本,所以数据丢失很多。

这是我的代码:

void RequetteBDD::add(Files::Fichier file)
{

    string query = "INSERT INTO files(titre,url,type,txt,lastcrawl) VALUES (?,?,?,?,?)";

    sql::PreparedStatement  *prep_stmt;

    prep_stmt = con->prepareStatement(query);

    prep_stmt->setString(1,file.getNom()); //title
    prep_stmt->setString(2,file.getURL().getUri()); //url
    prep_stmt->setInt(3,file.getTypeInt()); //type

    //i also try :
    istringstream stream(file.getTextFull());
    prep_stmt->setBlob(4,&stream);
    //but the saved length was exactly the same.

    prep_stmt->setString(4,file.getTextFull()); //here is the probleme

    prep_stmt->setInt(5,time(NULL)); //timstamp

    prep_stmt->execute();
    delete prep_stmt;

}

mysql ddb:
CREATE TABLE IF NOT EXISTS `files` (
  `id` int(11) NOT NULL AUTO_INCREMENT ,
  `titre` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
  `url` varchar(512) COLLATE utf8_unicode_ci NOT NULL,
  `type` int(1) NOT NULL DEFAULT '0' COMMENT ,
  `txt` longtext COLLATE utf8_unicode_ci NOT NULL,
  `lastcrawl` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=7 ;

谢谢你的帮助。

最佳答案

这是一个编码问题;它们是解决问题的两种解决方案:

  • 将编码从数据库更改为ASCII
  • 或通过boost::locale::conv轻松更改字符串的编码

  • 我希望它会帮助其他人。

    关于c++ - 当我插入数据库时​​,mysql c++连接器会减少我的字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21957999/

    10-10 08:57
    查看更多