(三)连接SQL

代码如下:

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()

得到的结果:

3.1运行结果.jpg
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,523评论 25 708
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,769评论 18 399
  • 文件大小2.3MB,作者君尽力了。。 1. 为什么看了那么多书,却什么也记不住?为什么聊了那么多天,却什么也学不会...
    夏小月阅读 1,018评论 1 3
  • 刚刚看到一篇文章,很多人的生活中,这世界上太多人都不敢得罪,上班同事间谨言慎行,朋友间说话也是客客气气,对...
    方圆_22cf阅读 277评论 0 1
  • 生活,就是这么不尽如人意! 朋友终于恋爱了,值得可喜可贺的事情。昨晚他给我打电话说心情很复杂,前几天背着女方家长去...
    f3c5470326c4阅读 318评论 0 0