mysql - Python tempfile NamedTemporaryFile 文件在尝试访问它以进行读取时未找到
问题描述
无法访问临时 csv 文件并且它给出的错误是 MySQLdb._exceptions.InternalError: (2, "File '/tmp/tmpc1hsxls9' not found (Errcode: 2 - No such file or directory)")。任何有关它的建议都将是非常可观的。
tmpfile = None
with NamedTemporaryFile("wb") as tmpfile:
self.log.info("Selecting rows from MySql to local file %s...", tmpfile.name)
self.log.info("select mtime, dpid, route_id, skid, `group`, family, date, sale, dprice, rprice, dcc_price, issue, `return`, memos, vmemos, tlp, cnc, vp, mvp, p, tcc, dcc, ecnc, gt, struc, semi_struc, streetk, mass_hrc, pop_hrc, prem_hrc, kaccounts, ogrocery, snb_cnc, pay_n_go, shop_n_browse, entertainment, outlets, apps_version, updated, visited from daily_sales_msr where date between '"+day+"' and '"+day+"'")
# self.log.info("select mtime, dpid, route_id, skid, group, family, date, datetime, sale, dprice, rprice, dcc_price, issue, return, memoss, vmemos, tlp, cnc, vp, mvp, p, tcc, dcc, ecnc, gt, struc, semi_struc, streetk, mass_hrc, pop_hrc, prem_hrc, kaccounts, ogrocery, snb_cnc, pay_n_go, shop_n_browse, entertainment, outlets, apps_version, updated, visited from daily_sales_msr where month(date) = month("+month+") and year(date) = year("+month+")")
# self.log.info(self.sql)
csv_writer = csv.writer(tmpfile, delimiter='\t', encoding='utf-8')
result = cursor.fetchall()
self.log.info(result)
for row in result:
self.log.info(row)
csv_writer.writerow(row)
count += 1
tmpfile.flush()
with closing(mysql_destination.get_conn()) as conn:
with closing(conn.cursor()) as cursor:
cursor.execute(
"LOAD DATA LOCAL INFILE '%s' INTO "
"TABLE %s LINES TERMINATED BY '\r\n' (%s)" %
(tmpfile.name,
self.mysql_destination_table,
", ".join(selected_columns))
)
conn.commit()
tmpfile.close()
解决方案
推荐阅读
- javascript - 提示不显示
- ajax - 使用循环中的 Ajax 调用发送邮件
- html - 使项目更大而不会碰到 html 中的其他项目
- java - Jetty 9 客户端设置 Content-Range 值正常不起作用
- angular - Angular 9:不订阅其他组件?
- python - 将通过列的函数,如果数字大于 0 返回列名,但当为 0 时返回“不可用”
- r - 在 R 中使用 Plotly 绘制显示毫秒的时间戳
- x86-64 - 在这个简单的例子中调用汇编指令做什么?
- node.js - API中的Put方法总是收到空的请求体
- unity3d - 如何检查 Unity 中 NavMeshAgent 的路径是否存在到 NavMesh 上的某个点?