Skip to content

[pandas] writing BLOB columns with to_sql()

Gord Thompson edited this page Mar 8, 2021 · 1 revision

If you have a DataFrame with one or more columns that contain binary (BLOB) data you need to use the dtype argument to tell to_sql() to create the column as large binary (a.k.a. "OLE Object" in the Access UI). For example, if your DataFrame looks like this

print(df)
"""
   ID FirstName                    Photo
0   1      Gord  b'\x01\x00\x02\x00\x03'
"""

then your to_sql() call would be

import sqlalchemy_access as sa_a

df.to_sql("my_table", engine, if_exists="replace", index=False, 
          dtype={'Photo': sa_a.OleObject})