数据库编程中经常要构造sql语句,需要连接字符串,比较麻烦,我找到一种比较好的写法,用std::ostringstream。利用流进行这种处理是很方便的
例子:
std::ostringstream sql;
sql << "select * from Roles where RegisterId = " << RegisterId;
mysql_query(m_Conn, sql.str().c_str());
... ...
//再次使用sql,需要将其置空
sql.seekp(0); //将流指针重置
sql.clear(); //重置标志
sql.str(""); //将串内容置为空串
sql << "select * from Players where RoleName = '" << RegisterId << "'";