在sqlite3中使用正则表达式查询name,要求返回name中以M开头紧接着是数字的记录
时间: 2024-03-21 18:40:16 浏览: 22
可以使用SQLite3中的REGEXP函数和通配符实现这个查询,具体的SQL语句如下:
```
SELECT name FROM table_name WHERE name REGEXP '^M[0-9]';
```
其中,'^M'表示name以M开头,'[0-9]'表示紧接着M后面是一个数字。注意,这里使用的是正则表达式语法,而不是通常的SQL语法。
如果你需要在Python中使用SQLite3进行这个查询,可以使用以下代码:
```python
import sqlite3
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
cursor.execute("SELECT name FROM table_name WHERE name REGEXP '^M[0-9]'")
results = cursor.fetchall()
for row in results:
print(row[0])
conn.close()
```
其中,'database.db'是你的SQLite3数据库文件名,'table_name'是你需要查询的表名。查询结果会以元组的形式返回,其中每个元组只包含一个字符串,即符合条件的name。
相关问题
在sqlite3中有一字段mahine_number,使用sql语句如何将此字段中的中文字符删除
如果字段 `mahine_number` 中的中文字符需要删除,可以使用 SQLite3 中的 `replace` 函数将中文字符替换为空字符串。具体的 SQL 语句如下:
```
UPDATE table_name SET mahine_number = replace(mahine_number, '中文字符', '');
```
其中,`table_name` 是表名,`mahine_number` 是需要处理的字段名,`中文字符` 是需要被删除的中文字符。如果有多个中文字符需要删除,可以将多个中文字符用 `|` 连接起来,形成一个正则表达式,如 `replace(mahine_number, '中文字符1|中文字符2', '')`。
在sqlite3中有一字段mahine_number,使用sql语句如何将此字段中的所有中文字符和符号删除
如果字段 `mahine_number` 中的所有中文字符和符号都需要删除,可以使用 SQLite3 中的 `regexp_replace` 函数将所有中文字符和符号替换为空字符串。具体的 SQL 语句如下:
```
UPDATE table_name SET mahine_number = regexp_replace(mahine_number, '[\\u4e00-\\u9fa5[:punct:]]', '');
```
其中,`table_name` 是表名,`mahine_number` 是需要处理的字段名。`[\\u4e00-\\u9fa5[:punct:]]` 是一个正则表达式,表示匹配所有中文字符和符号。使用 `regexp_replace` 函数将匹配到的字符替换为空字符串即可。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
url="http://www.cctv.com.cn" html=requests. get(url) html. encoding=html. apparent_encoding data=html. text reg=r'(.*?)' urls=re.findall(reg, data) print(urls) conn=sqlite3.connect("school.db") SQL='''create table information(name char(30) not null. link char(20), primary key("name"))''' conn.execute(SQL) conn.commit() for item in urls: SOL='''insert into information(name,link) values('%s', '%s')'''%(item[1],item[0]) conn.execute(SQL) conn.commit() conn.close()
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)