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

Lucas Andrews
Lucas Andrews
4,919 Points

Running command .tables indicates that initialize() does not appear to be creating Entry as a table.

Here is the class and initialise() code:

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

    class Meta:
        databse = db


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

And later in the document, here is where initialize() is called:

if __name__ == '__main__':
    initialize()
    menu_loop()

Any assistance would be much appreciated - thanks.

2 Answers

Kris Powell
Kris Powell
6,246 Points

Hi! I'm not sure if it's causing your issue, but I noticed a typo:

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

    class Meta:
        databse = db

database variable should be:

    class Meta:
        database = db

gl! kris

Lucas Andrews
Lucas Andrews
4,919 Points

Thanks, Kris - I've corrected that typo, but calling .tables still doesn't return any results. Here's the code in full:

#!/usr/bin/env python3

from collections import OrderedDict
import datetime

from peewee import *

db = SqliteDatabase('diary.db')

#menu = OrderedDict([
#    ('a', add_entry),
#    ('v', view_entries),
#])


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

    class Meta:
        database = db


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


def menu_loop():
    """Show the menu."""
    choice = None

    while choice != 'q':
        print("Enter 'q' to quit.")
        for key, value in menu.items():
            print('{}) {}'.format(key, value.__doc__))
            choice = input('Action: ').lower().strip()

            if choice in menu:
                menu[choice]()


def add_entry():
    """Add an entry."""


def view_entries():
    """View previous entries."""


def delete_entry(entry):
    """Delete an entry."""


if __name__ == '__main__':
    initialize()
#    menu_loop()
Kris Powell
Kris Powell
6,246 Points

Your OrderedDict is commented-out there, if you cut it from where it is, above the Entry class, and paste it below your delete_entry() method, but before dunder name, uncomment it, you should have some success. Let me know :)

Kris Powell
Kris Powell
6,246 Points

Sorry! I got carried away there. I can't see why your tables aren't being created. Part of your menu loop might be indented incorrectly, but it shouldn't have any effect on the initialize() method. I don't know if it will help but are you running the file on a local machine or in workspaces? Maybe there could be an issue with your sqlite3 install? Sorry if I confused things! kris

edit: Now that I see your code on a desktop screen rather than mobile, it is easy to see why you have commented those blocks.

Kris Powell
Kris Powell
6,246 Points

I ran your code without any problem on my machine. Checked sqlite and the entry table was present. Wish I could be more help :/

Lucas Andrews
Lucas Andrews
4,919 Points

Thanks, Kris - I'm running this on Workspaces. Interesting that you were able to run it on your end. I'll try to run the script in Python 3.5.2 Shell for Windows (I've only been using Workspaces so far).