TypeError: cant escape psycopg2.extensions.Binary to binary

2024/9/8 10:41:53

I try to store binary file into postgresql through sqlalchemy and file is uploaded from client. A bit google on the error message brings me to this source file:" wrapped object is not bytes or a buffer, this is an error"

   binaries = []for f in request.files.values():if f and allowed_file(f.filename):fn = secure_filename(f.filename)file_path = os.path.join(basedir, fn)f.save(file_path)#data = f.read()data = open(fn, 'rb').read()binaries.append(psycopg2.Binary(data))f.close()#does the escapingmytable=mytable(...,document1 = binaries[0]...)#Modelclass mytable(mydb.Model):document1 = mydb.Column(mydb.LargeBinary())
Answer

Casting the bytes from the file to psycopg2.Binary is unnecessary. It will happen automatically when SQLAlchemy when sends the statement and values to the database (using the DBAPI connector, which would be psycopg2 in this case).

Something like

with open(fn, 'rb') as f:bytes_ = f.read()instance = MyModel(document1=bytes_)session.add(instance)session.commit() 

works in both Python2 and Python3, SQLAlchemy 1.3.x, generating this output from the engine:

2020-09-06 10:39:27,775 INFO sqlalchemy.engine.base.Engine INSERT INTO mytable (document1) VALUES (%(document1)s) RETURNING mytable.id
2020-09-06 10:39:27,775 INFO sqlalchemy.engine.base.Engine {'document1': <psycopg2.extensions.Binary object at 0x7f8ea012ff60>}
https://en.xdnf.cn/q/72423.html

Related Q&A

Keras: Cannot Import Name np_utils [duplicate]

This question already has answers here:ImportError: cannot import name np_utils(19 answers)Closed 6 years ago.Im using Python 2.7 and a Jupyter notebook to do some basic machine learning. Im following…

Python 3 string index lookup is O(1)?

Short story:Is Python 3 unicode string lookup O(1) or O(n)?Long story:Index lookup of a character in a C char array is constant time O(1) because we can with certainty jump to a contiguous memory loca…

Using PIL to detect a scan of a blank page

So I often run huge double-sided scan jobs on an unintelligent Canon multifunction, which leaves me with a huge folder of JPEGs. Am I insane to consider using PIL to analyze a folder of images to detec…

Pandas: Filling data for missing dates

Lets say Ive got the following table:ProdID Date Val1 Val2 Val3 Prod1 4/1/2019 1 3 4 Prod1 4/3/2019 2 3 54 Prod1 4/4/2019 3 4 54 Prod2 4/1/2019 1 3 3…

Linear Regression: How to find the distance between the points and the prediction line?

Im looking to find the distance between the points and the prediction line. Ideally I would like the results to be displayed in a new column which contains the distance, called Distance.My Imports:impo…

How to draw a Tetrahedron mesh by matplotlib?

I want to plot a tetrahedron mesh by matplotlib, and the following are a simple tetrahedron mesh: xyz = np.array([[-1,-1,-1],[ 1,-1,-1], [ 1, 1,-1],[-1, 1,-1],[-1,-1, 1],[ 1,-1, 1], [ 1, 1, 1],[-1, 1, …

How to set seaborn jointplot axis to log scale

How to set axis to logarithmic scale in a seaborn jointplot? I cant find any log arguments in seaborn.jointplot Notebook import seaborn as sns import pandas as pddf = pd.read_csv("https://storage…

Convert decision tree directly to png [duplicate]

This question already has answers here:graph.write_pdf("iris.pdf") AttributeError: list object has no attribute write_pdf(10 answers)Closed 7 years ago.I am trying to generate a decision tree…

Python: can I modify a Tuple?

I have a 2 D tuple (Actually I thought, it was a list.. but the error says its a tuple) But anyways.. The tuple is of form: (floatnumber_val, prod_id) now I have a dictionary which contains key-> p…

Saving scatterplot animations

Ive been trying to save an animated scatterplot with matplotlib, and I would prefer that it didnt require totally different code for viewing as an animated figure and for saving a copy. The figure show…