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 Gettin' CRUD-y With It View and Search Entries

no such table: entry

Hello, I have been trying to get diary.py to work, however, after entering the diary entry and pressing Ctrl D, this error happens:

File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/peewee.py", line 2657, in __e
reraise(new_type, new_type(*exc_value.args), traceback)                                      
File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/peewee.py", line 115, in rera
raise value.with_traceback(tb)                                                               
File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/peewee.py", line 2780, in exe
cursor.execute(sql, params or ())                                                            
peewee.OperationalError: no such table: entry 

Code:

#!/usr/bin/env python3

import datetime
import sys

from peewee import *
from collections import OrderedDict


db = SqliteDatabase('diary.db')



class Entry(Model):
  content = TextField() #Holds whatever text we want it to hold
  timestamp = DateTimeField(default=datetime.datetime.now)

  class Meta:
    database = db

def initalize():
  """Create the database and the table if they don't exist"""

def menu_loop():
  choice = None #Makes it a pointless variable. (giving it without a value)

  while choice != 'q': #While its not equal to q
    print("Enter 'q' to quit.")
    for key, value in menu.items():  #KGets the key of the dict, and the value of the dict.a
      print('{}) {}'.format(key, value.__doc__)) #The values are functions, reads the doc of 
    choice = input('Action: ').lower().strip()

    if choice in menu:
      menu[choice]() #We go back to menu, find the function they selected, and run it.

def add_entry():
  """Add an entry."""
  print('Enter your entry. Press ctrl+d when finished.')
  data = sys.stdin.read().strip()

  if data:
    if input('Save entry? [Y/n ').lower() != 'n':
      Entry.create(content=data)
      print('Saved successfuly.')



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

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

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

  ])  

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

1 Answer

Tatiana Vasilevskaya
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tatiana Vasilevskaya
Python Web Development Techdegree Graduate 28,600 Points

There is some code missing in the diary.py file, in particular the body of initialize function.

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