Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Using Databases in Python Our Diary App Doing Data Entry

Where did the databases come from in this video?

I am confused how our current file (diary.py) contains the other files in our current workspace. I.e. - when we do ls after typing python diary.py, why do queries.py, students.db and students.py show? I see no mention of those files in our diary.py file. See below for the code i am referring to.

Thanks!

Kathryn

import datetime 
from peewee import *

db = SqliteDatabase('diary.db')

class Entry(Model):
    content = TextField()
    timestamp = DateTimeField(default=datetime.datetime.now)

    class Meta:
        database = db

def initailize():
    """Create the database and table if they don't exist"""
    db.connect()
    db.create_tables([Entry], safe=True)

def menu_loop():
    """show the menu"""


def add_entry():
    """add an entry"""

def view_entry():
    """view previous entries"""

def delete_entry(entry):
    """Delete and entry"""

if __name__ == '__main__':
    menu_loop()

1 Answer

Cheo R
Cheo R
37,150 Points

Your confusion is one reason why it's discouraged to use the Kleene star to import everything from a package: if it were used in combination with other packages, nobody would know what came from where.

The database comes from the first two lines:

from peewee import *

db = SqliteDatabase('diary.db')

The first one says, import everything from peewee; the second ones creates the database. Here is an example showing it done without everything being imported, simply:

import peewee

db = peewee.SqliteDatabase('test.sqlite')

got it, thanks!