【发布时间】:2018-03-23 11:47:19
【问题描述】:
这是一个我无法找到答案的简单问题。我有一个带有两个命令的 .SQL 文件。我想让 Pandas 将这些命令的结果提取到 DataFrame 中。
SQL 文件的命令就是这样,较长的查询使用今天的日期。
SET @todaydate = DATE(NOW());
SELECT ...long query....;
在建立连接 (prod_db) 后,我尝试通过以下方式使用 read_sql,并收到错误消息“'NoneType' object is not iterable”
sqlpath = 'path.sql'
scriptFile = open(sqlpath,'r')
script = scriptFile.read()
df = pd.read_sql(script,prod_db)
我也尝试使用此处描述的功能和方法reading external sql script in python,但我不确定如何将结果放入 pandas 数据框(或者我可能遗漏了一些东西)。它似乎没有读取结果,因为我反复得到“跳过命令”。
def executeScriptsFromFile(filename):
fd = open(filename, 'r')
sqlFile = fd.read()
fd.close()
# all SQL commands (split on ';')
sqlCommands = sqlFile.split(';')
# Execute every command from the input file
for command in sqlCommands:
try:
c.execute(command)
except OperationalError, msg:
print "Command skipped: ", msg
df = executescriptsfromfile(sqlpath)
【问题讨论】:
标签: python pandas dataframe path mysql-python