将Excel值附加到SQL查询

时间:2018-08-02 06:51:38

标签: python sql pandas csv

import pymysql
import csv
from O365 import Message
import pandas
import sys

#Database Connection
connection = pymysql.connect(host="localhost", user="root", passwd="nishu123", database="last")
cursor = connection.cursor()
query = "SELECT merchant.m_id,merchant.m_name, sum(case when status='captured' then 1 else 0 end)/count(*)*100 as Success_Per ,merchant.status,merchant.issuer, merchant.payment_gateway, merchant.amount, waste.threshold_value, waste.email_id FROM merchant INNER JOIN waste ON merchant.m_name = waste.m_name group by m_name;"
df = pandas.read_sql(query, connection)

connection.close()

df.to_csv('yo.csv')
print('Done')

def findmail():
    print("Entering IN")
    db = pymysql.connect(host="localhost", user="root", passwd="nishu123", database="last")
    cur = db.cursor()
    query = "select email_id from waste where m_name='m_name';"
    data=cur.execute(query)
    data=cur.fetchall()
    print(data)
    def sendemail():
        o365_auth = ("username", "password")
        m = Message(auth=o365_auth)
        m.setRecipients(data)
        m.setSubject('Performance Decreased')
        m.setBody('Hi...')
        m.sendMessage()
    sendemail()

with open('yo.csv') as csvfile:
    csvfile.seek(0, 0)
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        if row[3] < row[8]:
            findmail()
        else:
            print("everything is fine")

我想通过使用Email_idcsv文件中获取匹配值来从SQL中找到Pandas值。 csv文件包含列name =m_id,m_name,status,successrate,thresholdvalue, 当我将successrate与阈值进行比较时 在这段代码中,当游标转到查询部分的findmail函数时,它不会重试任何内容。

0 个答案:

没有答案