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

Josh Benner
Josh Benner
5,554 Points

Table entry has no column named timestamp

Tried saving another entry and then this error hit. Note: I named the file journal.py instead of diary.py

journal.py:

!/usr/bin/env python3

from collections import OrderedDict import datetime import sys

from peewee import *

db = SqliteDatabase('journal.db')

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

class Meta:
    database = db

def initialize(): '''Create the database and 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 k,v in menu.items():
        print('{}) {}'.format(k,v.__doc__))
    choice = input('Action: ').lower().strip()

    if choice in menu:
        menu[choice]()

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? [Yn] ').lower() != 'n':
        Entry.create(content=data)
        print("Saved successfully")

def view_entries(): '''view previous entries''' entries = Entry.select().order_by(Entry.timestamp.desc()) for entry in entries: timestamp = entry.timestamp.strftime('%A %B %d % Y %I:%M%p') print(timestamp) print('='*len(timestamp)) print(entry.content) print('N) next entry') print('q) return to main menu')

    next_action = input('Action: [Nq] ').lower().strip()
    if next_action == 'q':
        break

def delete_entry(): '''delete an entry'''

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

if( name == 'main'): initialize() menu_loop()

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/peewee.py", line 3191, in execute_sql cursor.execute(sql, params or ()) sqlite3.OperationalError: table entry has no column named timestamp

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/Users/joshbenner/Documents/journal.py", line 75, in <module> menu_loop() File "/Users/joshbenner/Documents/journal.py", line 36, in menu_loop menuchoice File "/Users/joshbenner/Documents/journal.py", line 46, in add_entry Entry.create(content=data) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/peewee.py", line 4127, in create inst.save(force_insert=True) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/peewee.py", line 4287, in save pk_from_cursor = self.insert(**field_dict).execute() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/peewee.py", line 2980, in execute cursor = self.execute() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/peewee.py", line 2470, in _execute return self.database.execute_sql(sql, params, self.require_commit) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/peewee.py", line 3199, in execute_sql self.commit() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/peewee.py", line 3048, in __exit_ reraise(new_type, new_type(*exc_args), traceback) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/peewee.py", line 123, in reraise raise value.with_traceback(tb) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/peewee.py", line 3191, in execute_sql cursor.execute(sql, params or ()) peewee.OperationalError: table entry has no column named timestamp

3 Answers

Owen Grace
Owen Grace
5,292 Points

I had the same problem but have a solution.

The issue is with how Kenneth progressively developed the code. You'll notice we initially run the code and make a database with only the text in it - but no timestamp field. Later we tell the code to add a timestamp to the entries, but the database doesn't seem to have that field - which I think trips the code.

The solution is to delete the old diary.db file and run the code again (adding at least one new entry). It then works after you do that.

Martin Cornejo Saavedra
Martin Cornejo Saavedra
18,132 Points

Your code is not easy to read that way, read the markdown Cheatsheet.

Edit: I think the server may be a little buggy so the code format may be not your fault.

Anyway, I guess the error is here:

print('='*len(timestamp))   #this is wrong
print('='*len(entry.timestamp))   #this is right
mykolash
mykolash
12,955 Points

hi guys, nope, I think(!), it deals nothing with timestamp/entry.timestamp cause of the string:

timestamp = entry.timestamp.strftime('%A %B %d, %Y %I:%M%p')

so, while printing, we're dealing not with entry's attribute itself, but with a number of symbols of this attribute in a sense of this (a bit tricky) '%A %B %d, %Y %I:%M%p' -view

again:

timestamp - a string

entry.timestamp - an attribute of the Entry class

pls, correct me if I'm wrong

mykolash
mykolash
12,955 Points

btw, I've met kinda similar issue - there were no timestamp attribute of the Entry class. But it was my fault, cause (for some reason) I've made a typo and called this attr not 'timestamp' but 'datestamp'. And db was initialized with this 'datestamp' attr. So, after renaming all 'timestamp's into 'datestamps's it works.