Skip to content

Instantly share code, notes, and snippets.

View BexTuychiev's full-sized avatar
🏠
Working from home

bexgboost BexTuychiev

🏠
Working from home
View GitHub Profile
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:
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
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.
# 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
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:
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()
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"""
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}')
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 = ?"""