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

Tom Finet
Tom Finet
7,027 Points

Do you need to install sqlite to use databases in flask?

I am building a flask API and need to use a database to store my data.

My model looks like this:

from peewee import *
import datetime

db = SqliteDatabase('stock.sqlite')

class Stock(Model):
    id = PrimaryKeyField()
    date = DateTimeField(default = datetime.datetime.now)
    stock_type = CharField()
    stock_title = CharField()
    stock_cost = IntegerField()
    stock_number = IntegerField()

    class Meta:
        database = db;

def initialize_db():
    db.connect();
    db.create_tables([Stock], safe=True)

Should I import sqlite at the top of the file, to do this do I need to install some package or does it come with flask? Basically what steps do I need to perform to set up an sqlite database?

2 Answers

Ryan S
Ryan S
27,276 Points

Hi Tom,

I believe Sqlite is included with the Python standard library. You shouldn't have to install anything. And as far as importing Sqlite, I don't believe that is necessary either. To be honest, I am little confused on the subject as well. In the Flask API course, we didn't import it at all, and by using similar code to what you have provided, it worked just fine. But if you read the documentation it often suggests to import Sqlite3.

I would say try it as you have it now, it should work. When you run it you should see a database file created in your working directory.

Good luck.

Tom Finet
Tom Finet
7,027 Points

I solved it, just looked up the error on stackoverflow and the problem was solved. =) Thanks anyways Ryan!

Ryan S
Ryan S
27,276 Points

Good to hear. Just curious, what was the solution?