Skip to content

Using Databases With Python


ORM - Object Relational Mapper#

Convert rows in tables into object (Models) in code

A simple lightweight one is called peewee

How to import everything

from peewee import *

Working with Mysql#

sudo pip install pymysql


db = MySQLDatabase('peewee')

from peewee import *

make connection to db#

            db = MySQLDatabase('peewee', user='root', password='pass')

            # classes inherit from model
            # always name models for singular name
            # represents a single item in db
            class Student(Model):
                username = CharField(max_length=255, unique=True)
                points = IntegerField(default=0)

                # tell model what database it belongs to
                # class inside of classs
                class Meta:
                    database = db

            students = [
                {'username': 'stephen', 'points':45},
                {'username': 'squash', 'points':100},
                {'username': 'pat', 'points':150},
                {'username': 'matt', 'points':30},
                {'username': 'casey', 'points':1345}

            def add_students():
                for student in students:
                    Student.create(username=Student.username, points=Student.points)

            # if run directly and not imported
            if __name__ == '__main__':
                db.create_tables([Student], safe=True)

Making queries#

  • create()
  • select()
  • save()
  • get()
  • delete_instance()


Python intentionally does not have switches

Can use a dictionary but has no order

So can use an ordered dict

from collections import OrderedDict

Items must be added like a list