UNC 路径的 Python 和 MySQL 转义序列

Python and MySQL escape sequence for UNC path

我有一段时间让加载数据本地 infile 与 python 一起工作。我在 Python 3.6.5 中尝试了很多不同的方法:

sqlpath = '\corp.domain.com\dept\DCGSI\Extracts\SIM\'
sqlpath = "\corp.domain.com\dept\DCGSI\Extracts\SIM\"
sqlpath = '\\corp.domain.com\dept\DCGSI\Extracts\SIM\'
sqlpath = '//corp.domain.com/dept/DCGSI/Extracts/SIM/'
sqlpath = os.path.join("\corp.domain.com","dept","DCGSI","Extracts","SIM")
sqlpath = os.path("\\corp.domain.com\dept\DCGSI\Extracts\SIM\")

为了挽救我的理智,我似乎无法获得用于以下用途的转义权。我在这里至少阅读了 10 篇不同的帖子,并尝试了所有这些建议。我做错了什么所以我可以在 loadQuery 中使用 unc 路径。

这是(当前)整个脚本:

import config
import os
import pymysql


username = config.username
dbpassword = config.dbpassword
dbhost = config.dburl
conn =  pymysql.connect(host=dbhost, port=3306,user=username,password=dbpassword,db='dcgsreports',autocommit=True,local_infile=1)
path = '//corp.domain.com/dept/DCGSI/Extracts/SIM'
tables = []
files = []

fileNames = os.listdir(path)
for fileNames in fileNames:
    if fileNames.endswith(".csv"):
        files.append(fileNames)
        tables.append(fileNames.split('.')[0])

for f,t in zip(files, tables) :
    truncQuery = '''TRUNCATE TABLE %s''' %t
    loadQuery = '''LOAD DATA LOCAL INFILE '//corp.domain.com/dept/DCGSI/Extracts/SIM/%s' INTO TABLE %s FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;''' %(f, t)
    print(loadQuery)
    cursor = conn.cursor()
    cursor.execute(truncQuery)
    cursor.execute(loadQuery)
    conn.commit()
conn.close()

打印语句显示以下是应该作为查询传递的内容(应该是正确的): 将数据本地 INFILE '//corp.domain.com/dept/DCGSI/Extracts/SIM/SIM_Availability.csv' 装入 TABLE SIM_Availability 由 ',' 终止的字段,可选地由 '"' 终止的行括起来 ' 忽略 1 行;

虽然所有表都是空的,但这似乎表明只执行了 truncQuery。

考虑 Python 的 os-agnostic os.path.join(),不要担心反斜杠或正斜杠。并使用 string.format 甚至 Python 3.6 的新 f-string(如 Perl 和 Perl 的 $string 的文字字符串插值)。不建议使用字符串插值的模数运算符。请务必也 commit 行动:

path = r"\corp.domain.com\dept\DCGSI\Extracts\SIM"

for t, f in zip(tables, files) :

    truncQuery = "TRUNCATE TABLE `{t_name}`"
    loadQuery = """LOAD DATA LOCAL INFILE '{f_name}'
                   INTO TABLE `{t_name}` FIELDS TERMINATED BY ',' 
                   OPTIONALLY ENCLOSED BY '"' LINES 
                   TERMINATED BY '\r\n' IGNORE 1 LINES;"""

    print(loadQuery)

    cursor = conn.cursor()
    cursor.execute(truncQuery.format(t_name = t)
    cursor.execute(loadQuery.format(f_name = os.path.join(path, f),
                                    t_name = t))

    cursor.close()
    conn.commit()

有 f-string

for t, f in zip(tables, files) :

    f_name = os.path.join(path, f)

    truncQuery = f"TRUNCATE TABLE `{t}`"
    loadQuery = f"""LOAD DATA LOCAL INFILE '{f_name}'
                    INTO TABLE `{t}` FIELDS TERMINATED BY ',' 
                    OPTIONALLY ENCLOSED BY '"' LINES 
                    TERMINATED BY '\r\n' IGNORE 1 LINES;"""

    print(loadQuery)

    cursor = conn.cursor()
    cursor.execute(truncQuery)
    cursor.execute(loadQuery)

    cursor.close()
    conn.commit()