Issue
I'm fairly new to programming in general and I'm trying to build a simple website as a pet project.
It's built with python using Flask and SQLAlchemy for the db.
I'm trying to store a QR Pillow Image(as generated or later converted to PNG) in the database by converting it to a byte stream with BufferIO and then encoding it to base64 UTF-8 as a byte string (this is where I might be doing something wrong) and then storing it in a String Column in the database.
Printing the string to the console, I can't see anything wrong with it, but I'm being met with a database error and it seems I hit a wall.
I would appreciate if anyone could point me in the right direction or if I should try something else entirely as I tried a few different methods and nothing seems to work. Thank you!
The error I`m faced with:
(sqlite3.InterfaceError) Error binding parameter 2 - probably unsupported type.
[SQL: INSERT INTO qrdb (data, qr_b64s, date, owner_id) VALUES (?, ?, CURRENT_TIMESTAMP, ?)]
[parameters: ('test', 'iVBORw0KGgoAAAANSUhEUgAAASIAAAEiAQAAAAB1xeIbAAABjklEQVR4nO2aQW6kMBRE3x9bytJIOUCOYm4wZ50bNEeZA0Syl0hu1SzAnSa9mGxoaLAXH7Ce5BL6KsoWJv4_hl8_gKBRjWpUoxq1d ... (310 characters truncated) ... q50xhxy2KypyCmuesSuX5rN6sq_n-drd_9a9J-W_PInem4WM0wZWWc55I3eV78pus34muI1IP55jkbs73hNFa369IPZxjQs33SrR8vyZl7d-oRjWqUY06BfUPhs3JLUffqKQAAAAASUVORK5CYII=', <User 1>)]
The generator function:
def enq(data):
qr = qrcode.make(data)
img_buffer = BytesIO()
qr.save(img_buffer, format='PNG')
byte_data = img_buffer.getvalue()
b64s = base64.urlsafe_b64encode(byte_data)
string = str(b64s, 'UTF-8')
return string
Posting to DB:
if request.method == 'POST':
data = request.form.get('data')
if len(data) < 1:
flash('Data too short!', category='error')
else:
new_qr = Qrdb(qr_b64s=codegen.enq(data), data=data, owner_id=current_user)
db.session.add(new_qr)
db.session.commit
print('Qr added to DB.')
Database model:
class Qrdb(db.Model):
id = db.Column(db.Integer, primary_key=True)
data = db.Column(db.String(10000))
qr_b64s = db.Column(db.String(10000))
date = db.Column(db.DateTime(timezone=True), default=func.now())
owner_id = db.Column(db.Integer, db.ForeignKey('user.id'))
Solution
The line
new_qr = Qrdb(qr_b64s=codegen.enq(data), data=data, owner_id=current_user)
should be
new_qr = Qrdb(qr_b64s=codegen.enq(data), data=data, owner_id=current_user.id)
because Qrdb.owner_id
is an integer column. If you change the Qrdb
model to add a relationship to the User
model then you could pass a User
instance instead of an integer.
Answered By - snakecharmerb
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.