再上篇中擷取到的訊息只會即時傳到聊天室,並不會保存下來。要保存訊息的話,就要把訊息保存到資料庫。
這邊設計資料表訊息與LINE emoji存放的資料表,message_emoji的mid為FK關聯到group_buying_message的mid
group_buying_message
mid|keyword|message_text
---|---
message_emoji
eid|mid|index|product_id|emoji_id
---|---
mid為FK關聯到group_buying_message的mid
def update_emoji_message(text,keyword,emojis):
cursor = conn.cursor()
cursor.execute("INSERT INTO group_buying_message (message_text, keyword) VALUES (%s, %s);", (text, keyword))
cursor.execute(f"SELECT mid FROM group_buying_message WHERE message_text='{text}';")
row = cursor.fetchone()
for emoji in emojis:
cursor.execute("INSERT INTO message_emoji (mid, index, product_id, emoji_id) VALUES (%s, %s, %s, %s);", (row, emoji.index, emoji.product_id, emoji.emoji_id))
conn.commit()
cursor.close()
print("successfully update emoji message")