2018-08-31

PeeWee

PeeWee

f
from treehouse

from peewee import *  # import all of them 
 db =  SqliteDatabase('students.db')  # make new datebase. 
# this is a  name, student.db but any other name is okay. something like students.

class Student(Model):
    username = CharField(max_length=255, unique=True)
    points = IntegerField(default=0)
# E3: 
        class Meta:
            database = db

if __name__ == '__main__':
    # if our file is run directly and not imported. 
    db.connect() 
    # connect to the database. 
    db.create_tables([Student], safe=True)
    # create the tables. 
    # arg, safe=True why? 
        # The reason we have to pass in the safe equals true keyword argument is so, that if we run this multiple times toward the database that's been already created. 
        #  And the table has already been created, peewee won't freak out and break.
  • E3:
a CharField is a field that holds characters.
“In sequel terminology, this is a VarChar field . “

sqlite3 students.db
.tables 
select * from student;
.exit


# New Terminology

model - A code object that represents a database table
SqliteDatabase - The class from Peewee that lets us connect to an SQLite database
Model - The Peewee class that we extend to make a model
CharField - A Peewee field that holds onto characters. It’s a varchar in SQL terms
max_length - The maximum number of characters in a CharField
IntegerField - A Peewee field that holds an integer
default - A default value for the field if one isn’t provided
unique - Whether the value in the field can be repeated in the table
.connect() - A database method that connects to the database
.create_tables() - A database method to create the tables for the specified models.
safe - Whether or not to throw errors if the table(s) you’re attempting to create already exist

 .

댓글 없음:

댓글 쓰기