免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3145 | 回复: 3
打印 上一主题 下一主题

急问,用python连接mysql的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-11-24 11:14 |只看该作者 |倒序浏览

  1. def sql():
  2.       try:
  3.           connect = MySQLdb.connect(host = '127.0.0.1',port = 3306,user = 'scm'',passwd = 'lead',db = 'scm')
  4.           try:
  5.               find = 'select baseline,address from mail where time <= adddate(now(),interval 1 minute) and time >= subdate(now(),interval 1 minute)'
  6.               while 1:
  7.                   cursor = connect.cursor()
  8.                   cursor.execute(find)
  9.                   for row in cursor.fetchall():
  10.                        print row[0]
  11.                        print row[1]
  12.                        sendmail(row[0],row[1])
  13.                        cursor.close()
  14.                        time.sleep(120)
  15.            except:
  16.                connect.close()
  17.                error('command ',2)
  18.     except:
  19.         error('mysql was not connected',2)
复制代码


这是连接并过2分钟查询一次mysql的一个函数,里面的sendmail和error都是函数。
现在有一个问题:在这个程序运行期间,我手动连接mysql,修改里面的数据。但程序运行的结果还是修改之前的数据。不知道为什么。
游标每次都会关掉啊,怎么找不到新的数据呢。

[ 本帖最后由 wolfg 于 2005-11-24 14:13 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2005-11-24 18:22 |只看该作者
哈哈,,是你~~ 晚上回去再想想看。。

论坛徽章:
0
3 [报告]
发表于 2005-11-25 13:30 |只看该作者
sql是个函数,你在函数内部打开了它。而没有传回全局。你再次调用实际上是又打开了个新连接。
另外,你修改的数据好象没有提交。

论坛徽章:
0
4 [报告]
发表于 2005-11-25 13:50 |只看该作者

回复 3楼 xichen 的帖子

import smtplib,time,MySQLdb

def sendmail(baseline,To):
        mailserver = '192.168.0.10'
        From = 'ha@haha.com'
        to = [To,'haha@haha.com']
        date = time.ctime(time.time())
        text = ('From: %s\nTo: %s\nDate: %s +0800\nSubject: 提交制品\nContent-Type: text/plain;charset="gb2312"\nContent-Transfer-Encoding: 8bit\n\n明天提交  %s,\n如不能准时提交,请在sharepoint上填写软件问题报告单和变更申请单.'%(From,To,date,baseline))
        try:
                server = smtplib.SMTP(mailserver)
                send = server.sendmail(From,to,text)
                server.quit()
        except:
                error(baseline,1)

def error(log,id):
        datetime = time.ctime(time.time())
        file = open('error_log.txt','a')
        if id == 1:
                file.write('sendmail: ''%s was not sent!!! %s\n'%(log,datetime))
        elif id == 2:
                file.write('mysql: %s %s\n'%(log,datetime))
        file.close()

def sql():
        try:
                connect = MySQLdb.connect(host = '127.0.0.1',port = 3306,user = 'root',passwd = 'lead',db = 'scm')
                try:
                        find = 'select baseline,address from mail where time <= adddate(now(),interval 1 minute) and time >= subdate(now(),interval 1 minute)'
                        while 1:
                                cursor = connect.cursor()
                                cursor.execute(find)
                                for row in cursor.fetchall():
                                        print row[0]
                                        print row[1]
                                        sendmail(row[0],row[1])
                                cursor.close()
                                time.sleep(120)
                except:
                        connect.close()
                        error('command ',2)
        except:
                        error('mysql was not connected',2)
sql()

谢谢版主,哈哈。以上是整个程序。sql其实是主函数,只调用一次,程序不修改数据只查询。我的意思是程序运行的同时,我自己连mysql并修改数据,在这种情况下我这个程序查询出来的不是最新的数据,还是修改之前的数据。搞不明白为什么。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP