1

Suppose that I have the following table and data inside it:

CREATE TABLE urls (url TEXT);
INSERT INTO urls VALUES('https://www.google.ru/?gws_rd=ssl#newwindow=1&q=&safe=off');
INSERT INTO urls VALUES('https://www.google.ru/?gws_rd=ssl#newwindow=1&safe=off&q=1');
INSERT INTO urls VALUES('https://www.google.ru/?gws_rd=ssl#newwindow=1&q=2&safe=off');
INSERT INTO urls VALUES('https://www.google.ru/?gws_rd=ssl#newwindow=1&q=23&safe=off');

I need to select all records where URL contains non-empty query parameter (in this case it's all but the first record).

I'm using SQLite, so I decided to use REGEXP operator like this:

string sql = @"SELECT * FROM urls WHERE url REGEXP '\/(?:www\.)?google.+?q=([^&]+)(?:&|$)'";
var command = new SQLiteCommand(sql, dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
    Console.WriteLine("Url: " + reader["url"]);
}

but it gives me the following error:

An unhandled exception of type 'System.Data.SQLite.SQLiteException' occurred in System.Data.SQLite.dll

Additional information: SQL logic error or missing database

no such function: REGEXP

Is there any way to fix it? If no, what can I do instead?

Thanks in advance.

0

2 Answers 2

3

SQLite doesn't have a built-in regexp() function, as explained in the documentation:

The REGEXP operator is a special syntax for the regexp() user function. No regexp() user function is defined by default and so use of the REGEXP operator will normally result in an error message. If an application-defined SQL function named "regexp" is added at run-time, then the "X REGEXP Y" operator will be implemented as a call to "regexp(Y,X)".

I admit this it is unusual for a database to support syntax but not the underlying function. However, it should be easy enough to supply your own function.

Sign up to request clarification or add additional context in comments.

1 Comment

How can I supply my own regexp function? Is there any implementation?
0

The regexp comparison operator is not part of the SQL standard. It is an extension specific to mysql.

Probably you can just use like. This should do the same :

url LIKE '%google.%q=%' 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.