【发布时间】:2013-06-25 21:17:52
【问题描述】:
我在使用 mysql c++ 驱动程序转义字符串时遇到问题。我通过阅读mysql论坛找到了一个小例子,以下似乎是正确的方法。但是,动态转换似乎不起作用。有没有人有任何见识?谢谢!
附: “conn”是我的 Connection 对象。保证在此时分配,所以不是问题。
编辑:添加类构造函数以完成代码示例。
DbConnector::DbConnector(const ns4__ServerHostResponse &response)
{
try
{
driver = get_driver_instance();
conn.reset(
driver->connect(boost::str(boost::format("tcp://%1%:3306") % response.DatabaseHostName), response.Username, response.Password));
conn->setSchema(response.DbSchema);
query << "";
}
catch(std::exception &ex)
{
throw CustomException(boost::str(boost::format("Unable to connect to database: %1%") % response.DbSchema), ex.what());
}
catch(...)
{
throw StokedTcpException(boost::str(boost::format("Unable to connect to database: %1%") % response.DbSchema));
}
}
void DbConnector::EscapeString(std::string &s) {
if (conn)
{
std::shared_ptr<sql::mysql::MySQL_Connection> mysqlConn(dynamic_cast<sql::mysql::MySQL_Connection*>(conn.get()));
if (mysqlConn)
s = mysqlConn->escapeString(s);
else
throw CustomException("Cannot allocate connection object to escape mysql string!");
}
}
【问题讨论】:
-
仍然没有看到“conn”的声明...?
-
这是一个 shared_ptr。查看 conn.reset() 行。
-
在我的标题中,它是 std::shared_ptr<:connection> conn;
-
sql::mysql::MySQL_Connection 是合法的向下转换,还是拼写错误?我很难找到 mysql 文档。有必要吗?你能用你有的 sql::Connection 调用 escapeString 吗?
-
我相信这是一个合法的沮丧。 sql::Connection 类中没有 escapeString 函数。这是我尝试的第一件事......