Python Using Databases in Python Our Diary App Doing Data Entry

Quentin Durantay
Quentin Durantay
17,880 Points

Database file created, but no tables are visible in Sqlite3.

Hi everybody,

I ran the script as made in the video, but in contrary to Kenneth, I got a "diary.db" file visible in the Workspaces directory (on the upper left). And when I try to use Sqlite3, it says that I have no "Entry" tables in it.

Here's my code:

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 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."""


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


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


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

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

I don't understand where the problem is. If you have any answers I would thank you very much ;)

Chris Freeman
Chris Freeman
Treehouse Moderator 56,832 Points

Try adding a db.close() to your initialize() function.

Can you post the sqlite3 session output?

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 56,832 Points

sqlilte3 can be opened with or without a database on the command line. Without a database, sqlite3 opens without any data. You can open a database using the .open command

$ sqlite3
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
sqlite> .databases
seq  name             file                                                      
---  ---------------  ----------------------------------------------------------
0    main                                                                       
sqlite> .open diary.db
sqlite> .tables
entry
sqlite> .dump entry
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE "entry" ("id" INTEGER NOT NULL PRIMARY KEY, "content" TEXT NOT NULL, "timestamp" DATETIME NOT NULL);
COMMIT;
sqlite> 

The shortcut is to provide the database on the command line:

$ sqlite3 diary.db 
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
entry
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE "entry" ("id" INTEGER NOT NULL PRIMARY KEY, "content" TEXT NOT NULL, "timestamp" DATETIME NOT NULL);
COMMIT;
sqlite> .q
Quentin Durantay
Quentin Durantay
17,880 Points

Yeah ok, as I already know some SQL, it seems logical. But any idea why it isn't working from the Python script? Maybe just a bug from workspaces?

Anyway thank you Chris!

Chris Freeman
Chris Freeman
Treehouse Moderator 56,832 Points

What do mean by not working in the Python script?

Quentin Durantay
Quentin Durantay
17,880 Points

Thank you for your answer Chris. I tried what you said, but the only change is that the .db file is not showing again in the workspaces files (but still when typing ls on the command line, so it's created). Here is a screenshot of my workspace (as you see there is no output from sqlite): link to screenshot

Quentin Durantay
Quentin Durantay
17,880 Points

Oh Thanks Chris, it worked using sqlite3 diary.db, I just omitted to type the file name!