Skip to content

Instantly share code, notes, and snippets.

@rdapaz
Created May 14, 2024 07:39
Show Gist options
  • Save rdapaz/9b593cb93e9a6fdebf201760e3fa0765 to your computer and use it in GitHub Desktop.
Save rdapaz/9b593cb93e9a6fdebf201760e3fa0765 to your computer and use it in GitHub Desktop.
Use REGEXP with sqlite
# This makes use of REGEXP within an sqlite3 query
```python
def regexp(expr, item):
reg = re.compile(expr)
return reg.search(item) is not None
def make_data():
import sqlite3
sql = """
SELECT
ot_risks.psm_shortname,
ot_risks.sys_desc,
ot_risks.current_risk,
coalesce(ot_risks.dominant_conduit, 'Remote / OT DMZ'),
RiskRatings.RISK_WEIGHT
FROM
RiskRatings
INNER JOIN
ot_risks
ON
RiskRatings.RISK_RATING = ot_risks.current_risk
WHERE
(
( ot_risks.current_risk REGEXP '(B|C)'
AND coalesce(ot_risks.dominant_conduit, 'Internet') NOT LIKE '%Local%'
AND RiskRatings.RISK_WEIGHT > 21
)
OR ot_risks.psm_shortname REGEXP '(DCS|PCS|SIS|IPS|CCC|Power|PMS|PWR|POWER|ENMC|ICSS)'
)
AND facility IN ('KGP')
ORDER BY
2 ASC,
1 DESC
"""
DB_PATH = r'//path_to_file'
conn = sqlite3.connect(DB_PATH)
conn.create_function('REGEXP', 2, regexp)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment