diff --git a/module1-introduction-to-sql/README.md b/module1-introduction-to-sql/README.md index 40497956..52018fca 100644 --- a/module1-introduction-to-sql/README.md +++ b/module1-introduction-to-sql/README.md @@ -51,14 +51,79 @@ randomized, the numeric and boolean fields were left as defaults. Use `sqlite3` to load and write queries to explore the data, and answer the following questions: -- How many total Characters are there? +- How many total Characters are there? 302 + SELECT + count(character_id) +FROM charactercreator_character + - How many of each specific subclass? -- How many total Items? -- How many of the Items are weapons? How many are not? + Cleric = 75 + Fighter = 68 + Mage = 108 + Necromancer = 11 + Thief = 51 + SELECT + COUNT() + FROM charactercreator_cleric + SELECT + COUNT() + FROM charactercreator_fighter + SELECT + COUNT() + FROM charactercreator_mage + SELECT + COUNT() + FROM charactercreator_necromancer + SELECT + COUNT() + FROM charactercreator_thief + +- How many total Items? 211 + SELECT + (select COUNT(item_id) FROM armory_item) + + (select COUNT(item_ptr_id) FROM armory_weapon) + AS Total_Items + +- How many of the Items are weapons? How many are not? + Weapons = 37, Other items = 174 + SELECT + COUNT() + FROM armory_weapon + - How many Items does each character have? (Return first 20 rows) + SELECT + character_id + ,count(item_id) + FROM charactercreator_character_inventory + GROUP BY character_id + LIMIT 20 + - How many Weapons does each character have? (Return first 20 rows) + SELECT character_id, count(item_id) + FROM charactercreator_character_inventory LEFT JOIN armory_weapon + ON item_id = item_ptr_id + GROUP BY character_id + LIMIT 20 + - On average, how many Items does each Character have? + SELECT + character_id + ,COUNT(item_id) as 'No_of_Items' + ,ROUND(AVG(item_id)) + FROM charactercreator_character_inventory + GROUP BY character_id + ORDER BY character_id + LIMIT 20 + - On average, how many Weapons does each character have? + SELECT + character_id + ,count(item_id) + ,round(avg(item_ptr_id)) +FROM charactercreator_character_inventory LEFT JOIN armory_weapon +ON item_id = item_ptr_id +GROUP BY character_id +LIMIT 20 You do not need all the tables - in particular, the `account_*`, `auth_*`, `django_*`, and `socialaccount_*` tables are for the application and do not have diff --git a/module1-introduction-to-sql/buddymove_holidayiq.py b/module1-introduction-to-sql/buddymove_holidayiq.py new file mode 100644 index 00000000..01e25052 --- /dev/null +++ b/module1-introduction-to-sql/buddymove_holidayiq.py @@ -0,0 +1,26 @@ +import pandas as pd +import os.path +import sqlite3 + +file_path = 'https://archive.ics.uci.edu/ml/machine-learning-databases/00476/buddymove_holidayiq.csv' +df = pd.read_csv(file_path) +print(f'DATA FRAME SHAPE : {df.shape}') + +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) +DB_FILEPATH = os.path.join(BASE_DIR, 'buddymove_holidayiq.sqlite3') + +connection = sqlite3.connect(DB_FILEPATH) +print("CONNECTION:", connection) + +cursor = connection.cursor() +print("CURSOR", cursor) + +df.to_sql(name='review', con=connection) + +no_rows = 'SELECT COUNT(*) FROM review' +result_rows = cursor.execute(no_rows).fetchall() +print('No. of Rows :', result_rows) + +reviews = 'SELECT COUNT(Nature), COUNT(Shopping) FROM review WHERE Nature >= 100 AND Shopping >= 100' +result_reviews = cursor.execute(reviews).fetchall() +print('No. of Users Reviewing Nature & Shopping at >= 100 :', result_reviews) \ No newline at end of file diff --git a/module1-introduction-to-sql/buddymove_holidayiq.sqlite3 b/module1-introduction-to-sql/buddymove_holidayiq.sqlite3 new file mode 100644 index 00000000..0814583a Binary files /dev/null and b/module1-introduction-to-sql/buddymove_holidayiq.sqlite3 differ diff --git a/module1-introduction-to-sql/data/rgb_db.sqlite3 b/module1-introduction-to-sql/data/rgb_db.sqlite3 new file mode 100644 index 00000000..e69de29b diff --git a/module1-introduction-to-sql/data/rpg_db.sqlite3 b/module1-introduction-to-sql/data/rpg_db.sqlite3 new file mode 100644 index 00000000..837d7f16 Binary files /dev/null and b/module1-introduction-to-sql/data/rpg_db.sqlite3 differ diff --git a/module1-introduction-to-sql/rpg_queries.py b/module1-introduction-to-sql/rpg_queries.py new file mode 100644 index 00000000..5ee28131 --- /dev/null +++ b/module1-introduction-to-sql/rpg_queries.py @@ -0,0 +1,33 @@ +import os.path +import sqlite3 + +# construct a path to wherever your database exists +# DB_FILEPATH = 'https://github.com/RAV10K1/DS-Unit-3-Sprint-2-SQL-and-Databases/blob/master/module1-introduction-to-sql/rpg_db.sqlite3' +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) +DB_FILEPATH = os.path.join(BASE_DIR, 'rpg_db.sqlite3') + +connection = sqlite3.connect(DB_FILEPATH) +print("CONNECTION:", connection) + +cursor = connection.cursor() +print("CURSOR", cursor) + +total_char = "SELECT count(character_id) FROM charactercreator_character" +no_clerics = "SELECT COUNT() FROM charactercreator_cleric" +no_fighters = "SELECT COUNT() FROM charactercreator_fighter" +no_mages = "SELECT COUNT() FROM charactercreator_mage" +no_necro = "SELECT COUNT() FROM charactercreator_necromancer" +no_thief = "SELECT COUNT() FROM charactercreator_thief" +total_items = "SELECT (select COUNT(item_id) FROM armory_item) + (select COUNT(item_ptr_id) FROM armory_weapon) AS Total_Items" +item_count = "SELECT COUNT() FROM armory_weapon" +char_item = 'SELECT character_id, count(item_id) FROM charactercreator_character_inventory GROUP BY character_id LIMIT 20' +char_wpn = 'SELECT character_id, count(item_id) FROM charactercreator_character_inventory LEFT JOIN armory_weapon ON item_id = item_ptr_id GROUP BY character_id LIMIT 20' +avg_char_wpn = "SELECT character_id, COUNT(item_id) as 'No_of_Items',ROUND(AVG(item_id)) FROM charactercreator_character_inventory GROUP BY character_id ORDER BY character_id LIMIT 20" +avg_char_item = "SELECT character_id, count(item_id), round(avg(item_ptr_id)) FROM charactercreator_character_inventory LEFT JOIN armory_weapon ON item_id = item_ptr_id GROUP BY character_id LIMIT 20" + +queries = [total_char, no_clerics, no_fighters, no_mages, no_necro, no_thief, + total_items, item_count, char_item, char_wpn, avg_char_item, avg_char_wpn] + +for query in queries: + result = cursor.execute(query).fetchall() + print('Result :', result[0]) \ No newline at end of file diff --git a/module2-sql-for-analysis/.gitignore b/module2-sql-for-analysis/.gitignore new file mode 100644 index 00000000..2eea525d --- /dev/null +++ b/module2-sql-for-analysis/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/module2-sql-for-analysis/.vscode/settings.json b/module2-sql-for-analysis/.vscode/settings.json new file mode 100644 index 00000000..00b8b8cc --- /dev/null +++ b/module2-sql-for-analysis/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "C:\\Users\\Ravi\\.virtualenvs\\DS-Unit-3-Sprint-2-SQL-and-Databases-kzZYgH90\\Scripts\\python.exe" +} \ No newline at end of file diff --git a/module2-sql-for-analysis/Pipfile b/module2-sql-for-analysis/Pipfile new file mode 100644 index 00000000..13523987 --- /dev/null +++ b/module2-sql-for-analysis/Pipfile @@ -0,0 +1,16 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] +pylint = "*" + +[packages] +python-dotenv = "*" +psycopg2-binary = "*" +pandas = "*" +sqlalchemy = "*" + +[requires] +python_version = "3.7" diff --git a/module2-sql-for-analysis/Pipfile.lock b/module2-sql-for-analysis/Pipfile.lock new file mode 100644 index 00000000..10d38bec --- /dev/null +++ b/module2-sql-for-analysis/Pipfile.lock @@ -0,0 +1,284 @@ +{ + "_meta": { + "hash": { + "sha256": "d8f3061993f5bc9e04f2243505d55bb4fc8b945e19bca643a5dc1ada5e3e1dd3" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.7" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "numpy": { + "hashes": [ + "sha256:082f8d4dd69b6b688f64f509b91d482362124986d98dc7dc5f5e9f9b9c3bb983", + "sha256:1bc0145999e8cb8aed9d4e65dd8b139adf1919e521177f198529687dbf613065", + "sha256:309cbcfaa103fc9a33ec16d2d62569d541b79f828c382556ff072442226d1968", + "sha256:3673c8b2b29077f1b7b3a848794f8e11f401ba0b71c49fbd26fb40b71788b132", + "sha256:480fdd4dbda4dd6b638d3863da3be82873bba6d32d1fc12ea1b8486ac7b8d129", + "sha256:56ef7f56470c24bb67fb43dae442e946a6ce172f97c69f8d067ff8550cf782ff", + "sha256:5a936fd51049541d86ccdeef2833cc89a18e4d3808fe58a8abeb802665c5af93", + "sha256:5b6885c12784a27e957294b60f97e8b5b4174c7504665333c5e94fbf41ae5d6a", + "sha256:667c07063940e934287993366ad5f56766bc009017b4a0fe91dbd07960d0aba7", + "sha256:7ed448ff4eaffeb01094959b19cbaf998ecdee9ef9932381420d514e446601cd", + "sha256:8343bf67c72e09cfabfab55ad4a43ce3f6bf6e6ced7acf70f45ded9ebb425055", + "sha256:92feb989b47f83ebef246adabc7ff3b9a59ac30601c3f6819f8913458610bdcc", + "sha256:935c27ae2760c21cd7354402546f6be21d3d0c806fffe967f745d5f2de5005a7", + "sha256:aaf42a04b472d12515debc621c31cf16c215e332242e7a9f56403d814c744624", + "sha256:b12e639378c741add21fbffd16ba5ad25c0a1a17cf2b6fe4288feeb65144f35b", + "sha256:b1cca51512299841bf69add3b75361779962f9cee7d9ee3bb446d5982e925b69", + "sha256:b8456987b637232602ceb4d663cb34106f7eb780e247d51a260b84760fd8f491", + "sha256:b9792b0ac0130b277536ab8944e7b754c69560dac0415dd4b2dbd16b902c8954", + "sha256:c9591886fc9cbe5532d5df85cb8e0cc3b44ba8ce4367bd4cf1b93dc19713da72", + "sha256:cf1347450c0b7644ea142712619533553f02ef23f92f781312f6a3553d031fc7", + "sha256:de8b4a9b56255797cbddb93281ed92acbc510fb7b15df3f01bd28f46ebc4edae", + "sha256:e1b1dc0372f530f26a03578ac75d5e51b3868b9b76cd2facba4c9ee0eb252ab1", + "sha256:e45f8e981a0ab47103181773cc0a54e650b2aef8c7b6cd07405d0fa8d869444a", + "sha256:e4f6d3c53911a9d103d8ec9518190e52a8b945bab021745af4939cfc7c0d4a9e", + "sha256:ed8a311493cf5480a2ebc597d1e177231984c818a86875126cfd004241a73c3e", + "sha256:ef71a1d4fd4858596ae80ad1ec76404ad29701f8ca7cdcebc50300178db14dfc" + ], + "version": "==1.19.1" + }, + "pandas": { + "hashes": [ + "sha256:0210f8fe19c2667a3817adb6de2c4fd92b1b78e1975ca60c0efa908e0985cbdb", + "sha256:0227e3a6e3a22c0e283a5041f1e3064d78fbde811217668bb966ed05386d8a7e", + "sha256:0bc440493cf9dc5b36d5d46bbd5508f6547ba68b02a28234cd8e81fdce42744d", + "sha256:16504f915f1ae424052f1e9b7cd2d01786f098fbb00fa4e0f69d42b22952d798", + "sha256:182a5aeae319df391c3df4740bb17d5300dcd78034b17732c12e62e6dd79e4a4", + "sha256:35db623487f00d9392d8af44a24516d6cb9f274afaf73cfcfe180b9c54e007d2", + "sha256:40ec0a7f611a3d00d3c666c4cceb9aa3f5bf9fbd81392948a93663064f527203", + "sha256:47a03bfef80d6812c91ed6fae43f04f2fa80a4e1b82b35aa4d9002e39529e0b8", + "sha256:4b21d46728f8a6be537716035b445e7ef3a75dbd30bd31aa1b251323219d853e", + "sha256:4d1a806252001c5db7caecbe1a26e49a6c23421d85a700960f6ba093112f54a1", + "sha256:60e20a4ab4d4fec253557d0fc9a4e4095c37b664f78c72af24860c8adcd07088", + "sha256:9f61cca5262840ff46ef857d4f5f65679b82188709d0e5e086a9123791f721c8", + "sha256:a15835c8409d5edc50b4af93be3377b5dd3eb53517e7f785060df1f06f6da0e2", + "sha256:b39508562ad0bb3f384b0db24da7d68a2608b9ddc85b1d931ccaaa92d5e45273", + "sha256:ed60848caadeacecefd0b1de81b91beff23960032cded0ac1449242b506a3b3f", + "sha256:fc714895b6de6803ac9f661abb316853d0cd657f5d23985222255ad76ccedc25" + ], + "index": "pypi", + "version": "==1.1.0" + }, + "psycopg2-binary": { + "hashes": [ + "sha256:008da3ab51adc70a5f1cfbbe5db3a22607ab030eb44bcecf517ad11a0c2b3cac", + "sha256:07cf82c870ec2d2ce94d18e70c13323c89f2f2a2628cbf1feee700630be2519a", + "sha256:08507efbe532029adee21b8d4c999170a83760d38249936038bd0602327029b5", + "sha256:107d9be3b614e52a192719c6bf32e8813030020ea1d1215daa86ded9a24d8b04", + "sha256:17a0ea0b0eabf07035e5e0d520dabc7950aeb15a17c6d36128ba99b2721b25b1", + "sha256:3286541b9d85a340ee4ed42732d15fc1bb441dc500c97243a768154ab8505bb5", + "sha256:3939cf75fc89c5e9ed836e228c4a63604dff95ad19aed2bbf71d5d04c15ed5ce", + "sha256:40abc319f7f26c042a11658bf3dd3b0b3bceccf883ec1c565d5c909a90204434", + "sha256:51f7823f1b087d2020d8e8c9e6687473d3d239ba9afc162d9b2ab6e80b53f9f9", + "sha256:6bb2dd006a46a4a4ce95201f836194eb6a1e863f69ee5bab506673e0ca767057", + "sha256:702f09d8f77dc4794651f650828791af82f7c2efd8c91ae79e3d9fe4bb7d4c98", + "sha256:7036ccf715925251fac969f4da9ad37e4b7e211b1e920860148a10c0de963522", + "sha256:7b832d76cc65c092abd9505cc670c4e3421fd136fb6ea5b94efbe4c146572505", + "sha256:8f74e631b67482d504d7e9cf364071fc5d54c28e79a093ff402d5f8f81e23bfa", + "sha256:930315ac53dc65cbf52ab6b6d27422611f5fb461d763c531db229c7e1af6c0b3", + "sha256:96d3038f5bd061401996614f65d27a4ecb62d843eb4f48e212e6d129171a721f", + "sha256:a20299ee0ea2f9cca494396ac472d6e636745652a64a418b39522c120fd0a0a4", + "sha256:a34826d6465c2e2bbe9d0605f944f19d2480589f89863ed5f091943be27c9de4", + "sha256:a69970ee896e21db4c57e398646af9edc71c003bc52a3cc77fb150240fefd266", + "sha256:b9a8b391c2b0321e0cd7ec6b4cfcc3dd6349347bd1207d48bcb752aa6c553a66", + "sha256:ba13346ff6d3eb2dca0b6fa0d8a9d999eff3dcd9b55f3a890f12b0b6362b2b38", + "sha256:bb0608694a91db1e230b4a314e8ed00ad07ed0c518f9a69b83af2717e31291a3", + "sha256:c8830b7d5f16fd79d39b21e3d94f247219036b29b30c8270314c46bf8b732389", + "sha256:cac918cd7c4c498a60f5d2a61d4f0a6091c2c9490d81bc805c963444032d0dab", + "sha256:cc30cb900f42c8a246e2cb76539d9726f407330bc244ca7729c41a44e8d807fb", + "sha256:ccdc6a87f32b491129ada4b87a43b1895cf2c20fdb7f98ad979647506ffc41b6", + "sha256:d1a8b01f6a964fec702d6b6dac1f91f2b9f9fe41b310cbb16c7ef1fac82df06d", + "sha256:e004db88e5a75e5fdab1620fb9f90c9598c2a195a594225ac4ed2a6f1c23e162", + "sha256:eb2f43ae3037f1ef5e19339c41cf56947021ac892f668765cd65f8ab9814192e", + "sha256:fa466306fcf6b39b8a61d003123d442b23707d635a5cb05ac4e1b62cc79105cd" + ], + "index": "pypi", + "version": "==2.8.5" + }, + "python-dateutil": { + "hashes": [ + "sha256:73ebfe9dbf22e832286dafa60473e4cd239f8592f699aa5adaf10050e6e1823c", + "sha256:75bb3f31ea686f1197762692a9ee6a7550b59fc6ca3a1f4b5d7e32fb98e2da2a" + ], + "version": "==2.8.1" + }, + "python-dotenv": { + "hashes": [ + "sha256:8c10c99a1b25d9a68058a1ad6f90381a62ba68230ca93966882a4dbc3bc9c33d", + "sha256:c10863aee750ad720f4f43436565e4c1698798d763b63234fb5021b6c616e423" + ], + "index": "pypi", + "version": "==0.14.0" + }, + "pytz": { + "hashes": [ + "sha256:a494d53b6d39c3c6e44c3bec237336e14305e4f29bbf800b599253057fbb79ed", + "sha256:c35965d010ce31b23eeb663ed3cc8c906275d6be1a34393a1d73a41febf4a048" + ], + "version": "==2020.1" + }, + "six": { + "hashes": [ + "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259", + "sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced" + ], + "version": "==1.15.0" + }, + "sqlalchemy": { + "hashes": [ + "sha256:0942a3a0df3f6131580eddd26d99071b48cfe5aaf3eab2783076fbc5a1c1882e", + "sha256:0ec575db1b54909750332c2e335c2bb11257883914a03bc5a3306a4488ecc772", + "sha256:109581ccc8915001e8037b73c29590e78ce74be49ca0a3630a23831f9e3ed6c7", + "sha256:16593fd748944726540cd20f7e83afec816c2ac96b082e26ae226e8f7e9688cf", + "sha256:427273b08efc16a85aa2b39892817e78e3ed074fcb89b2a51c4979bae7e7ba98", + "sha256:50c4ee32f0e1581828843267d8de35c3298e86ceecd5e9017dc45788be70a864", + "sha256:512a85c3c8c3995cc91af3e90f38f460da5d3cade8dc3a229c8e0879037547c9", + "sha256:57aa843b783179ab72e863512e14bdcba186641daf69e4e3a5761d705dcc35b1", + "sha256:621f58cd921cd71ba6215c42954ffaa8a918eecd8c535d97befa1a8acad986dd", + "sha256:6ac2558631a81b85e7fb7a44e5035347938b0a73f5fdc27a8566777d0792a6a4", + "sha256:716754d0b5490bdcf68e1e4925edc02ac07209883314ad01a137642ddb2056f1", + "sha256:736d41cfebedecc6f159fc4ac0769dc89528a989471dc1d378ba07d29a60ba1c", + "sha256:8619b86cb68b185a778635be5b3e6018623c0761dde4df2f112896424aa27bd8", + "sha256:87fad64529cde4f1914a5b9c383628e1a8f9e3930304c09cf22c2ae118a1280e", + "sha256:89494df7f93b1836cae210c42864b292f9b31eeabca4810193761990dc689cce", + "sha256:8cac7bb373a5f1423e28de3fd5fc8063b9c8ffe8957dc1b1a59cb90453db6da1", + "sha256:8fd452dc3d49b3cc54483e033de6c006c304432e6f84b74d7b2c68afa2569ae5", + "sha256:adad60eea2c4c2a1875eb6305a0b6e61a83163f8e233586a4d6a55221ef984fe", + "sha256:c26f95e7609b821b5f08a72dab929baa0d685406b953efd7c89423a511d5c413", + "sha256:cbe1324ef52ff26ccde2cb84b8593c8bf930069dfc06c1e616f1bfd4e47f48a3", + "sha256:d05c4adae06bd0c7f696ae3ec8d993ed8ffcc4e11a76b1b35a5af8a099bd2284", + "sha256:d98bc827a1293ae767c8f2f18be3bb5151fd37ddcd7da2a5f9581baeeb7a3fa1", + "sha256:da2fb75f64792c1fc64c82313a00c728a7c301efe6a60b7a9fe35b16b4368ce7", + "sha256:e4624d7edb2576cd72bb83636cd71c8ce544d8e272f308bd80885056972ca299", + "sha256:e89e0d9e106f8a9180a4ca92a6adde60c58b1b0299e1b43bd5e0312f535fbf33", + "sha256:f11c2437fb5f812d020932119ba02d9e2bc29a6eca01a055233a8b449e3e1e7d", + "sha256:f57be5673e12763dd400fea568608700a63ce1c6bd5bdbc3cc3a2c5fdb045274", + "sha256:fc728ece3d5c772c196fd338a99798e7efac7a04f9cb6416299a3638ee9a94cd" + ], + "index": "pypi", + "version": "==1.3.18" + } + }, + "develop": { + "astroid": { + "hashes": [ + "sha256:2f4078c2a41bf377eea06d71c9d2ba4eb8f6b1af2135bec27bbbb7d8f12bb703", + "sha256:bc58d83eb610252fd8de6363e39d4f1d0619c894b0ed24603b881c02e64c7386" + ], + "version": "==2.4.2" + }, + "colorama": { + "hashes": [ + "sha256:7d73d2a99753107a36ac6b455ee49046802e59d9d076ef8e47b61499fa29afff", + "sha256:e96da0d330793e2cb9485e9ddfd918d456036c7149416295932478192f4436a1" + ], + "markers": "sys_platform == 'win32'", + "version": "==0.4.3" + }, + "isort": { + "hashes": [ + "sha256:54da7e92468955c4fceacd0c86bd0ec997b0e1ee80d97f67c35a78b719dccab1", + "sha256:6e811fcb295968434526407adb8796944f1988c5b65e8139058f2014cbe100fd" + ], + "version": "==4.3.21" + }, + "lazy-object-proxy": { + "hashes": [ + "sha256:0c4b206227a8097f05c4dbdd323c50edf81f15db3b8dc064d08c62d37e1a504d", + "sha256:194d092e6f246b906e8f70884e620e459fc54db3259e60cf69a4d66c3fda3449", + "sha256:1be7e4c9f96948003609aa6c974ae59830a6baecc5376c25c92d7d697e684c08", + "sha256:4677f594e474c91da97f489fea5b7daa17b5517190899cf213697e48d3902f5a", + "sha256:48dab84ebd4831077b150572aec802f303117c8cc5c871e182447281ebf3ac50", + "sha256:5541cada25cd173702dbd99f8e22434105456314462326f06dba3e180f203dfd", + "sha256:59f79fef100b09564bc2df42ea2d8d21a64fdcda64979c0fa3db7bdaabaf6239", + "sha256:8d859b89baf8ef7f8bc6b00aa20316483d67f0b1cbf422f5b4dc56701c8f2ffb", + "sha256:9254f4358b9b541e3441b007a0ea0764b9d056afdeafc1a5569eee1cc6c1b9ea", + "sha256:9651375199045a358eb6741df3e02a651e0330be090b3bc79f6d0de31a80ec3e", + "sha256:97bb5884f6f1cdce0099f86b907aa41c970c3c672ac8b9c8352789e103cf3156", + "sha256:9b15f3f4c0f35727d3a0fba4b770b3c4ebbb1fa907dbcc046a1d2799f3edd142", + "sha256:a2238e9d1bb71a56cd710611a1614d1194dc10a175c1e08d75e1a7bcc250d442", + "sha256:a6ae12d08c0bf9909ce12385803a543bfe99b95fe01e752536a60af2b7797c62", + "sha256:ca0a928a3ddbc5725be2dd1cf895ec0a254798915fb3a36af0964a0a4149e3db", + "sha256:cb2c7c57005a6804ab66f106ceb8482da55f5314b7fcb06551db1edae4ad1531", + "sha256:d74bb8693bf9cf75ac3b47a54d716bbb1a92648d5f781fc799347cfc95952383", + "sha256:d945239a5639b3ff35b70a88c5f2f491913eb94871780ebfabb2568bd58afc5a", + "sha256:eba7011090323c1dadf18b3b689845fd96a61ba0a1dfbd7f24b921398affc357", + "sha256:efa1909120ce98bbb3777e8b6f92237f5d5c8ea6758efea36a473e1d38f7d3e4", + "sha256:f3900e8a5de27447acbf900b4750b0ddfd7ec1ea7fbaf11dfa911141bc522af0" + ], + "version": "==1.4.3" + }, + "mccabe": { + "hashes": [ + "sha256:ab8a6258860da4b6677da4bd2fe5dc2c659cff31b3ee4f7f5d64e79735b80d42", + "sha256:dd8d182285a0fe56bace7f45b5e7d1a6ebcbf524e8f3bd87eb0f125271b8831f" + ], + "version": "==0.6.1" + }, + "pylint": { + "hashes": [ + "sha256:7dd78437f2d8d019717dbf287772d0b2dbdfd13fc016aa7faa08d67bccc46adc", + "sha256:d0ece7d223fe422088b0e8f13fa0a1e8eb745ebffcb8ed53d3e95394b6101a1c" + ], + "index": "pypi", + "version": "==2.5.3" + }, + "six": { + "hashes": [ + "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259", + "sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced" + ], + "version": "==1.15.0" + }, + "toml": { + "hashes": [ + "sha256:926b612be1e5ce0634a2ca03470f95169cf16f939018233a670519cb4ac58b0f", + "sha256:bda89d5935c2eac546d648028b9901107a595863cb36bae0c73ac804a9b4ce88" + ], + "version": "==0.10.1" + }, + "typed-ast": { + "hashes": [ + "sha256:0666aa36131496aed8f7be0410ff974562ab7eeac11ef351def9ea6fa28f6355", + "sha256:0c2c07682d61a629b68433afb159376e24e5b2fd4641d35424e462169c0a7919", + "sha256:249862707802d40f7f29f6e1aad8d84b5aa9e44552d2cc17384b209f091276aa", + "sha256:24995c843eb0ad11a4527b026b4dde3da70e1f2d8806c99b7b4a7cf491612652", + "sha256:269151951236b0f9a6f04015a9004084a5ab0d5f19b57de779f908621e7d8b75", + "sha256:4083861b0aa07990b619bd7ddc365eb7fa4b817e99cf5f8d9cf21a42780f6e01", + "sha256:498b0f36cc7054c1fead3d7fc59d2150f4d5c6c56ba7fb150c013fbc683a8d2d", + "sha256:4e3e5da80ccbebfff202a67bf900d081906c358ccc3d5e3c8aea42fdfdfd51c1", + "sha256:6daac9731f172c2a22ade6ed0c00197ee7cc1221aa84cfdf9c31defeb059a907", + "sha256:715ff2f2df46121071622063fc7543d9b1fd19ebfc4f5c8895af64a77a8c852c", + "sha256:73d785a950fc82dd2a25897d525d003f6378d1cb23ab305578394694202a58c3", + "sha256:8c8aaad94455178e3187ab22c8b01a3837f8ee50e09cf31f1ba129eb293ec30b", + "sha256:8ce678dbaf790dbdb3eba24056d5364fb45944f33553dd5869b7580cdbb83614", + "sha256:aaee9905aee35ba5905cfb3c62f3e83b3bec7b39413f0a7f19be4e547ea01ebb", + "sha256:bcd3b13b56ea479b3650b82cabd6b5343a625b0ced5429e4ccad28a8973f301b", + "sha256:c9e348e02e4d2b4a8b2eedb48210430658df6951fa484e59de33ff773fbd4b41", + "sha256:d205b1b46085271b4e15f670058ce182bd1199e56b317bf2ec004b6a44f911f6", + "sha256:d43943ef777f9a1c42bf4e552ba23ac77a6351de620aa9acf64ad54933ad4d34", + "sha256:d5d33e9e7af3b34a40dc05f498939f0ebf187f07c385fd58d591c533ad8562fe", + "sha256:fc0fea399acb12edbf8a628ba8d2312f583bdbdb3335635db062fa98cf71fca4", + "sha256:fe460b922ec15dd205595c9b5b99e2f056fd98ae8f9f56b888e7a17dc2b757e7" + ], + "markers": "implementation_name == 'cpython' and python_version < '3.8'", + "version": "==1.4.1" + }, + "wrapt": { + "hashes": [ + "sha256:b62ffa81fb85f4332a4f609cab4ac40709470da05643a082ec1eb88e6d9b97d7" + ], + "version": "==1.12.1" + } + } +} diff --git a/module2-sql-for-analysis/elephant_queries.py b/module2-sql-for-analysis/elephant_queries.py new file mode 100644 index 00000000..6dc45fe2 --- /dev/null +++ b/module2-sql-for-analysis/elephant_queries.py @@ -0,0 +1,91 @@ +import os +import psycopg2 +from psycopg2.extras import execute_values +import json +import pandas as pd +from psycopg2.extras import DictCursor +from dotenv import load_dotenv + +load_dotenv() + +DB_NAME = os.getenv('DB_NAME', default='Check env variables') +DB_USER = os.getenv('DB_USER', default='Check env variables') +DB_PASSWORD = os.getenv('DB_PASSWORD', default='Check env variables') +DB_HOST = os.getenv('DB_HOST', default='Check env variables') + +connection = psycopg2.connect(dbname = DB_NAME, user = DB_USER, + password = DB_PASSWORD, host = DB_HOST) +print("CONNECTION:", connection) + +cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) +print("CURSOR:", cursor) + +cursor.execute('SELECT * from test_table;') +result = cursor.fetchall() +print("RESULT:", type(result)) +print(result) + + +table_name = "test_table2" + +print("-------------------") +query = f""" +CREATE TABLE IF NOT EXISTS {table_name} ( + id SERIAL PRIMARY KEY, + name varchar(40) NOT NULL, + data JSONB +); +""" +print("SQL:", query) +cursor.execute(query) + +# +# INSERT SOME DATA +# + +my_dict = { "a": 1, "b": ["dog", "cat", 42], "c": 'true' } + +#insertion_query = f"INSERT INTO {table_name} (name, data) VALUES (%s, %s)" +#cursor.execute(insertion_query, +# ('A rowwwww', 'null') +#) +#cursor.execute(insertion_query, +# ('Another row, with JSONNNNN', json.dumps(my_dict)) +#) + +# h/t: https://stackoverflow.com/questions/8134602/psycopg2-insert-multiple-rows-with-one-query +insertion_query = f"INSERT INTO {table_name} (name, data) VALUES %s" +#execute_values(cursor, insertion_query, [ +# ('A rowwwww', 'null'), +# ('Another row, with JSONNNNN', json.dumps(my_dict)), +# ('Third row', "3") +#]) + +df = pd.DataFrame([ + ['A rowwwww', 'null'], + ['Another row, with JSONNNNN', json.dumps(my_dict)], + ['Third row', "null"], + ["Pandas Row", "null"] +]) + +records = df.to_dict("records") #> [{0: 'A rowwwww', 1: 'null'}, {0: 'Another row, with JSONNNNN', 1: '{"a": 1, "b": ["dog", "cat", 42], "c": "true"}'}, {0: 'Third row', 1: '3'}, {0: 'Pandas Row', 1: 'YOOO!'}] +list_of_tuples = [(r[0], r[1]) for r in records] + +execute_values(cursor, insertion_query, list_of_tuples) + +# +# QUERY THE TABLE +# + +print("-------------------") +query = f"SELECT * FROM {table_name};" +print("SQL:", query) +cursor.execute(query) +for row in cursor.fetchall(): + print(row) + +# ACTUALLY SAVE THE TRANSACTIONS +connection.commit() + +cursor.close() +connection.close() \ No newline at end of file diff --git a/module2-sql-for-analysis/insert_titanic.py b/module2-sql-for-analysis/insert_titanic.py new file mode 100644 index 00000000..0e180692 --- /dev/null +++ b/module2-sql-for-analysis/insert_titanic.py @@ -0,0 +1,31 @@ +import psycopg2 +from sqlalchemy import create_engine +import pandas as pd +import os + +from dotenv import load_dotenv + +load_dotenv() + +# Loading csv to pandas dataframe +file_path = os.path.join(os.path.dirname(__file__), 'titanic.csv') +df = pd.read_csv(file_path) +print(df.shape) +print(df.head()) + +# Establishing variables to PostgreSQL + +DB_NAME = os.getenv('DB_NAME2', default='Check env variables') +DB_USER = os.getenv('DB_USER2', default='Check env variables') +DB_PASSWORD = os.getenv('DB_PASSWORD2', default='Check env variables') +DB_HOST = os.getenv('DB_HOST2', default='Check env variables') + +sql_url = f'postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}/{DB_NAME}' + +engine = create_engine(sql_url) + +print('ENGINE :', engine) + +# Inserting DF data to table + +df.to_sql('titanic', engine, if_exists='replace') diff --git a/module2-sql-for-analysis/rpg_db.sqlite3 b/module2-sql-for-analysis/rpg_db.sqlite3 new file mode 100644 index 00000000..837d7f16 Binary files /dev/null and b/module2-sql-for-analysis/rpg_db.sqlite3 differ diff --git a/module2-sql-for-analysis/rpg_queries.py b/module2-sql-for-analysis/rpg_queries.py new file mode 100644 index 00000000..1354ae5c --- /dev/null +++ b/module2-sql-for-analysis/rpg_queries.py @@ -0,0 +1,63 @@ +import os +import psycopg2 +import sqlite3 +from psycopg2.extras import execute_values +import json +import pandas as pd +from psycopg2.extras import DictCursor +from dotenv import load_dotenv + +## Setting up PostgreSQL Connection + +load_dotenv() + +DB_NAME = os.getenv('DB_NAME1', default='Check env variables') +DB_USER = os.getenv('DB_USER1', default='Check env variables') +DB_PASSWORD = os.getenv('DB_PASSWORD1', default='Check env variables') +DB_HOST = os.getenv('DB_HOST1', default='Check env variables') + +connection = psycopg2.connect(dbname = DB_NAME, user = DB_USER, + password = DB_PASSWORD, host = DB_HOST) +print("CONNECTION:", connection) + +cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) +print("CURSOR:", cursor) + +create_table = """CREATE TABLE IF NOT EXISTS rpg_data (character_id SERIAL PRIMARY KEY, +name varchar(30) NOT NULL, +level int, +exp int, +hp int, +strength int, +intelligence int, +dexterity int, +wisdom int); +""" + +table_query = "SELECT * FROM rpg_data" + +cursor.execute(create_table) +cursor.execute(table_query) +connection.commit() +result = cursor.fetchall() +print("RESULT:", type(result)) +# print(result) + +# Connecting to SQLite3 DB for RPG Data + +sl_conn = sqlite3.connect('rpg_db.sqlite3') +sl_cursor = sl_conn.cursor() +characters = sl_conn.execute('SELECT * FROM charactercreator_character').fetchall() +print(characters) + +## Inserting SQLite data into PostgreSQL DB + +for character in characters: + insert_query_pg = f"""INSERT INTO rpg_data (character_id, name, level, exp, hp, strength, intelligence, dexterity, wisdom) VALUES + {character}""" + + print(insert_query_pg) + + cursor.execute(insert_query_pg) + +connection.commit() \ No newline at end of file diff --git a/module3-nosql-and-document-oriented-databases/mongo_queries.py b/module3-nosql-and-document-oriented-databases/mongo_queries.py new file mode 100644 index 00000000..a96517dc --- /dev/null +++ b/module3-nosql-and-document-oriented-databases/mongo_queries.py @@ -0,0 +1,39 @@ +import pymongo +import os +from dotenv import load_dotenv + +load_dotenv() + +DB_USER = os.getenv("MONGO_USER", default="OOPS") +DB_PASSWORD = os.getenv("MONGO_PASSWORD", default="OOPS") +CLUSTER_NAME = os.getenv("MONGO_CLUSTER_NAME", default="OOPS") + + +connection_uri = f"mongodb+srv://{DB_USER}:{DB_PASSWORD}@{CLUSTER_NAME}.mongodb.net/test?retryWrites=true&w=majority" +print("----------------") +print("URI:", connection_uri) + +client = pymongo.MongoClient(connection_uri) +print("----------------") +print("CLIENT:", type(client), client) + +db = client.test_database # "test_database" or whatever you want to call it +print("----------------") +print("DB:", type(db), db) + +collection = db.pokemon_test # "pokemon_test" or whatever you want to call it +print("----------------") +print("COLLECTION:", type(collection), collection) + +print("----------------") +print("COLLECTIONS:") +print(db.list_collection_names()) + +collection.insert_one({ + "name": "Pikachu", + "level": 30, + "exp": 76000000000, + "hp": 400, +}) +print("DOCS:", collection.count_documents({})) +print(collection.count_documents({"name": "Pikachu"})) diff --git a/module3-nosql-and-document-oriented-databases/rpg_db.sqlite3 b/module3-nosql-and-document-oriented-databases/rpg_db.sqlite3 new file mode 100644 index 00000000..837d7f16 Binary files /dev/null and b/module3-nosql-and-document-oriented-databases/rpg_db.sqlite3 differ