使用cursor.fetchall()返回空数据框

时间:2018-09-26 12:44:19

标签: python

我有一个SQL查询,该查询可能返回非空记录。我需要在Python中执行以下查询:

yticks

这给了我一个例外:

import os
import pandas as pd
import pymysql
from pandas import ExcelWriter
from sqlalchemy import create_engine

connection = pymysql.connect(host='localhost',
                            user='root',
                            password='root',
                            db='device_db',
                            cursorclass=pymysql.cursors.DictCursor)

cur3=connection.cursor()

cur3.execute('SELECT outgoing_db.Customer_No, sum(SUM_IC_CALLS) AS SumOfSUM_IC_CALLS, sum(SUM_AIC_DURATION_C1) AS SumOfSUM_AIC_DURATION_C1, sum(SUM_AIC_AMOUNT_C1) AS SumOfSUM_AIC_AMOUNT_C1, outgoing_db.Rate, outgoing_db.Customer_Name FROM outgoing_db WHERE (((outgoing_db.User_ID) != ("IND")) AND ((outgoing_db.Incoming_ID)="Airtel") AND ((outgoing_db.Customer_Type)="Check") AND ((outgoing_db.Current_Operation)="NO") AND ((outgoing_db.Active)="NO")) GROUP BY outgoing_db.Customer_No, outgoing_db.Rate, outgoing_db.Customer_Name HAVING (((outgoing_db.Customer_No) In (33165,33197)));')
mydf3=pd.DataFrame(cur3.fetchall())
print(mydf3)

当我做 cur3.fetchone()时,它会给我输出:

Traceback (most recent call last):
  File "C:\wamp64\www\DevicePortal\Query.py", line 16, in <module>
    mydf3=pd.DataFrame(cur3.fetchall())
  File "C:\Users\Sonal\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\frame.py", line 404, in __init__
raise ValueError('DataFrame constructor not properly called!')
ValueError: DataFrame constructor not properly called! 

但是将来可能会添加数据(大约100万)。 是否有使用 cur3.fetchall()

返回空数据帧的解决方案

1 个答案:

答案 0 :(得分:0)

fetchall()返回一个元组列表;如果要从它们构造数据框,则可以使用pandas.DataFrame.from_records()

一种更简单的方法是使用以下方法直接从SQL读取数据:

pandas.read_sql()

它具有一个chunksize参数,如果您的数据集太大,则允许您逐块加载数据。

在此处阅读:

https://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.DataFrame.from_records.html https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_sql.html