Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
python-dotenv = "*"
psycopg2-binary = "*"
pandas = "*"

[dev-packages]

[requires]
python_version = "3.7"
134 changes: 134 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions module1-introduction-to-sql/buddymove_holidayiq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pandas as pd
import sqlite3
import os

df = pd.read_csv('buddymove_holidayiq.csv')
print(df.head())
print(df.shape)
print(df.isnull().sum())

filepath = os.path.join(os.path.dirname(__file__), "buddymove_holidayiq.sqlite3")

connection = sqlite3.connect(filepath)

df.to_sql("review", connection)
cursor = connection.cursor()
# queries

query = "SELECT * from review"
chars = cursor.execute(query).fetchall()
q1 = len(chars)

print(' # How many rows in the table?')
print(f' {q1} rows in the table.')
Binary file not shown.
33 changes: 33 additions & 0 deletions module1-introduction-to-sql/buddymove_holidayiq_queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sqlite3

# construct a path to wherever your database exists
DB_FILEPATH = "buddymove_holidayiq.sqlite3"
# DB_FILEPATH = os.path.join(os.path.dirname(__file__), "..", "chinook.db")

connection = sqlite3.connect(DB_FILEPATH)

cursor = connection.cursor()

# queries

query = "SELECT * from review"
chars = cursor.execute(query).fetchall()
q1 = len(chars)

print(' # How many rows in the table?')
print(f' {q1} rows in the table.')

query1 = '''
SELECT
(Nature > 100) as nature_count,
(Shopping > 100) as shopping_count
FROM review
WHERE nature_count = 1
AND shopping_count = 1
'''

chars = cursor.execute(query1).fetchall()
q2 = len(chars)

print(' # How many users who reviewed at least 100 Nature in the category also reviewed at least 100 in the Shopping category?')
print(f' {q2} who reviewed at least 100 Nature in the category also reviewed at least 100 in the Shopping category.')
Binary file modified module1-introduction-to-sql/rpg_db.sqlite3
Binary file not shown.
119 changes: 119 additions & 0 deletions module1-introduction-to-sql/sqlwithpython.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# import os
import sqlite3

# construct a path to wherever your database exists
DB_FILEPATH = "rpg_db.sqlite3"
# DB_FILEPATH = os.path.join(os.path.dirname(__file__), "..", "chinook.db")

connection = sqlite3.connect(DB_FILEPATH)

cursor = connection.cursor()

query = "SELECT name FROM charactercreator_character"
chars = cursor.execute(query).fetchall()
q1 = len(chars)

print('\n # How many total characters are there?')
print(f' Total characters: {q1}')

query1 = "SELECT * FROM charactercreator_cleric"
query2 = "SELECT * FROM charactercreator_fighter"
query3 = "SELECT * FROM charactercreator_mage"
query4 = "SELECT * FROM charactercreator_thief"
query5 = "SELECT * FROM charactercreator_necromancer"

chars = cursor.execute(query1).fetchall()
cleric = len(chars)
chars = cursor.execute(query2).fetchall()
fighter = len(chars)
chars = cursor.execute(query3).fetchall()
mage = len(chars)
chars = cursor.execute(query4).fetchall()
thief = len(chars)
chars = cursor.execute(query5).fetchall()
necromancer = len(chars)

print('\n # How many of each specific subclass?')
print(f' Cleric: {cleric}')
print(f' Fighter: {fighter}')
print(f' Mage: {mage}')
print(f' Thief: {thief}')
print(f' Necromancer: {necromancer}')

query6 = "SELECT * FROM charactercreator_character_inventory"
chars = cursor.execute(query6).fetchall()
item = len(chars)

print('\n # How many total Items?')
print(f' Total Items: {item}')

query7 = "SELECT * FROM armory_weapon"
chars = cursor.execute(query7).fetchall()
weapons = len(chars)

print('\n # How many of the Items are weapons? How many are not?')
print(f' Weapon Items: {weapons}')
print(f' Items that are not weapon: {item - weapons}')


query8 = '''
SELECT
cc.name,
count(distinct ai.item_id) as item_count
FROM charactercreator_character as cc
JOIN charactercreator_character_inventory as ci
ON cc.character_id = ci.character_id
JOIN armory_item as ai
on ci.item_id = ai.item_id
GROUP by cc.name
LIMIT 20'''
chars = cursor.execute(query8).fetchall()

print('\n # How many Items does each character have? (Return first 20 rows)')
for i in chars:
print(i)

query9 = '''
SELECT
cc.name,
count(distinct aw.item_ptr_id) as weapon_count
FROM charactercreator_character as cc
JOIN charactercreator_character_inventory as ci
ON cc.character_id = ci.character_id
JOIN armory_weapon as aw
on ci.item_id = aw.item_ptr_id
GROUP by cc.name
LIMIT 20'''

chars = cursor.execute(query9).fetchall()

print('\n # How many Weapons does each character have? (Return first 20 rows)')
for c in chars:
print(c)
# print(f'First 20 rows of weapons each character have are: \n{chars}')

print('\n # On average, how many Items does each Character have?')
print(f' On average, Character have {item/q1:.2f} items.')


query10 = '''

SELECT AVG(weapon_count) as avg_weapons_per_char
FROM (
SELECT
c.character_id
-- ,c."name"
--,inv.*
--,w.*
,count(distinct w.item_ptr_id) as weapon_count
FROM charactercreator_character c
LEFT JOIN charactercreator_character_inventory inv ON c.character_id = inv.character_id
LEFT JOIN armory_weapon w ON inv.item_id = w.item_ptr_id
GROUP BY c.character_id
) subq
'''

chars = cursor.execute(query10).fetchall()

print('\n # On average, how many Weapons does each Character have?')
print(f' On average, Character have {chars[0][0]:.2f} weapons.')
56 changes: 56 additions & 0 deletions module2-sql-for-analysis/elephant_queries_secured.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# app/elephant_queries.py

import os
from dotenv import load_dotenv
import psycopg2

load_dotenv() #> loads contents of the .env file into the script's environment

DB_NAME = os.getenv("DB_NAME")
DB_USER = os.getenv("DB_USER")
DB_PASSWORD = os.getenv("DB_PASSWORD")
DB_HOST = os.getenv("DB_HOST")

connection = psycopg2.connect(dbname=DB_NAME, user=DB_USER, password=DB_PASSWORD, host=DB_HOST)
print("CONNECTION:", connection)

cursor = connection.cursor()
print("CURSOR:", cursor)

cursor.execute('SELECT * from test_table;')
result = cursor.fetchall()

print(result)


query = '''
CREATE TABLE if not exists titanic (
id Serial Primary Key,
Survived int,
Pclass int,
Name varchar,
Sex varchar,
Age int,
Siblings_Spouses_Aboard int,
Parents_Children_Aboard int,
Fare int
);
'''

cursor.execute(query)

# inserting records (single)

insertion_query = """
INSERT INTO titanic
(Survived, Pclass, Name, Sex, Age, Siblings_Spouses_Aboard,
Parents_Children_Aboard, Fare) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
"""
record_to_insert = (0, 3, 'Mr. Owen Harris Brown', 'male', 22, 1, 0, 7.25)
cursor.execute(insertion_query, record_to_insert)

# save the transactions
connection.commit()

cursor.close()
connection.close()
Loading