Skip to content

Instantly share code, notes, and snippets.

@nbari
Last active February 17, 2020 20:03
Show Gist options
  • Save nbari/6fdfe1614fca4b33a06f709d9e0c34c0 to your computer and use it in GitHub Desktop.
Save nbari/6fdfe1614fca4b33a06f709d9e0c34c0 to your computer and use it in GitHub Desktop.
add a file into a mysql BLOB column
""" Example for adding a file into a blob
CREATE TABLE test (
id INT AUTO_INCREMENT,
data LONGBLOB NOT NULL,
PRIMARY KEY (id)
);
pip install --upgrade --user mysqlclient
"""
import MySQLdb
db = MySQLdb.connect(
host="127.0.0.1",
port=3306,
user="root",
passwd="test",
db="test")
cursor = db.cursor()
data = open('data.test', 'rb').read()
sql = "INSERT INTO test(data) VALUES (_binary %s)"
print(cursor.execute(sql, (data,)))
db.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment