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 Meet Peewee Modeling

deepak kumar pradhan
PLUS
deepak kumar pradhan
Courses Plus Student 3,133 Points

i am not able to see the table after connecting to sqlite3 db. i ran these cmd sqlite3 students.db .tables

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)

2 Answers

Josh Keenan
Josh Keenan
20,315 Points

Here's the problem, you don't close the database after, use:

db.close()

If I'm wrong post to this thread again!

James N
James N
17,864 Points

you are wrong... I try to type .tables into sqlite and it doesnt work!

mycode.py
from peewee import *

db = SqliteDatabase("students.db")


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

    class Meta:
        databse = db

if __name__ == "__main__":
    db.connect()
    db.create_tables([Student], safe=True)
    db.close()
myoutput.txt
treehouse:~/workspace$ python students.py                                                                                                                                                                   
treehouse:~/workspace$ sqlite3 students.db                                                                                 
SQLite version 3.7.17 2013-05-20 00:56:22                                                                                  
Enter ".help" for instructions                                                                                             
Enter SQL statements terminated with a ";"                                                                                 
sqlite> .tables                                                                                                            
sqlite> select * from student;                                                                                             
Error: no such table: student                                                                                              
sqlite> .exit                                                                                                              
treehouse:~/workspace$                                                                                                     

with the myoutput.txt file above, i just basically mimicked exactly what the teacher did.

Josh Keenan
Josh Keenan
20,315 Points

What? I don't understand what you mean?

I'll watch the video

James N
James N
17,864 Points

what don't you understand? to be more specific, when i type .tables, i don't get back anything.

James N
James N
17,864 Points

did i close it in the wrong spot?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

James. It might be helpful to start a new post. And post your models. How are you initing the DB? You posted mycode.py but ran python students.py

Did you run python mycode.py?

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

I ran your code locally (saved as dbtest.py) and inspected with sqline3 successfully:

$ python dbtest.py 
$ sqlite3 students.db 
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
student
sqlite> .dump student
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE "student" ("id" INTEGER NOT NULL PRIMARY KEY, "username" VARCHAR(255) NOT NULL, "points" INTEGER NOT NULL);
CREATE UNIQUE INDEX "student_username" ON "student" ("username");
COMMIT;
sqlite> .q
$ 

What errors were you seeing?