从python导入pyodbc上传图像文件

2024-03-28 22:58:26 发布

您现在位置:Python中文网/ 问答频道 /正文

从python导入pyodbc,因为我正在尝试将图像文件插入SQL Server

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.).')

在上面的pythonshell结果中执行cursor = cnxn.cursor()之后?


Tags: insqlserverlocallineusersappdatacursor
1条回答
网友
1楼 · 发布于 2024-03-28 22:58:26

Windows上的典型错误,因为默认用户目录是C:\user\<your_user>,所以当您想将此路径用作Python函数的字符串参数时,会出现Unicode错误,因为\u是Unicode转义。在此之后的任何非数字字符都会产生错误。你知道吗

要解决这个问题,只需将反斜杠加倍:C:\\\user\\\<\your_user>...

相关问题 更多 >