代码如下:
import itchatmp
import pyodbc
itchatmp.update_config(itchatmp.WechatConfig(
token='*******替换成自己的********',
appId = '******替换成自己的*********',
appSecret = '*******替换成自己的********'))
@itchatmp.msg_register(itchatmp.content.TEXT)
def text_reply(msg):
rcmsg=msg['Content']
if rcmsg=="生日":
rcmsg=TodayBirth()
return rcmsg
def TodayBirth():
server = '*******\\SQLEXPRESS'
database = '*******'
username = 'sa'
password = '*******'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
#Sample select query
cursor.execute("select * from namelist where "+
"(month(getdate()) = month(birth) and day(getdate()) = day(birth)) ;")
rctb ="今天过生日的有:\r\n"
row = cursor.fetchone()
while row:
if row[2]=="男":
row[2]="帅哥"
elif row[2]=="女":
row[2]="美女"
rctb =rctb+"学号是:"+str(row[0])+" 生日为:" + str(row[1]) +" 是位"+row[2]+ "\r\n"
row = cursor.fetchone()
cursor.execute("select * from namelist where "+
"(month(getdate()) = month(birth) and day(getdate()+1) = day(birth)) ;")
row = cursor.fetchone()
rctb =rctb+"明天过生日的有:\r\n"
n = 0
while row:
if row[2]=="男":
row[2]="帅哥"
elif row[2]=="女":
row[2]="美女"
rctb =rctb+"学号是:"+str(row[0])+" 生日为:"
+str(row[1]) +" 是位"+row[2]+ "\r\n"
row = cursor.fetchone()
if n==0:
rctb =rctb+"明天没有人过生日。。。。"
cnxn.commit()
cursor.close()
cnxn.close()
return rctb
itchatmp.run()
得到的结果: