SQL类似于C ++中的用法 [英] Sql Like similar use in c++
问题描述
我想问一个简单的问题,有什么方法可以在c ++中使用喜欢"这样的东西
I would like to ask a simple question, is there any way to use "like" such thing in c++
喜欢'%s,我想要用c ++这样的东西,因为如果我希望用户在程序中找到某些东西,那么它将简单地匹配所有相关数据.
LIKE '%s I want some thing like this in c++ because if I want user to find something in the program, this simply matches all the related data.
推荐答案
正则表达式可以用来实现您想要的.但是,有两个问题:
Regular expressions can be used to achieve what you want. There are two problems, though:
1.)与简单的SQL Like
关键字相比,它们使用起来确实复杂.
1.) They are really complicated to use compared to the simple SQL LIKE
keyword.
2.)它们只是 C ++ 11 中的标准功能.如果使用旧" C ++编写,则必须使用 Boost.Regex 库.
2.) They are only a standard feature in C++11. If you write in "old" C++, then you'd have to use the Boost.Regex library.
但是...
Looking at the very basic LIKE
examples at w3schools.com, you could in fact implement many of them in simple C++, using normal std::string functions.
例如,让我们看看这个:
For instance, let's have a look a this:
SELECT * FROM Customers WHERE City LIKE '%s';
这只是意味着对于每个字符串,您都要查看最后一个字符并检查它是否为's':
This just means that for every string, you look at the last character and check if it's 's':
std::string s;
// ...
if (!s.empty() && (s[s.size() - 1] == 's') {
// ...
}
或者这个:
SELECT * FROM Customers WHERE Country NOT LIKE '%land%';
在C ++中:
std::string s;
// ...
if (s.find("land") == std::string::npos) {
// ...
}
因此,最后,这实际上取决于您对 Like
到底想做什么.
So, in the end, it really depends on what exactly you'd want to do with LIKE
.
这篇关于SQL类似于C ++中的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!