-
Notifications
You must be signed in to change notification settings - Fork 22
Pokedex codes for mysql activity #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| drop database IF EXISTS classicmodels; | ||
| drop database IF EXISTS pokedex ; | ||
| create database pokedex; | ||
| use pokedex; | ||
| CREATE TABLE pokemon ( | ||
| pokemon_id INT PRIMARY KEY NOT NULL, | ||
| name VARCHAR(255) NOT NULL | ||
| -- color VARCHAR(255), | ||
| -- type VARCHAR(255), | ||
| -- type2 VARCHAR(255), | ||
| -- weakness VARCHAR(255) | ||
| ); | ||
| CREATE TABLE types ( | ||
| type_id INT PRIMARY KEY NOT NULL auto_increment, | ||
| type VARCHAR(255) NOT NULL | ||
| -- FOREIGN KEY (type_id) REFERENCES pokemon_type(type_id) | ||
| ); | ||
| CREATE TABLE pokemon_type ( | ||
| pokemon_type_id INT PRIMARY KEY NOT NULL auto_increment, | ||
| type_id INT NOT NULL, | ||
| pokemon_id INT NOT NULL, | ||
| FOREIGN KEY (pokemon_id) REFERENCES pokemon(pokemon_id), | ||
| FOREIGN KEY (type_id) REFERENCES types(type_id) | ||
| ); | ||
| ALTER TABLE pokemon_type ADD FOREIGN KEY(pokemon_id) REFERENCES pokemon(pokemon_id); | ||
| ALTER TABLE pokemon_type ADD FOREIGN KEY(type_id) REFERENCES types(type_id); | ||
|
|
||
|
|
||
| INSERT INTO pokemon (pokemon_id, name) | ||
| VALUES | ||
| (1,'bulbasaur'), | ||
| (2, 'ivysaur'), | ||
| (3, 'venusaur'), | ||
| (4, 'charmander'), | ||
| (5, 'charmeleon'), | ||
| (6, 'charizard'), | ||
| (7, 'squirtle'), | ||
| (8, 'wartortle'), | ||
| (9, 'blastoise'), | ||
| (10, 'caterpie'); | ||
|
|
||
| INSERT INTO types(type) | ||
| VALUES | ||
| ('fire'), | ||
| ('bug'), | ||
| ('grass'), | ||
| ('water'), | ||
| ('normal'), | ||
| ('electric'), | ||
| ('poison'), | ||
| ('ground'), | ||
| ('fairy'), | ||
| ('psychic'), | ||
| ('rock'), | ||
| ('fighting'), | ||
| ('ghost'), | ||
| ('ice'), | ||
| ('dragon'), | ||
| ('steel'), | ||
| ('dark'), | ||
| ('flying'); | ||
|
|
||
|
|
||
| INSERT INTO pokemon_type(type_id, pokemon_id) | ||
| VALUES | ||
| (3, 1), | ||
| (3, 2), | ||
| (3, 3), | ||
| (1, 4), | ||
| (1, 5), | ||
| (1, 6), | ||
| (4, 7), | ||
| (4, 8), | ||
| (4, 9), | ||
| (2, 10); | ||
|
|
||
| -- SELECT p.name, tp.type, pt.pokemon_type_id | ||
| -- FROM pokemon p | ||
| -- LEFT JOIN pokemon_type pt | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its best to delete code instead of committing commenting out code |
||
| -- ON p.pokemon_id = pt.pokemon_id | ||
| -- INNER JOIN types tp | ||
| -- ON tp.type_id = pt.type_id; | ||
|
|
||
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,44 @@ | ||
| const express = require('express') | ||
| const mysql = require("mysql"); | ||
| const cors = require("cors"); | ||
| const app = express() | ||
| const port = 3000 | ||
|
|
||
| app.use(cors()) | ||
|
|
||
| app.get('/', (req, res) => { | ||
| res.send('Hello World!') | ||
| }) | ||
| app.get('/pokemon', async (req, res) => { | ||
| let customerData = await getCustomerData(); | ||
| res.send(customerData); | ||
| }) | ||
|
|
||
| app.listen(port, () => { | ||
| console.log (`Example app listening at http://localhost:${port}`) | ||
| }) | ||
|
|
||
|
|
||
| async function getCustomerData(){ | ||
|
|
||
| let con = mysql.createConnection ({ | ||
|
|
||
| password: "Pakistan@777", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure you change your password haha |
||
| user: "root", | ||
| database: "pokedex", | ||
| host: "localhost", | ||
| }); | ||
|
|
||
| let data = await new Promise ((resolve, reject) => { | ||
| con.query ("SELECT * FROM pokemon", (err, result) => { | ||
| (err) ? reject(err) : resolve(result); | ||
|
|
||
| }) | ||
|
|
||
| }) | ||
| con.end(); | ||
|
|
||
| return data; | ||
|
|
||
| } | ||
|
|
||
| app.listen(port, () => { | ||
| console.log(`Example app listening at http://localhost:${port}`) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,5 +7,7 @@ | |
| </head> | ||
| <body> | ||
| <p>Hello Pokemon!</p> | ||
| <div id="customers"></div> | ||
| <script src="script.js"></script> | ||
| </body> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
|
|
||
| fetch("http://localhost:3000/pokemon") | ||
| .then(response =>response.json()) | ||
| .then(customers => { | ||
| document.getElementById("customers").innerHTML = customers.map(customer => `<div>${customer.pokemon_id}: ${customer.name}</div>`).join("") | ||
| }); | ||
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing image src