将std :: wstring转换为const wchar_t x []

时间:2016-04-04 14:32:08

标签: c++

我有以下代码:

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>

1 个答案:

答案 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());