Created
August 22, 2020 06:49
-
-
Save BexTuychiev/18c3b282458c2c01afdfbbe3f2493dda to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def insert_file(file_name, db_name, table_name): | |
try: | |
# Establish a connection | |
connection = sqlite_connect(db_name) | |
print(f"Connected to the database `{db_name}`") | |
# Create a cursor object | |
cursor = connection.cursor() | |
sqlite_insert_blob_query = f""" | |
INSERT INTO {table_name} (name, data) VALUES (?, ?) | |
""" | |
# Convert the file into binary | |
binary_file = convert_into_binary(file_name) | |
data_tuple = (file_name, binary_file) | |
# Execute the query | |
cursor.execute(sqlite_insert_blob_query, data_tuple) | |
connection.commit() | |
print('File inserted successfully') | |
cursor.close() | |
except sqlite3.Error as error: | |
print("Failed to insert blob into the table", error) | |
finally: | |
if connection: | |
connection.close() | |
print("Connection closed") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment