Skip to content

Commit 8b2a56d

Browse files
author
caofanCPU
committed
fix bug of SQL for string key symbol "'" -> "\'"
1 parent aebd7b9 commit 8b2a56d

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

ju_dan/main.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ def handle_content(msg):
1818
content = msg['Text']
1919
else:
2020
content = '非文本内容'
21+
# 内容最多1024个字符
22+
content = custom_str(content, 1024)
2123
time_stamp = msg['CreateTime']
2224
create_time = time.strftime("%Y--%m--%d %H:%M:%S", time.localtime(time_stamp))
2325
current_talk_group_id = msg['User']['UserName']
24-
current_talk_group_name = msg['User']['NickName']
25-
from_user_name = msg['ActualNickName']
26+
# 内容最多64个字符
27+
current_talk_group_name = custom_str(msg['User']['NickName'], 64)
2628
from_user_id = msg['ActualUserName']
29+
# 发送者昵称最多1024个字符
30+
from_user_name = custom_str(msg['ActualNickName'], 64)
2731
sql = "INSERT INTO wx_group_chat(msg_type, content, sender_id, sender_name,\
2832
group_id, group_name, time_stamp, create_time) \
2933
VALUE ('%d','%s','%s','%s','%s','%s','%d','%s');"\
@@ -35,6 +39,20 @@ def handle_content(msg):
3539
return
3640

3741

42+
def custom_str(source, length):
43+
"""
44+
处理字符串长度, 并将'字符转义\'
45+
:param source:
46+
:param length:
47+
:return:
48+
"""
49+
if len(source) > length:
50+
fix_length_sub_str = source[0:length-1]
51+
else:
52+
fix_length_sub_str = source
53+
return fix_length_sub_str.replace("'", "\\'")
54+
55+
3856
def build_logs():
3957
# 获取当前时间
4058
now_time = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime())
@@ -63,8 +81,8 @@ def build_logs():
6381

6482
def connect_mysql(logger):
6583
try:
66-
db = pymysql.connect(host='47.93.206.227', port=3306, user='root', passwd='root', db='spring_clould', charset='utf8mb4')
67-
# db = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='spring_clould', charset='utf8mb4')
84+
#db = pymysql.connect(host='47.93.206.227', port=3306, user='root', passwd='root', db='spring_clould', charset='utf8mb4')
85+
db = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='spring_clould', charset='utf8mb4')
6886
return db
6987
except Exception as e:
7088
logger.debug('MySQL数据库连接失败')
@@ -97,8 +115,7 @@ def select_db(db, sql, logger):
97115

98116
def main():
99117
# 手机扫码登录
100-
newInstance = itchat.new_instance()
101-
newInstance.auto_login(hotReload=True, enableCmdQR=2)
118+
itchat.auto_login(hotReload=True, enableCmdQR=2)
102119
global logger
103120
logger = build_logs()
104121
global db

0 commit comments

Comments
 (0)