Skip to content

Instantly share code, notes, and snippets.

@medmin
Last active December 3, 2020 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save medmin/54cd48aab25a520fc0dc9ba4a4489609 to your computer and use it in GitHub Desktop.
Save medmin/54cd48aab25a520fc0dc9ba4a4489609 to your computer and use it in GitHub Desktop.
SaveAllWeixinMessages
# -*- coding: utf-8 -*-
from wxpy import *
import pymysql.cursors
import datetime
bot = Bot(console_qr=True)
@bot.register(None, TEXT, False)
def saveMsg(msg):
# Manipulate msg string
msgID = msg.id
msgRaw = str(msg)
msgSrcName = msg.chat.name
msgSrcID = msg.chat.wxid
msgTextContent = msg.text
msgCreatedDate = msg.create_time.date()
msgCreatedDatetime = msg.create_time
# Connect to the database
connection = pymysql.connect(host='localhost',
user='myDBUser',
password='myDBpw',
db='myDB',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `myTable` (`msgID`, `msgRaw`, `msgSrcName`, `msgSrcID`, `msgTextContent`, `msgCreatedDate` ,`msgCreatedDatetime`) VALUES (%s, %s, %s, %s, %s, %s, %s)"
cursor.execute(sql, (msgID, msgRaw, msgSrcName, msgSrcID, msgTextContent, msgCreatedDate, msgCreatedDatetime))
# connection is not autocommit by default. So you must commit to save your changes.
connection.commit()
finally:
connection.close()
bot.join()
CREATE TABLE `myTable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`msgID` BIGINT UNSIGNED NOT NULL,
`msgRaw` TEXT NOT NULL,
`msgSrcName` varchar(255) NOT NULL,
`msgSrcID` varchar(255) NOT NULL,
`msgTextContent` TEXT NOT NULL,
`msgCreatedDate` DATE NOT NULL,
`msgCreatedDatetime` DATETIME NOT NULL,
`DaysToExpirationDate` int(11) NOT NULL DEFAULT 90,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_general_ci AUTO_INCREMENT=1;
//注意上面的:
//1。msgID是64位整型,正整数
//2。DATE和DATETIME类型
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment