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
15 changes: 15 additions & 0 deletions module1-introduction-to-sql/buddymove_holidayiq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import sqlite3
import pandas as pd

connect = sqlite3.connect('buddymove_holidayiq.sqlite3')
curs = connect.cursor()

df = pd.read_csv('buddymove_holidayiq.csv')

df.to_sql('buddy', connect)

curs.execute('SELECT COUNT(*) FROM buddy')
print(f'There are {curs.fetchall()[0][0]} rows in the database.')

curs.execute('SELECT COUNT(*) FROM buddy WHERE Nature > 99 AND Shopping > 99')
print(f'{curs.fetchall()[0][0]} users reviewed at least 100 in both Nature and Shopping.')
Binary file not shown.
132 changes: 132 additions & 0 deletions module1-introduction-to-sql/rpg_queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import sqlite3


def connect_to_db(db_name='rpg_db.sqlite3'):
return sqlite3.connect(db_name)

def execute_query(cursor, query):
cursor.execute(query)
return cursor.fetchall()


CHARACTER_COUNT = """
SELECT COUNT(character_id)
FROM charactercreator_character;
"""

CLERIC_COUNT = """
SELECT COUNT(character_ptr_id)
FROM charactercreator_cleric;
"""

FIGHTER_COUNT = """
SELECT COUNT(character_ptr_id)
FROM charactercreator_fighter;
"""

MAGE_COUNT = """
SELECT COUNT(character_ptr_id)
FROM charactercreator_mage;
"""

NECRO_COUNT = """
SELECT COUNT(mage_ptr_id)
FROM charactercreator_necromancer;
"""

THIEF_COUNT = """
SELECT COUNT(character_ptr_id)
FROM charactercreator_thief;
"""

ITEM_COUNT = """
SELECT COUNT(item_id)
FROM armory_item;
"""

WEAPON_COUNT = """
SELECT COUNT(DISTINCT item_id)
FROM armory_item, armory_weapon
WHERE item_id = item_ptr_id;
"""

CHAR_INV_COUNT = """
SELECT COUNT(item_id)
FROM charactercreator_character_inventory
GROUP BY character_id
LIMIT 20;
"""

CHAR_WEAP_COUNT = """
SELECT COUNT(item_id)
FROM charactercreator_character_inventory
WHERE item_id > 137
GROUP BY character_id
LIMIT 20;
"""

CHAR_INV_AVG = """
SELECT AVG(items.avg_count) FROM
(SELECT COUNT(item_id) AS avg_count
FROM charactercreator_character_inventory
GROUP BY character_id) AS items;
"""

CHAR_WEAP_AVG = """
SELECT AVG(weapons.avg_count) FROM
(SELECT COUNT(item_id) AS avg_count
FROM charactercreator_character_inventory
WHERE item_id > 137
GROUP BY character_id) AS weapons;
"""


if __name__ == '__main__':
conn = connect_to_db()
curs = conn.cursor()

results1 = execute_query(curs, CHARACTER_COUNT)[0][0]
print('How many total characters are there?')
print(f'There are {results1} characters.')
print('***************')

total_cleric = execute_query(curs, CLERIC_COUNT)[0][0]
total_fight = execute_query(curs, FIGHTER_COUNT)[0][0]
total_mage = execute_query(curs, MAGE_COUNT)[0][0]
total_necro = execute_query(curs, NECRO_COUNT)[0][0]
total_thief = execute_query(curs, THIEF_COUNT)[0][0]
print('How many of each specific subclass?')
print(f'Breakdown of classes: {total_cleric} clerics, {total_fight} fighters, {total_thief} thieves, and {total_mage} mages, {total_necro} of which are necromancers.')
print('***************')

total_items = execute_query(curs, ITEM_COUNT)[0][0]
print('How many total items?')
print(f'There are {total_items} items in the game.')
print('***************')

total_weap = execute_query(curs, WEAPON_COUNT)[0][0]
print ('How many of the items are weapons? How many are not?')
print(f'Of these, {total_weap} are weapons and {total_items - total_weap} are not.')
print('***************')

char_items = execute_query(curs, CHAR_INV_COUNT)[0:20]
print('How many Items does each character have? (Return first 20 rows)')
print('Number of items in the top 20 inventories:')
print(char_items)
print('***************')

char_weapons = execute_query(curs, CHAR_WEAP_COUNT)[0:20]
print('How many Weapons does each character have? (Return first 20 rows)')
print('And these are the number of weapons within them:')
print(char_weapons)
print('***************')

char_avg_items = execute_query(curs, CHAR_INV_AVG)[0][0]
print('On average, how many Items does each Character have?')
print(f'The average number of items carried by players is {char_avg_items}')
print('***************')

char_avg_weaps = execute_query(curs, CHAR_WEAP_AVG)[0][0]
print('On average, how many Weapons does each Character have?')
print(f'The average number of weapons carried by players is {char_avg_weaps}')
print('***************')
13 changes: 13 additions & 0 deletions module2-sql-for-analysis/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

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

[requires]
python_version = "3.8"
65 changes: 65 additions & 0 deletions module2-sql-for-analysis/Pipfile.lock

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

67 changes: 67 additions & 0 deletions module2-sql-for-analysis/elephant_queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import os
import psycopg2
from dotenv import load_dotenv

load_dotenv()

DB_NAME = os.getenv('DB_NAME')
DB_USER = os.getenv('DB_USER')
DB_PASS = os.getenv('DB_PASS')
DB_HOST = os.getenv('DB_HOST')

### Connect to ElephantSQL-hosted PostgreSQL
conn = psycopg2.connect(dbname=DB_NAME,
user=DB_USER,
password=DB_PASS,
host=DB_HOST)

### A "cursor", a structure to iterate over db records to perform queries
cursor = conn.cursor()

### An example query
cursor.execute('SELECT * from test_table;')

### Note - nothing happened yet! We need to actually *fetch* from the cursor
results = cursor.fetchone()
# print(results)


################ Connect to SQLite3 DB for RPG data #####################

import sqlite3

sl_conn = sqlite3.connect('rpg_db.sqlite3')
sl_cursor = sl_conn.cursor()
characters = sl_cursor.execute('SELECT * FROM charactercreator_character LIMIT 10').fetchall()
print(characters)

################# Create Character Table in PostGRES ####################

create_character_table_query = '''
CREATE TABLE IF NOT EXISTS rpg_characters (
character_id SERIAL PRIMARY KEY,
name VARCHAR(30),
level INT,
exp INT,
hp INT,
strength INT,
intelligence INT,
dexterity INT,
wisdom INT
)
'''

cursor.execute(create_character_table_query)
conn.commit()

################# Insert Character Data in PostGRES ####################

for character in characters:

insert_query = f''' INSERT INTO rpg_characters
(character_id, name, level, exp, hp, strength, intelligence, dexterity, wisdom) VALUES
{character}
'''
cursor.execute(insert_query)

conn.commit()
66 changes: 66 additions & 0 deletions module2-sql-for-analysis/elephant_queries_DS17.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os
import psycopg2
from dotenv import load_dotenv
import sqlite3

load_dotenv()

# Variables saved in .env file
DB_NAME = os.getenv('DB_NAME')
DB_USER = os.getenv('DB_USER')
DB_PASS = os.getenv('DB_PASS')
DB_HOST = os.getenv('DB_HOST')

# Connect to ElephantSQL-hosted PostgreSQL and instantiate cursor
conn = psycopg2.connect(dbname=DB_NAME,
user=DB_USER,
password=DB_PASS,
host=DB_HOST)
cursor = conn.cursor()

# An example query
# Note - nothing happened yet! We need to actually *fetch* from the cursor
cursor.execute('SELECT * from test_table;')
results = cursor.fetchone()


# Connect to SQLite3 DB for RPG data
sl_conn = sqlite3.connect('rpg_db.sqlite3')
sl_cursor = sl_conn.cursor()
characters = sl_cursor.execute('SELECT * FROM charactercreator_character LIMIT 10').fetchall()
print(characters)

# Create Character Table in PostGRES

create_character_table_query = '''
CREATE TABLE IF NOT EXISTS rpg_characters (
character_id SERIAL PRIMARY KEY,
name VARCHAR(30),
level INT,
exp INT,
hp INT,
strength INT,
intelligence INT,
dexterity INT,
wisdom INT
)
'''

cursor.execute(create_character_table_query)
conn.commit()

# Insert Character Data in PostGRES

for character in characters:
insert_query = f''' INSERT INTO rpg_characters
(character_id, name, level, exp, hp, strength, intelligence, dexterity, wisdom) VALUES
{character}
'''
cursor.execute(insert_query)

# Commit and close out connection and cursors
conn.commit()
sl_cursor.close()
sl_conn.close()
cursor.close()
conn.close()
Loading