Python数据库连接和结果集成为字符串

时间:2015-01-27 11:32:20

标签: python sql-server-2008 python-2.7 pyodbc

我差不多完成了一个脚本,它会检查一个表并通过电子邮件发送这个结果。虽然我收到了这个错误。

No results.  Previous SQL was not a query.

这就是我的脚本的样子。

import os
import sys
import pyodbc
import smtplib

# Set up mail info
FROM = "it@company.se"
TO = ["pd@company.se"] # must be a list
TEXT = "Dialer import status "
SUBJECT = "Dialer import status"

#Defining variables for script 
try:
    cnxn = pyodbc.connect('DRIVER={SQL  Server}
    ;SERVER=sesrv413;DATABASE=Maintenance;Trusted_Connection=yes')
    cursor = cnxn.cursor()
    query = "Select status from dialer_import_status"
    rows =  cursor.fetchall()
    for row in rows:
    TEXT = TEXT + str(row.status)

except:
# Send the mail
  server = smtplib.SMTP("email.intrum.net",25)
  message = "From:%s \nTo:%s \nSubject:%s 
  \n\n%s\n" %(FROM, ", ".join(TO),  SUBJECT, TEXT)
  server = smtplib.SMTP("email.company.net",25)
  server.sendmail(FROM, TO, message)
  server.quit()
  raise

之前是否有人遇到类似问题?你是怎么解决的?

干杯

1 个答案:

答案 0 :(得分:2)

未执行任何查询,因为您忘记在cursor.execute(query)之前致电cursor.fetchall()