上传从python

时间:2017-12-18 12:26:33

标签: python sql-server

从python导入pypyodbc,因为我试图将图像文件插入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'编解码器无法解码位置72-73中的字节:截断\ UXXXXXXXX转义

同样的查询,在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.).')

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

1 个答案:

答案 0 :(得分:0)

Windows上的典型错误,因为默认用户目录是C:\user\<your_user>,因此当您想将此路径用作Python函数的字符串参数时,您会收到Unicode错误,因为\u是一个Unicode转义。此后任何非数字字符都会产生错误。

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