0

I need python to issue a query via pyodbc to insert a .PNG file into a table as a blob. The problem seems to have something to do with how the path to the file is represented. Here's some code:

    OutFilePath = 'd:\\DilerBW_images\\'

    OutFileName = SubjID+'_'+str(UserID)+'_'+DateTime+'.png'

    print OutFilePath+OutFileName        
    qstring = ('insert into [wpic-smir].[Diler_BW].[Beckwith].[PlotImages](ID, UserID, ImageType, MoodType, ImageIndex, ImageData)'
    'select '+str(SubjID)+' as ID, '+str(UserID)+' as UserID,'
    '1 as ImageType,'
    'NULL as MoodType,'
    'NULL as ImageIndex,'
    '* from OPENROWSET(Bulk \''+OutFilePath+OutFileName+'\', SINGLE_BLOB) as ImageData')
    print qstring

    cursor.execute(qstring)
    conn.commit()

` And here's the output:

   d:\DilerBW_images\999123_999123_2015-01-20_14-25-07.013000.png

    insert into [wpic-smir].[Diler_BW].[Beckwith].[PlotImages](ID, UserID, ImageType, MoodType, ImageIndex, ImageData)select 999123 as ID, 999123 as UserID,1 as ImageType,NULL as MoodType,NULL as ImageIndex,* from OPENROWSET(Bulk 'd:\DilerBW_images\999123_999123_2015-01-20_14-25-07.013000.png', SINGLE_BLOB) as ImageData

Now, here's the error I get:

Traceback (most recent call last):
File "c:\pythonscripts\DilerBW_Plot_Single_report_2_EH.py", line 253, in <module>
cursor.execute(qstring)
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot bulk load because the file "d:\\DilerBW_images\\999123_999123_2015-01-20_14-25-07.013000.png" could not be opened. Operating system error code 3(The system cannot find the path specified.). (4861) (SQLExecDirectW)')

Sorry this is so long. Notice that the file path in the error message includes double backslashes, where the query only includes singles. I have looked extensively at the various methods to build the string (os.path.sep, using a raw string, os.path.join), but it doesn't seem to matter, and I'm not certain the input string is the problem. Again, if I cut and paste the query as it's presented in the output into SSMS and execute it, it works fine.

Thanx.

Alphecca
  • 11
  • 3
  • Take a look at my answer [here](http://stackoverflow.com/a/11847994/366335) for inserting blob data without the use of `OPENROWSET`. – Bryan Mar 25 '15 at 18:30
  • While I'd still like to see someone address the double-backslash issue I'm having, Bryan's "workaround" worked just fine. Thanx!! – Alphecca Mar 30 '15 at 14:40

0 Answers0