📜  Flask – SQLite(1)

📅  最后修改于: 2023-12-03 15:15:05.796000             🧑  作者: Mango

Flask – SQLite

Flask is a popular Python web framework that allows developers to build web applications easily and quickly. SQLite, on the other hand, is a lightweight relational database management system that is widely used in mobile and embedded devices.

Flask provides built-in support for SQLite, making it easy for developers to integrate SQLite databases into their Flask applications. In this article, we will explore how to use SQLite with Flask.

Setting up a Flask App with SQLite

To get started, we need to create a Flask app and connect it to an SQLite database. We can do this using the flask_sqlalchemy extension.

First, let's install flask_sqlalchemy:

pip install flask_sqlalchemy

Next, let's create a Flask app and initialize flask_sqlalchemy:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///example.db'
db = SQLAlchemy(app)

In the code above, we created a Flask app and set the SQLALCHEMY_DATABASE_URI configuration variable to connect to an SQLite database named example.db.

We also initialized db with our app instance, which gives us access to the db object that we can use to interact with the database.

Creating a Model

Next, let's create a model that we can use to store data in our database. In this example, we'll create a simple User model:

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)

This model defines a table named user with three columns: id, username, and email. The id column is the primary key, and the username and email columns are both unique and required.

Creating a Database

Once we have defined our model, we need to create our database. We can do this by running the following commands in the Python shell:

from app import db
db.create_all()

This will create an SQLite database with our User table.

Adding Data

Now that we have a database, let's add some data to it. We can do this by creating new User objects and adding them to the database:

db.session.add(User(username='john', email='john@example.com'))
db.session.add(User(username='jane', email='jane@example.com'))
db.session.commit()

This will add two new users to our database.

Querying Data

Finally, let's retrieve our data from the database. We can do this using the query method on the User model:

users = User.query.all()
for user in users:
    print(user.username, user.email)

This will retrieve all users from the database and print their usernames and emails.

Conclusion

In this article, we learned how to use SQLite with Flask. We created a Flask app, connected it to an SQLite database, created a model, created a database, added data to it, and queried data from it.

SQLite is a great choice for small to medium-sized web applications, and Flask's built-in support for SQLite makes it easy to get started.