且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL类似于C ++中的用法

更新时间:2022-06-27 01:05:05

正则表达式可以用来实现您想要的.但是,有两个问题:

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.