如何将输入的变量添加到查询字符串中?

亚细亚 2009-01-16 11:48:53
string sql;
string name;
cin>>name;
sql="select * from user ";
想让变量name作为sql的一个where查询条件,请问如何处理?

...全文
91 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
亚细亚 2009-01-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 Chiyer 的回复:]
C/C++ code#include"iostream"#include"string"usingnamespacestd;intmain()
{stringsql;stringname;
cin>>name;
sql=string("select * from user = \"")+name+string("\"");

cout<<sql<<endl;return0;
}
[/Quote]
ok,结贴!
waizqfor 2009-01-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 Chiyer 的回复:]
C/C++ code#include "iostream"
#include "string"
using namespace std;

int main()
{
string sql;
string name;
cin>>name;
sql = string("select * from user = \"") + name + string("\"");

cout<<sql<<endl;
return 0;
}
[/Quote]
UP
xiaoyisnail 2009-01-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yaxiya 的回复:]
sprintf(sql, "select * from user where loadName='%s'", name.c_str());
这样好像有错误.
[/Quote]

没有啊


#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

int main()
{
char sql[128] = {0};
string name;
cout<<"please enter the name."<<endl;
cin>>name;
sprintf(sql, "select * from user where loadName='%s'", name.c_str());

cout<<sql<<endl;
return 0;
}


运行结果:

please enter the name.
abc
select * from user where loadName='abc'
Press any key to continue
亚细亚 2009-01-16
  • 打赏
  • 举报
回复
sprintf(sql, "select * from user where loadName='%s'", name.c_str());
这样好像有错误.
xiaoyisnail 2009-01-16
  • 打赏
  • 举报
回复

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

int main()
{
char sql[128] = {0};
string name;
cin>>name;
sprintf(sql, "select * from user %s", name.c_str());

cout<<sql<<endl;
return 0;
}
星羽 2009-01-16
  • 打赏
  • 举报
回复
#include "iostream"
#include "string"
using namespace std;

int main()
{
string sql;
string name;
cin>>name;
sql = string("select * from user = \"") + name + string("\"");

cout<<sql<<endl;
return 0;
}
yellowhwb 2009-01-16
  • 打赏
  • 举报
回复
string好像是可以用+的吧!!
sql += "where name='"+ name+"'"

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧