00:00:00 – Introduction
00:00:51 – Databases
00:01:53 – SQL
00:02:35 – SQL Data Types
00:04:26 – create.sql
00:09:27 – Constraints
00:11:07 – INSERT
00:13:39 – insert.sql
00:14:06 – SELECT
00:25:37 – SQL Functions
00:26:21 – UPDATE
00:28:37 – DELETE
00:36:57 – Foreign Keys
00:42:48 – joins.sql
00:50:15 – Indexing
00:56:30 – SQL Injection
01:02:57 – Race Conditions
01:08:10 – SQL Transactions
01:09:08 – SQLAlchemy
01:09:45 – list.py
01:15:42 – import.py
01:22:05 – passengers.py
01:25:19 – airline0
01:38:59 – airline1
Original source
33 responses to “SQL – Lecture 3 – CS50's Web Programming with Python and JavaScript”
1:20:35 Brian Yu: good question!… what do you mean?
what a good-question man🤣🤣🤣
Struggling with import.py I got this: [SQL: INSERT INTO books (isbn, title, author, year) VALUES (%(isbn)s, %(title)s, %(author)s, %(year)s )]
[parameters: {'isbn': '0380795272', 'title': 'Krondor: The Betrayal', 'author': 'Raymond E. Feist', 'year': '1998'}]
I want to run postgresql on vscode. Does anyone know how to do that???
How does one get postgreSQL on vscode?
8:35 I think he meant URI, not URL.
I don't like coding, but Brian always urges me into that. Very motivating!!!
If you are stuck with
engine = create_engine(os.getenv("DATABASE_URL"))
remove "os.getenv()" and replace "DATABASE_URL" with a string of your heroku URI
Hey guys, Im stacked here with an error
db.execute gives : Instance of 'scoped_session' has no 'execute' member
Does anybody have an idea how to go about that, please ?
db.execute("INSERT INTO BOOKS (isbn,title,author,year) VALUES (:isbn,:title,:author,:year)",{"isbn":isbn,"title":title,"author":author,"year":year})
ERROR–invalid input syntax for integer: "isbn"
LINE 1: …NSERT INTO BOOKS (isbn,title,author,year) VALUES ('isbn','ti…
how to set DATABASE_URL ?
## My Notes to this lecture
– DataTypes
* INTEGER
* DECIMAL
* SERIAL
* VARCHAR
* TIMESTAMP
* BOOLEAN
* ENUM
– LOGIN CMD ` psql -U postgres postgres`
– TO SHOW List of Relations `d`
– TO SHOW List of Roles `du`
– Constraints
* NOT NULL
* UNIQUE
* PRIMARY KEY
* DEFAULT
* CHECK
* …
– Functions
* SUM
* COUNT
* MIN
* MAX
* AVG
* …
– CREATE TABLE
* `CREATE TABLE flights(
id SERIAL PRIMARY KEY,
origin VARCHAR NOT NULL,
destination VARCHAR NOT NULL,
duration INTEGER NOT NULL
);`
– INSERT ROW
* `INSERT INTO flights
(origin, destination, duration)
VALUES ('New York', 'London', 415);`
– SELECT
* `SELECT * FROM flights;`
* `SELECT origin, destination FROM flights;`
* `SELECT * FROM flights WHERE duration > 400;`
* `SELECT * FROM flights WHERE id = 3;`
* `SELECT AVG(duration) FROM flights;`
* `SELECT AVG(duration) FROM flights WHERE origin = 'New York';`
* `SELECT COUNT(*) FROM flights;`
* `SELECT COUNT(*) FROM flights WHERE origin = 'New York';`
* `SELECT MIN(duration) FROM flights;`
* `SELECT MAX(duration) FROM flights;`
* `SELECT * FROM flights WHERE origin IN ('New York', 'Lima');`
* `SELECT * FROM flights WHERE destination = 'Paris' AND duration > 500;`
* `SELECT * FROM flights WHERE destination = 'Paris' OR duration > 500;`
* `SELECT * FROM flights WHERE origin LIKE '%a%';` # contain a char
* `SELECT * FROM flights ORDER BY duration ASC LIMIT 3;`
* `SELECT * FROM flights ORDER BY duration DESC;`
* `SELECT origin, COUNT(*) FROM flights GROUP BY origin;`
* `SELECT origin, COUNT(*) FROM flights GROUP BY origin HAVING COUNT(*) > 1;`
– DELETE
* `DELETE FROM flights;` # Delete all rows
* `DELETE FROM flights WHERE id=0;`
– UPDATE
* `UPDATE flights
SET duration = 430
WHERE origin = 'New York'
AND destination = 'London';`
– JOIN
* `SELECT origin, destination, name FROM flights JOIN passengers ON passengers.flight_id = flights.id;`
* `SELECT origin, destination, name FROM flights JOIN passengers ON passengers.flight_id = flights.id; WHERE name='Alice'`
* `SELECT origin, destination, name FROM flights INNER JOIN passengers ON passengers.flight_id = flights.id;`
* `SELECT origin, destination, name FROM flights JOIN passengers ON passengers.flight_id = flights.id;`
* `SELECT origin, destination, name FROM flights LEFT OUTER JOIN passengers ON passengers.flight_id = flights.id;`
* `SELECT origin, destination, name FROM flights RIGHT OUTER JOIN passengers ON passengers.flight_id = flights.id;`
– INDEXING
* Speed up the selection from table
* `CREATE INDEX duration_index ON flights (duration);
`
https://www.youtube.com/watch?v=Eda-NmcE5mQ&t=5720s … I understand how this protects against sql injection but don't see how it protects from race conditions.
day 2/100 #100daysofcode
Still ststuck on this error, please help, i'm losing motivation, tried so many things
Error: flask.cli.NoAppException: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
if the colon variable thing ( :variable ) is giving you errors like the following –
db.execute("INSERT INTO flights(origin, destination, duration) VALUES (:origin, :destination, :duration)", {"origin" : origin, "destination" : destination, "duration" : duration})
wrap the string part into a text() like the following –
db.execute(text("INSERT INTO flights(origin, destination, duration) VALUES (:origin, :destination, :duration)"), {"origin" : origin, "destination" : destination, "duration" : duration})
It works for me. For more info check out: https://docs.sqlalchemy.org/en/13/core/sqlelement.html#sqlalchemy.sql.expression.text
Hey Cs50 . Please help me to solve 500:Internal server error in flask .
If you're running into authentication issues on Windows, It's because PSQL by default use your windows user as user. First switch to the postgres with "psql -u postgres" then enter the password you created when you ran the PSQL installer.
Brian. Computer Monk.
1:35:07 I think he meant malicious SQL injections here.
If you are stuck with this error
engine = create_engine(os.getenv("DATABASE_URL"))
AttributeError: 'NoneType' object has no attribute '_instantiate_plugins'
I solved it by
1. installing psycopg2 —– pip install psycopg2
2. Edited engine to read like this engine=create_engine("postgres://username:password@localhost:5432/name_of_db")
When I click on the "book ticket " button it does not redirect to the success or the error page instead shows "server error".
And in the bash terminal it shows an error:
"sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) column "flight_id" of relation "passengers" does not exist
"
Does anybody have any idea about what is wrong here..
This kid is still in doing his undergrad and started teaching his 2nd year in college! Da fuck.
Thanks, sir for this wonderful video…But I am not able to use import CSV technique
CREATE TABLE flights (
id SERIAL PRIMARY KEY,
origin VARCHAR NOT NULL,
origin_code VARCHAR NOT NULL,
destination VARCHAR NOT NULL,
destination_code VARCHAR NOT NULL,
duration INTEGER NOT NULL
);
I can’t seem to connect to the postgresql database via terminal. I have windows.
My command was “psql ‘your url’”
https://cs50.github.io/web/2018/spring/projects/1
any examples for sign up/sign-in source code?
where's all the projects? does this course have a website? all links are disabled
https://docs.cs50.net/web/2018/x/projects/1/project1.html
Set the environment variable DATABASE_URL to be the URI of your database, which you should be able to see from the credentials page on Heroku.
In line 20 engine = create_engine(os.getenv, I have copy pasted the URI of my database but when I run, I get this error "DATABASE URL NOT SET"
Traceback (most recent call last):
File "application.py", line 9, in <module>
engine = create_engine(os.getenv("DATABASE_URL"))
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/__init__.py", line 424, in create_engine
return strategy.create(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/strategies.py", line 52, in create
plugins = u._instantiate_plugins(kwargs)
AttributeError: 'NoneType' object has no attribute '_instantiate_plugins'
Hey CS50 kindly help me to resolve the database problem in postregsql , i have created simple database named airline what to do for connecting it with flask application..
Why can't I run any command that he runs on his terminal on mine?