Skip to content

Instantly share code, notes, and snippets.

@BexTuychiev
Created August 22, 2020 06:49
Show Gist options
  • Save BexTuychiev/18c3b282458c2c01afdfbbe3f2493dda to your computer and use it in GitHub Desktop.
Save BexTuychiev/18c3b282458c2c01afdfbbe3f2493dda to your computer and use it in GitHub Desktop.
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