Enable SQLite Regular Expressions
masterSQLite does not provide a default implementation for the regexp function used in WHERE clauses. You can enable it in your database connection by calling SQLite.@register db SQLite.regexp.
Once registered, you can use the REGEXP operator in SQL syntax or call the regexp() function explicitly.
Note on Backslashes: When using regex patterns with backslashes (e.g., \d), Julia's string parsing may strip them. To ensure the backslashes reach the SQLite engine, either manually escape them (e.g., '\\d') or use Julia's raw string literal raw"...".
using SQLite
import DBInterface
db = SQLite.DB("Chinook_Sqlite.sqlite")
# Register the regexp function
SQLite.@register db SQLite.regexp
# Option 1: Using the REGEXP operator
DBInterface.execute(db, "SELECT FirstName, LastName FROM Employee WHERE LastName REGEXP 'e(?=a)'")
# Option 2: Using the regexp() function explicitly
DBInterface.execute(db, "SELECT * FROM Genre WHERE regexp('e[trs]', Name)")
# Handling backslashes correctly using raw strings
DBInterface.execute(db, raw"SELECT * FROM MediaType WHERE Name REGEXP '-\d'")