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
from sqlalchemy import create_engine | |
# create an engine | |
engine = create_engine("sqlite:///sample.db") | |
try: | |
# Establish a connection | |
connection = engine.connect() | |
print('Connection successful!') | |
except Exception as error: |
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
import pandas as pd | |
from sqlalchemy import create_engine | |
engine = create_engine("sqlite:///sample.db") | |
# Option one | |
with engine.connect() as connection: | |
results = connection.execute(""" | |
SELECT column1, column2, column3 | |
FROM table_name |
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
import pandas as pd | |
# Create a cursor object | |
cursor = connection.cursor() | |
# Execute any SQL query | |
results = cursor.execute(""" | |
SELECT table1.column1, table2.column2 | |
FROM table1 | |
JOIN table2 ON table1.primary = table2.foreign |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
# Create a function that converts a digital file into binary | |
def convert_into_binary(file_path): | |
with open(file_path, 'rb') as file: | |
binary = file.read() | |
return binary |
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
import sqlite3 | |
# Create a function to connect to a database with SQLite | |
def sqlite_connect(db_name): | |
"""Connect to a database if exists. Create an instance if otherwise. | |
Args: | |
db_name: The name of the database to connect | |
Returns: |
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
connection = sqlite_connect('sample.db') | |
cursor = connection.cursor() | |
sql_create_table_query = """ | |
CREATE TABLE audio (name TEXT NOT NULL, data BLOB); | |
""" | |
cursor.execute(sql_create_table_query) | |
connection.commit() | |
connection.close() |
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""" |
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 write_to_file(binary_code, file_name): | |
# Convert binary to a proper file and store in memory | |
with open(file_name, 'wb') as file: | |
file.write(binary_code) | |
print(f'Created file with name: {file_name}') |
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 retrieve_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() | |
sql_retrieve_file_query = f"""SELECT * FROM {table_name} WHERE name = ?""" |