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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ venv.bak/

# mypy
.mypy_cache/


11 changes: 11 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.8"
15 changes: 15 additions & 0 deletions module1-introduction-to-sql/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

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

[dev-packages]

[requires]
python_version = "3.8"
148 changes: 148 additions & 0 deletions module1-introduction-to-sql/Pipfile.lock

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

21 changes: 21 additions & 0 deletions module1-introduction-to-sql/elephant_queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

import os
from dotenv import load_dotenv
import psycopg2


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)

cursor = connection.cursor()
print("CONNECTION", connection)

cursor.execute('SELECT * from test_table;')
print("CURSOR", cursor)

result = cursor.fetchall()
print(result)
96 changes: 96 additions & 0 deletions module1-introduction-to-sql/query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Define query
# How many total characters are there
GET_CHARACTERS = """
SELECT COUNT(*) AS Total_Char
FROM charactercreator_character;

"""
# How many of each specific subclass?
GET_CHARACTER_SUBCLASS = """
SELECT COUNT(DISTINCT(name)) AS sub_class
FROM charactercreator_character;

"""
# How many total Items?
GET_TOTAL_ITEMS = """
SELECT COUNT(*) AS Total_Items
FROM armory_item;
"""
# How many of the Items are weapons?
GET_ITEMS_ARE_WEAPONS = """
SELECT COUNT(item_id) AS items_weapons
FROM armory_item AS ai
LEFT JOIN armory_weapon AS aw
ON aw.item_ptr_id = ai.item_id;
"""
# How many are not?
GET_ITEMS_ARE_NOT_WEAPONS = """
SELECT COUNT(*) AS items_not_weapons
FROM armory_item AS ai
INNER JOIN armory_weapon AS aw
ON aw.item_ptr_id != ai.item_id;
"""
# How many Items does each character have? (Return first 20 rows)
GET_ITEMS_PER_CHARACTER = """
SELECT
cc.character_id,
COUNT(DISTINCT ai.item_id) AS item_per_character
FROM charactercreator_character AS cc
LEFT JOIN charactercreator_character_inventory AS ci
ON cc.character_id = ci.character_id
LEFT JOIN armory_item AS ai
ON ci.item_id = ai.item_id
GROUP BY cc.character_id
LIMIT 20

"""
# How many Weapons does each character have? (Return first 20 rows)
GET_ITEMS_PER_CHARACTER = """
SELECT
cc.character_id,
COUNT(DISTINCT aw.item_ptr_id) AS weapon_per_character
FROM charactercreator_character AS cc
LEFT JOIN charactercreator_character_inventory AS ci
ON cc.character_id = ci.character_id
LEFT JOIN armory_weapon AS aw
ON ci.item_id = aw.item_ptr_id
GROUP BY cc.character_id
LIMIT 20

"""
# On average, how many Items does each character have?
GET_ITEMS_PER_CHARACTER = """
SELECT AVG(item_per_character) AS avg_weapon_per_char
FROM (
SELECT
cc.character_id,
COUNT(DISTINCT ai.item_id) AS item_per_character
FROM charactercreator_character AS cc
LEFT JOIN charactercreator_character_inventory AS ci
ON cc.character_id = ci.character_id
LEFT JOIN armory_item AS ai
ON ci.item_id = ai.item_id
GROUP BY cc.character_id

)
"""
# On average, how many Weapons does each character have?
GET_WEAPON_PER_CHARACTER = """
SELECT AVG(weapon_count) AS avg_weapon_per_char
FROM (
SELECT
cc.character_id,
COUNT(DISTINCT aw.item_ptr_id) AS weapon_count
FROM charactercreator_character AS cc
LEFT JOIN charactercreator_character_inventory AS ci
ON cc.character_id = ci.character_id
LEFT JOIN armory_weapon AS aw
ON ci.item_id = aw.item_ptr_id
GROUP BY cc.character_id

) subq
"""

QUERY_LIST = [GET_CHARACTERS, GET_CHARACTER_NAMES, GET_CHARACTER_SUBCLASS,
GET_ITEMS, GET_ITEMS_ARE_NOT_WEAPONS, GET_ITEMS_ARE_WEAPONS,
GET_ITEMS_PER_CHARACTER, GET_SUBCLASS, GET_TOTAL_ITEMS, GET_WEAPON_PER_CHARACTER]
24 changes: 24 additions & 0 deletions module1-introduction-to-sql/rgb_db_assignment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
import sqlite3
from query import *
# connect the databse
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()


if __name__ == "__main__":
# connect to DB
conn = connect_to_db()
# Create Cursor
curs = conn.cursor()
# Execute query
result1 = execute_query(curs, query.GET_CHARACTERS)
print(result1[0])
result2 = execute_query(curs, query.GET_CHARACTER_SUBCLASS)
print(result2[0])
result3 = execute_query(curs, query.GET_WEAPON_PER_CHARACTER)
print(result3[0])
40 changes: 40 additions & 0 deletions module1-introduction-to-sql/rpg_db_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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()

GET_CHARACTERS = """
SELECT *
FROM charactercreator_character;
"""

GET_SUBCLASS = """
SELECT COUNT(*) FROM charactercreator_character;
"""
GET_ITEMS = """
SELECT COUNT(DISTINCT name) FROM charactercreator_character;
"""

if __name__ == '__main__':
conn = connect_to_db()
curs = conn.cursor()
results = execute_query(curs, GET_CHARACTERS)
print(results)
x = len(results)
print(x)

results2 = execute_query(curs, GET_SUBCLASS)
print(results2)
y = len(results2)
print(y)

results3 = execute_query(curs, GET_ITEMS)
print(results3)
z = len(results3)
print(z)


Binary file added module1-introduction-to-sql/test_db.sqlite3
Binary file not shown.
11 changes: 11 additions & 0 deletions module3-nosql-and-document-oriented-databases/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.8"
17 changes: 17 additions & 0 deletions module3-nosql-and-document-oriented-databases/elephant_queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

import psycopg2

DB_NAME = 'DS 23.DB'
DB_USER = 'tlrztomk'
DB_PASSWORD = 'bc7MwOEWu6dNtmiBc8Cn8vbXCMOttZst'
DB_HOST = 'ziggy.db.elephantsql.com'
connection = psycopg2.connect(dbname=DB_NAME, user=DB_USER, password=DB_PASSWORD, host=DB_HOST)

cursor = connection.cursor()
print("CONNECTION", connection)

cursor.execute('SELECT * from test_table;')
print("CURSOR", cursor)

result = cursor.fetchall()
print(result)
Empty file added rpg_db.sqlite3
Empty file.