Hi,
How do i store files into blob datatype into mysql via user input with python?
def read_file(filename):
#Convert digital data to binary format
with open(filename, 'wb') as f:
photo = f.read()
return photo
@app.route('/createProduct', methods=['GET', 'POST'])
def createProduct():
createProductForm = CreateProductForm()
msg = ''
if request.method == 'POST' and 'productName' in request.form and 'productImage' in request.form and 'productModel' in request.form \
and 'productInfo' in request.form and 'productCareandComposition' in request.form \
and 'price' in request.form and 'category' in request.form:
productName = request.form['productName']
productImage = request.form['productImage']
productModel = request.form['productModel']
productInfo = request.form['productInfo']
productCareandComposition = request.form['productCareandComposition']
price = request.form['price']
category = request.form['category']
#Check if item exist
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM products WHERE productName = %s', (productName))
PRODUCT = cursor.fetchone()
if PRODUCT:
msg = 'Item has been added successful !'
else:
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
#products_insert = 'INSERT INTO products (productName, productImage,productModel, productInfo, productCareandComposition, price, category) VALUES (NULL, %s, %s, %s, %s, %s, %d, %s)'
pImage= read_file(productImage)
pModel = read_file(productModel)
print(pImage)
print(pModel)
#Convert data into tuple format
#insert_blob_tuple = (productName, pImage,pModel, productInfo, productCareandComposition, price, category)
cursor.execute('INSERT INTO products VALUES (NULL, %s, %s, %s, %s, %s, %d, %s)', (productName, pImage, pModel, productInfo, productCareandComposition, price, category))
mysql.connection.commit()
msg = 'You have successfully added the product!'
return redirect(url_for('retrieveProducts'))
These are my codes. it doesnt seems to work. There is also no error message given during execution.