2017-12-18 69 views
0

从蟒蛇进口pypyodbc因为我想插入图片文件到SQL Server
上传图像文件导入pypyodbc

SQLInsertImg = "insert into Register1 (logo) SELECT BulkColumn FROM Openrowset (Bulk 'C:\Users\PC\Desktop\Capture.JPG', Single_Blob) as img" 


在Python Shell

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 72-73: truncated \UXXXXXXXX escape

相同的查询,当在MSSQL查询表中执行时,结果保持良好,可能会发生什么错误?

我已经回答到现在我的问题出现的问题与

import pypyodbc 

cnxn = pypyodbc.connect('Driver={ODBC Driver 13 for SQL Server};' 
        'Server=DESKTOP-C6RS3DO;' 
        'Database=demo2016;' 
        'uid=sa;pwd=sa') 
print('connected <br/>') 
cursor = cnxn.cursor() 

SQLInsertImg = "insert into Register1 (logo) SELECT BulkColumn FROM Openrowset (Bulk 'C:\\Users\PC\Desktop\Capture.JPG', Single_Blob) as img" 


Traceback (most recent call last): 
     File "<stdin>", line 1, in <module> 
     File "C:\Users\Vitriv-Desktop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pypyodbc.py", line 1626, in execute 
     self.execdirect(query_string) 
     File "C:\Users\Vitriv-Desktop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pypyodbc.py", line 1652, in execdirect 
     check_success(self, ret) 
     File "C:\Users\Vitriv-Desktop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pypyodbc.py", line 1007, in check_success 
     ctrl_err(SQL_HANDLE_STMT, ODBC_obj.stmt_h, ret, ODBC_obj.ansi) 
     File "C:\Users\Vitriv-Desktop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pypyodbc.py", line 975, in ctrl_err 
     raise ProgrammingError(state,err_text) 
    pypyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Cannot bulk load because the file "C:\\Users\\PC\\Desktop\\Capture.JPG" could not be opened. Operating system error code 3(The system cannot find the path specified.).') 

以上蟒蛇外壳结果执行cursor = cnxn.cursor()后?

回答

0

在Windows的典型错误,因为默认的用户目录为C:\user\<your_user>,所以当你想用这个路径作为一个字符串参数转换为Python功能,你会得到一个Unicode错误,只是因为\u是Unicode逃逸。在此之后的任何字符都不会产生错误。

要解决它,只需加倍反斜杠:C:\\\user\\\<\your_user>...