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

James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,137 Points

cant run sqlite3 from powershell

Hi,

trying to work through the database section in learn flask track.

I can create the students.py file, and it creates a file called students.db.

I cannot however, run sqlite3 to do any of the additional tasks done during the video lesson.

sqlite3 is not recognized as the name of a cmdlet, function etc error when tried in powershell. In the Python shell I get an invalid syntax error when typing 'sqlite3 students.py'

it seems that python has sqlite3 module installed in Python35-32\lib\sqlite3\init.py, this shows when I type sqlite3 into the python shell alone.

I think I need to add SQLite3 to PATH?

5 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,989 Points

Correct. sqlite3 must be installed in an existing directory on your PATH or the install directory must be added to your PATH.

James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,137 Points

Thank you Chris,

any advice on how to do this? I have tried to set a path via the powershell command line and through the GUI, but no luck.

the sqlite3 database is included with python3, is this the problem? there is no .exe file.

James

Chris Freeman
Chris Freeman
Treehouse Moderator 67,989 Points

The python sqlite module provides a programmatic way an interface a sqlite database. It does not have an interactive interface. To get this you must download windows version of an sqlite executable. Once downloaded and installed, the PATH variable can be changed to add the sqlite3 install directory (see instructions)

Matthew Goodman
Matthew Goodman
12,786 Points

Please help been trying most of the day to get a DB to work tried MySql and sqlite3 which comes with Python yet not matter what i do or google nothing works tried the paths everything ? is there a DB that will work im now looking at mamp but done nothing as yet. just need some help. thanks

the error on my latest try.

C:\Users\Matt\Documents\GitHub\database [master +8 ~0 -0 !]> python .\date.py
Traceback (most recent call last):
  File ".\date.py", line 14, in <module>
    db.create_table([Student], safe=True)
  File "C:\Users\Matt\AppData\Local\Programs\Python\Python35-32\lib\site-package
s\peewee.py", line 3540, in create_table
    return self.execute_sql(*qc.create_table(model_class, safe))
  File "C:\Users\Matt\AppData\Local\Programs\Python\Python35-32\lib\site-package
s\peewee.py", line 1929, in inner
    return self.parse_node(fn(*args, **kwargs))
  File "C:\Users\Matt\AppData\Local\Programs\Python\Python35-32\lib\site-package
s\peewee.py", line 1948, in _create_table
    meta = model_class._meta
AttributeError: 'list' object has no attribute '_meta'
C:\Users\Matt\Documents\GitHub\database [master +8 ~0 -0 !]> sqlite3 students.db

following the video to the letter.

[MOD: fixed formating -cf]

Chris Freeman
Chris Freeman
Treehouse Moderator 67,989 Points

Matthew Goodman, don't confuse Model.create_table() with DATABASE.create_tables(). you need the latter (plural) method when working with the database. (see docs)

James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,137 Points

umm, you might need someone with a bit more expertise, but if you want to just create a db using peewee and SQLite, lets call the database students.db as in your error report:

from peewee import *

DATABASE = SqliteDatabase('students.db')

Model classes are the database tables, a model instance creates a row in the table, and a field instance is a column in the table.

class Student(Model):
     studentName = CharField()
     studentCourse = CharField()

     class Meta:
         database = DATABASE

DATABASE.connect()
DATABASE.create_tables([Student])

# creating a student entry
# I am creating a variable to hold the instance, and then calling save on it

aStudent = Student(studentName = 'first student', studentCourse = '101')
aStudent.save()

I dont know if that is helpful but maybe it is...

[MOD: added ```python markdown formating -cf]

Matthew Goodman
Matthew Goodman
12,786 Points

chrisfreeman3 you sir are correct thanks so much, I think was just along day yesterday many thanks really appreciate the help. one last thing I cannot seem to check in the DB using the CMD when I type sqlite3 students.db, get error? any ideas on this thanks again.

justlevy
justlevy
4,891 Points

I solved this issue on a Windows 64 bit. This was confusing for me since I understood Python came preinstalled with SQLITE3.

I did have to create a PATH. However, first I had to download SQLITE3 files 'sqlite-tools-win32-x86-3360000.zip' and 'sqlite-dll-win64-x64-3360000.zip'. I believe you need both. As mentioned, I'm running 64 bit but I still needed to download that first package even though it says win32. That download includes the CLI executable (.exe).

After unzipping all the files to the same directory I had to set a PATH. Close and reopen Visual Studio Code and Terminals. Type 'sqlite3' and it should start the CLI.

Hope this is helpful to someone. More info here: https://www.sqlitetutorial.net/download-install-sqlite/