Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Using Databases in Python Meet Peewee Modeling

nicole lumpkin
PLUS
nicole lumpkin
Courses Plus Student 5,328 Points

Accessing sqlite3 students.db in Terminal

So far I've been learning the Python track through my IDE Spyder which is ran through Anaconda. I have little to no experience using terminal but I was able to access SQLite by typing the command sqlite3 student.db. Similar to the video my version of SQLite opened, and I input '.tables', however, student did not appear. Does anyone have any insight as to why??

1 Answer

Dear Nicole,

I think your database is just "empty" there is no table.

Did you execute before something like that?

from peewee import *

db = SqliteDatabase('students.db')


class Student(Model):
  username = CharField(max_length=255, unique=True)
  points = IntegerField(default=0)

  class Meta:
    database = db

if __name__ == '__main__':
  db.connect()
  db.create_tables([Student], safe=True)

or might you can try something like that

import sqlite3

con = sqlite3.connect('Your_path_to_the_DB')

with con:
        cur = con.cursor()
        cur.execute("CREATE TABLE Students(Id INT, Name TEXT)")

But you need to create the table than you can look inside of the DB and see it.

I reproduced it on the workingspace and sqlite3 students.db .tables

worked for me. (after running code)

Greetings Julian

nicole lumpkin
nicole lumpkin
Courses Plus Student 5,328 Points

Because this is my first experience using anything database related I wasn't sure what was necessary. I did run your first chunk of code within Spyder. Are you saying I need to run that code within Terminal. Or perhaps do I need to navigate to the directory in which students.py is located and then run sqlite3 students.db from there?

nicole lumpkin
nicole lumpkin
Courses Plus Student 5,328 Points

Hooray!! I navigated to the directory that contained students.py and everything worked! Thanks Julian Steffen