Python 将pdf插入sql表';s像场
我有一个python脚本,需要将pdf插入到图像字段中的SQL表中。 我可以在这个字段中添加任何内容——数字、文本、十六进制版本的pdf,但我不能使用字节或字节数组添加pdf。文档图像字段定义为图像。我已经验证了sql语句本身是正确的-如果我对文档图像使用null 在下面的代码中,我尝试了hex_报告(它将插入,但adobe不可读), array_report和bin_report-这两种报告给出相同的错误Python 将pdf插入sql表';s像场,python,sql,image,pdf,bytearray,Python,Sql,Image,Pdf,Bytearray,我有一个python脚本,需要将pdf插入到图像字段中的SQL表中。 我可以在这个字段中添加任何内容——数字、文本、十六进制版本的pdf,但我不能使用字节或字节数组添加pdf。文档图像字段定义为图像。我已经验证了sql语句本身是正确的-如果我对文档图像使用null 在下面的代码中,我尝试了hex_报告(它将插入,但adobe不可读), array_report和bin_report-这两种报告给出相同的错误 report_map = "C:\\simple_test_pdf.pdf" initi
report_map = "C:\\simple_test_pdf.pdf"
initial_report = open(report_map, 'rb').read()
hex_report = binascii.b2a_hex(initial_report)
array_report = bytearray(initial_report)
bin_report = bytes(initial_report)
db = abc_sql()
db.Set_database("image")
conn = db.Open_Connection()
conn.execute_scalar("DECLARE @return_value int, \
@new_serial_id int, \
@return_msg varchar(4000) \
EXEC @return_value = image.image_insert \
@new_serial_id = @new_serial_id OUTPUT, \
@return_msg = @return_msg OUTPUT, \
@pd_request_id = 776, \
@mime_type = 'application/pdf', \
@document_file_name = 'Report_Results.pdf', \
@document_description = 'Report and Map', \
@document_image = %s \
SELECT @new_serial_id as '@new_serial_id',\
@return_msg as '@return_msg', \
'Return Value' = @return_value", str(bin_report ))
我得到的错误是:
mssql.MSSQLSDatabaseException:SQL Server消息105,严重性15,状态1,第1行:
字符串“%PDF-1.5”后未闭合的引号
%âãÏÓ
10 0 obj
endobj
15 0 obj
。。。
“%PDF-1.5”附近的语法不正确。。。(截断)能否显示表结构,以便我们从那里开始 为此,我将使用varbinary(MAX)。它会给你一些灵活性。
你也可以在上找到一些帮助。你可以这样做
INSERT INTO Table(ID, Blob)
SELECT 0, * FROM OPENROWSET(BULK N'C:\YourPathFolder\File.pdf', SINGLE_BLOB) rs
如果要将该TSQL语句参数化,可以这样做:
declare @path varchar(5000)
declare @SQL varchar(5000)
set @path = 'c:\YourPath\Yourfile.pdf'
set @SQL = 'INSERT INTO Table(ID, Blob)
SELECT 0, * FROM OPENROWSET(BULK N''' +@CertPath +''', SINGLE_BLOB) rs'
execute (@SQL)
话虽如此,我不建议使用图像字段——它很快就会被弃用。按照MS的建议,使用varBinary(max) 相关字段被定义为document_image(image,null)。更改字段类型不是一个选项,因为它被许多其他人使用。我想知道它是否与返回有关。十六进制格式是一组连续的数字。字节和数组有很多特殊的符号和carraige返回(不可见)。有什么理由在文件流上使用图像吗?看见