python脚本根据时间而不是日期轮询MSSQL

时间:2015-07-24 19:49:03

标签: python mysql sql-server datetime

Python脚本 - 我试图在最后一小时从MSSQL数据库中提取呼叫,而不是基于当前脚本的最后一天,然后将信息上传到mssql数据库。我在最后一小时发出代码时遇到了错误。任何人都可以看看,看看他们是否看到代码有什么问题。谢谢你的帮助。

运行时出现错误消息 - >错误是文件" update_calls_test_table.py" /第121行,在main(),文件" update_calls_test_teable.py:,第10行+6,在update_calls_test_table""" ")文件" /usr/local/li/python3.4/site-packages/pypyodbc.py" 1605m执行self.execdirect(query_string),check_success(self,ret),ctrl_err(SQL_HANDLE_STMT,ODBC_obj.stmt_h,ret,ODBC_obj.ansi)

#!/usr/bin/env python3
"""

This script collects data from the SQL database and a daily
.csv file sent as an e-mail attachment. It takes that data and inserts
it into a MySQL table called calls
"""

import uct_db as config
import logging
import pypyodbc
import mysql.connector
import imaplib
import smtplib
import email
import csv
import io
import time
import datetime

from datetime import date, timedelta, datetime

mysql_user  = config.mysql_user
mysql_pass  = config.mysql_pass
mysql_host  = config.mysql_host
mysql_db    = config.mysql_db
mysql_table = config.mysql_table

toll_free_dict  = {}
affiliate_dict  = {}
station_dict    = {}
code_dict       = {}

now = datetime.today()
now = now.strftime('%Y-%m-%d %H:%M') 
lastHourDateTime = datetime.today() - timedelta(hours = 1)
lastHourDateTime = lastHourDateTime.strftime('%Y-%m-%d %H:%M') 


mydb = mysql.connector.connect(user = mysql_user, password = mysql_pass,       host = mysql_host, database = mysql_db)

tvdb =          pypyodbc.connect('DRIVER=FreeTDS;SERVER=TeleVantage;Port=1433;UID=tv_browse;PWD=    tv_browse_account')
tv_cursor = tvdb.cursor()


def main():
build_lists()
update_calls_test_table()
mydb.close()


def build_lists():
"""Connects to the MySQL server and gets information from
   the codes_mercury table.
Args:
    none
Returns:
    none
"""

my_cursor = mydb.cursor()

my_cursor.execute("""
                  SELECT did, tollfree, promo, station, code
                  FROM codes_mercury
                  """)

for (did, tollfree, promo, station, code) in my_cursor:
     toll_free_dict.update({did: tollfree})
     affiliate_dict.update({did: promo})
     station_dict.update({did: station})
     code_dict.update({did: code})
my_cursor.close()


def update_calls_test_table():
"""Processes last hour calls and adds them to the calls table
Args:
    none
Returns:
    Boolean
"""
processed = 0
inserted = 0

my_cursor = mydb.cursor()

tv_cursor.execute("""
                  SELECT 
                  SUBSTRING(CustomData,16,3) AS inquiry, 
                  SUBSTRING(CallerIDNumber,2,10) AS src, 
                  CONVERT(VARCHAR(10), Starttime, 101) AS calldate,
                  CONVERT(VARCHAR(5), Starttime, 108) AS time,
                  DIDNumber,
                  result = CASE Result WHEN 3 THEN 'Hang Up' ELSE   'Connected' END,
                  center = CASE AnsweredByFirstName WHEN 'LiveOps' THEN  'LiveOps' Else  'ARW' END,
                  DateDiff(second, StartTime, StopTime) AS duration
                  FROM dbo.CallLog 
                  WHERE (CONVERT(VARCHAR(18), StartTime, 110) BETWEEN  CONVERT(VARCHAR(18), """+now+""" , 110) AND CONVERT(VARCHAR(18), """+lastHourDateTime+""" , 110))
                  AND (DIDNumber BETWEEN '5614171630' AND '5614171729' OR
                  DIDNumber BETWEEN '5614171000' AND '5614171038' OR
                  DIDNumber BETWEEN '5619816600' AND '5619816619' OR
                  DIDNumber BETWEEN '5613672700' AND '5613672739' OR
                  DIDNumber BETWEEN '5613672780' AND '5613672799')
                  order by calldate, time
                  """)

rows = tv_cursor.fetchall()

for row in rows:
    processed += 1

    print(row)



my_cursor.close()


if __name__ == "__main__":
main()

0 个答案:

没有答案