c++ prepareStatement使用like查询语句的方式

代码

sql::PreparedStatement  *read_stmt;
read_stmt = pconn->prepareStatement("SELECT * from `user` WHERE level LIKE ? ");
//1
sql::SQLString searchPattern(("%" + key + "%").c_str());
read_stmt->setString(1, searchPattern);
//2
//std::string kk = "%"+key+"%'";
//read_stmt->setString(1, kk);

auto_ptr<ResultSet> pres(read_stmt->executeQuery());
int n = pres->rowsCount();
LOG_INFO << "rows: " << n;
if(n > 0){
  while (pres->next())
  {
    //取出数据
  }
}
delete read_stmt;

注意,在某些情况下,如果查询始终未生效,请检查查询参数key的右侧是否包含多个'\0'或者其它不可见字符;

posted @ 2024-07-24 09:17  bitwoods  阅读(13)  评论(0编辑  收藏  举报