Last active
February 17, 2020 20:03
-
-
Save nbari/6fdfe1614fca4b33a06f709d9e0c34c0 to your computer and use it in GitHub Desktop.
add a file into a mysql BLOB column
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" 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