我有以下代码:
10.10.10.x
您可以看到代码
void get_id(int i, std::vector<item>& _items) {
auto cpool = get_db_connection_pool();
auto con = cpool->get_connection();
db::result m;
int _id;
if (i == 1) {
const wchar_t sql[] = LR"***(
SELECT * FROM TABLE1
)***";
db::statement st(con, sql);
m = st.execute().into(_id);
while (m.move_next()) {
_items.push_back(item {_id});
}
}
else {
const wchar_t sql[] = LR"***(
SELECT * FROM TABLE2
)***";
db::statement st(con, sql);
m = st.execute().into(_id);
while (m.move_next()) {
_items.push_back(item {_id});
}
}
}
在if-else语句中重复写入。我想将那部分移到if else案例之外:
db::statement st(con, sql);
m = st.execute().into(_id);
while (m.move_next()) {
_items.push_back(item {_id});
}
我尝试使用临时void get_id(int i, std::vector<item>& _items) {
auto cpool = get_db_connection_pool();
auto con = cpool->get_connection();
db::result m;
int _id;
if (i == 1) {
const wchar_t sql[] = LR"***(
SELECT * FROM TABLE1
)***";
}
else {
const wchar_t sql[] = LR"***(
SELECT * FROM TABLE2
)***";
}
db::statement st(con, sql);
m = st.execute().into(_id);
while (m.move_next()) {
_items.push_back(item {_id});
}
}
替换if-else案例中的sql[]
,但我无法弄清楚如何将std::wstring
转换为std::wstring
。< / p>
答案 0 :(得分:3)
std::wstring
有一个成员函数c_str
,它会将const wchar_t*
返回给基础字符串。只要db::statement
获得const wchar_t []
或const wchar_t *
,您就可以使用
std::wstring sql;
if (i == 1) {
sql = LR"***(
SELECT * FROM TABLE1
)***";
}
else {
sql = LR"***(
SELECT * FROM TABLE2
)***";
}
db::statement st(con, sql.c_str());