插入期间pyodbc和文件夹路径中的特殊字符

时间:2017-10-19 14:59:28

标签: sql-server python-3.x pyodbc

我想将所有pdf文件收集到一个文件夹中,然后将它们插入到MSSQL表中。它适用于查询编辑器,但由于有很多文件需要编写脚本,但我对文件夹路径中的特殊字符有问题,如ö,ş,ı,ü,ğ。它们在打印输出上正确显示,但在插入过程中会出现如下错误。

from glob import glob
import os, os.path
import pyodbc

encoding = 'utf-8'

files = []
customers = []
paths = []

for root, dirs, files in os.walk(r"C:\Users\xxx\Desktop\Sync Folder\Customers"):
    for f in files:
        fullpath = os.path.join(root, f)
        if os.path.splitext(fullpath)[1] == '.pdf':
            if "OLD" not in fullpath:
                files.append(f)
                customers.append(fullpath.split('/')[-1])
                paths.append(fullpath)

for i in range(0, len(customers)):
    cnxn = pyodbc.connect('DSN=DSN;UID=sa;PWD=PWD;DATABASE=DB')
    cursor = cnxn.cursor()
    cursor.execute("INSERT INTO PDF_TABLE(FILE, CUSTOMER) VALUES((SELECT * 
FROM OPENROWSET(BULK '{0}', SINGLE_BLOB) AS BLOB), '{1}')".format(paths[i],
customers[i]))

错误:

pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][SQL Server Native 
Client 11.0][SQL Server] 
"C:\\Users\\xxx\\Desktop\\Sync Folder\\Customers\\xxx S�STEM\\14.03.2017  \'
AND xxx PROJES�.pdf" dosyas� yok. (4860) (SQLExecDirectW)')

?标记在打印输出上可以正确显示。

谢谢

0 个答案:

没有答案