CREATE TABLE Student (
zID int,
first_name varchar(255),
last_name varchar(255)
);
INSERT INTO Student (zid, first_name, last_name)
VALUES
(5157372, 'Adam', 'Stucci'),
(5255918, 'An Thy', 'Tran'),
(5204829, 'Andrew', 'Han'),
(5204996, 'Braedon', 'Wooding'),
(5122521, 'Carlin', 'Williamson'),
(5259819, 'Chloe', 'Cheong'),
(5260633, 'Clarence', 'Feng'),
(5208437, 'Dominic', 'Wong'),
(5169811, 'Eddie', 'Qi'),
(5263737, 'Esther', 'Wong'),
(5210932, 'Kaiqi', 'Liang'),
(5113139, 'Max', 'Kelly'),
(5169779, 'Nick', 'Patrikeos'),
(5169766, 'Noa', 'Challis'),
(5214750, 'Vivian', 'Shen'),
(5260889, 'Weijie', 'Wang');
-- Select all
SELECT * FROM Student;
-- Select a specific column
SELECT zID FROM Student;
-- Filter by a column
SELECT first_name, last_name FROM Student WHERE zID = 5169779;
UPDATE Student SET zID = 555555 WHERE first_name = 'Nick';
DELETE FROM Student WHERE zID = 555555;
CREATE TABLE IF NOT EXISTS Program (
program_id int primary key
);
CREATE TABLE Students (
zID int primary key,
firstname varchar(255),
lastname varchar(255),
program_id int references Program(program_id)
);
import peewee as pw
db = pw.SqliteDatabase('students.db', pragmas={'foreign_keys': 'on'})
class Entity(pw.Model):
class Meta:
database = db # which database are we connected to?
class Program(Entity):
program_id = pw.IntegerField(primary_key=True)
class Student(Entity):
zID = pw.IntegerField(primary_key=True)
first_name = pw.TextField()
last_name = pw.TextField()
program_id = pw.ForeignKeyField(Program)
if __name__ == '__main__':
db.connect()
db.create_tables([Program, Student], safe=True)
db.close()
from model import Program, Student
def get_programs():
return Program.select().dicts()
def get_student_by_id(id: int):
result = Student.select().where(Student.zID == id).dicts()
if result:
return result[0]
def get_students_and_program_by_program(program_id: int):
return Student.select().join(Program)
.where(Program.program_id == program_id)
.dicts()
http://docs.peewee-orm.com/en/latest/
if "HEROKU" in os.environ:
url = urlparse(os.environ["DATABASE_URL"])
db = PostgresqlDatabase(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port,
)
else:
db = SqliteDatabase(
'test.db',
pragmas={
'foreign_keys': 'on'
}
)