50 lines
1.2 KiB
Python
50 lines
1.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
清理 search_name 字段:移除特殊符号、英文单词、数字、表情符号
|
|
"""
|
|
import sqlite3
|
|
import re
|
|
|
|
DB_PATH = r"D:\夏骥\微信研究\contacts.db"
|
|
|
|
|
|
def clean_search_name(name):
|
|
"""清理搜索姓名:只保留中文"""
|
|
if not name:
|
|
return ''
|
|
|
|
# 只保留中文字符
|
|
cleaned = re.sub(r'[^\u4e00-\u9fa5]', '', name)
|
|
|
|
return cleaned.strip()
|
|
|
|
|
|
def main():
|
|
conn = sqlite3.connect(DB_PATH)
|
|
cursor = conn.cursor()
|
|
|
|
# 更新所有记录
|
|
cursor.execute('SELECT id, search_name FROM contacts')
|
|
rows = cursor.fetchall()
|
|
updated = 0
|
|
for row in rows:
|
|
id_, search_name = row
|
|
cleaned = clean_search_name(search_name)
|
|
cursor.execute('UPDATE contacts SET search_name = ? WHERE id = ?', (cleaned, id_))
|
|
updated += 1
|
|
|
|
conn.commit()
|
|
print(f'已更新 {updated} 条记录')
|
|
|
|
# 显示示例
|
|
cursor.execute('SELECT id, name, search_name FROM contacts LIMIT 20')
|
|
print('\n示例数据:')
|
|
for row in cursor.fetchall():
|
|
print(f' {row[0]}: name="{row[1]}" -> search_name="{row[2]}"')
|
|
|
|
conn.close()
|
|
print('\n完成!')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main() |