Python SQLite SELECT LIKE IN [列表] [英] Python SQLite SELECT LIKE IN [list]

查看:27
本文介绍了Python SQLite SELECT LIKE IN [列表]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Python 中编写 SQL 查询来选择 LIKE IN Python 列表中的元素?

How to write SQL query in Python to select elements LIKE IN Python list?

例如,我有 Python 字符串列表

For example, I have Python list of strings

Names=['name_1','name_2',..., 'name_n']

和 SQLite_table.

and SQLite_table.

我的任务是找到最短的路径

My task is to find shortest way to

SELECT elements FROM SQLite_table WHERE element_name LIKE '%name_1%'
SELECT elements FROM SQLite_table WHERE element_name LIKE '%name_2%'
...
SELECT elements FROM SQLite_table WHERE element_name LIKE '%name_n%'

推荐答案

您需要一个 name_1|name_2|name_3|... 形式的正则表达式,您可以使用 ' 生成它|'.join(Names):

You would need a regular expression of the form name_1|name_2|name_3|…, which you can generate using '|'.join(Names):

SELECT elements FROM SQLite_table WHERE element_name REGEXP 'name_1|name_2|name_3|…|name_n'

查看如何在 SQLite 查询中使用正则表达式?了解如何在 SQLite 中使用正则表达式.

Check How do I use regex in a SQLite query? for instructions on how to use regular expressions in SQLite.

这篇关于Python SQLite SELECT LIKE IN [列表]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆