How to Use SQLite Viewer Flask
Last Updated :
28 Apr, 2025
SQLite is a popular lightweight relational database management system that is used in a variety of applications. Flask is a popular web framework for building web applications in Python. SQLite Viewer Flask is a tool that allows you to view the contents of an SQLite database within a Flask application.
Flask is a web framework for building web applications in the Python programming language. It is a lightweight framework that provides the necessary tools and libraries to build web applications quickly and easily. In this article, we will learn how to use SQLite Viewer in Flask.
Step 1: Install Required Libraries
The first step is to install the required libraries. You will need to install Flask and SQLite3. You can install them using pip.
pip install Flask
pip install sqlite3
Step 2: Project Structure
Project Structure
Step 3: Create an SQLite Database and insert values into the database
The next step is to create an SQLite database. You can use any SQLite management tool to create an SQLite database. For this example, we will create a simple database with a single table called "users" with columns for "id", "name", and "email".
sql.py
Python3
import sqlite3
# connect to the database
conn = sqlite3.connect('example.db')
# create a cursor object
c = conn.cursor()
# create a table named 'users' with columns for id, name, and email
c.execute('''CREATE TABLE users
(id INTEGER PRIMARY KEY, name TEXT, email TEXT)
''')
# insert five rows of data into the 'users' table
c.execute('''
INSERT INTO users (name, email) VALUES
('John Doe', '[email protected]'),
('Jane Smith', '[email protected]'),
('Bob Johnson', '[email protected]'),
('Emily Davis', '[email protected]'),
('Mike Lee', '[email protected]');
''')
# commit changes to the database
conn.commit()
# close the database connection
conn.close()
Output:
Â
Step 4: Create Flask Application
First, we set up the project using the following commands:
touch app.py
This is a Python code for a simple Flask web application that retrieves data from a SQLite database table called 'users' and renders it on an HTML template called 'index.html'. The Flask app is created using the Flask class, and a route is defined for the default URL. When the route is accessed, the app connects to the database, retrieves all the data from the 'users' table, pass it as a variable to the 'index.html' template, and renders the template with the data. Finally, the app is run in debug mode using the run() method.
app.py
Python3
# import Flask and render_template
from flask import Flask, render_template
# import sqlite3 library for database connection
import sqlite3
# create a Flask instance called app
app = Flask(__name__)
# route the default URL
@app.route('/')
def home():
# connect to the example.db database
conn = sqlite3.connect('example.db')
# create a cursor object to execute SQL commands
c = conn.cursor()
# execute a SQL command
c.execute('SELECT * FROM users')
# retrieve all the data from the users table
users = c.fetchall()
# close the database connection
conn.close()
# render the index.html template
return render_template('index.html', users=users)
if __name__ == '__main__':
app.run(debug=True)
Step 5: Create an HTML Template
Finally, we need to create an HTML template that will display the contents of our SQLite database.
mkdir templates
cd templates
touch index.html
index.html
HTML
<!DOCTYPE html>
<html>
<head>
<title>SQLite Viewer Flask</title>
</head>
<body>
<h1>Users</h1>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
{% for user in users %}
<tr>
<td>{{ user[0] }}</td>
<td>{{ user[1] }}</td>
<td>{{ user[2] }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
Output
Output
Output GIF
SQLite Viewer GIF
Similar Reads
How to Update the Flask Version Python is known for its vast collection of libraries and frameworks and when we talk about web development in Python programming language Flask comes into mind. Flask is a micro-framework in Python used to build and manage the server-side logic of web applications and APIs. In this article, we will
2 min read
How to Create View in SQLite SQLite is a self-contained, serverless, and open-source relational database management system. It is used for simplicity, efficiency, and portability, SQLite is widely employed in diverse applications, from embedded systems to mobile devices and large-scale software. It is serverless, zero-configura
6 min read
How to Use CSS in Python Flask Flask is a popular web framework for Python that makes it easy to build web applications. One of the key elements of any web application is styling, which is where CSS (Cascading Style Sheets) comes in. CSS allows us to control the look and feel of our web pages, making them more attractive and user
3 min read
Python SQLite - WHERE Clause Where clause is used in order to make our search results more specific, using the where clause in SQL/SQLite we can go ahead and specify specific conditions that have to be met when retrieving data from the database.If we want to retrieve, update or delete a particular set of data we can use the whe
4 min read
How to Build a Web App using Flask and SQLite in Python Flask is a lightweight Python web framework with minimal dependencies. It lets you build applications using Python libraries as needed. In this article, we'll create a Flask app that takes user input through a form and displays it on another page using SQLite.Run the following commands to install Fl
3 min read
SQLite Show Tables SQLite is a lightweight database library written in C which is used for embedding the database with applications. SQLite is a serverless, lightweight, cross-platform, and highly reliable database engine that provides the standard SQL syntax making it easy to use standalone or integrate with any othe
8 min read
How To Use Web Forms in a Flask Application A web framework called Flask provides modules for making straightforward web applications in Python. It was created using the WSGI tools and the Jinja2 template engine. An example of a micro-framework is Flask. Python web application development follows the WSGI standard, also referred to as web ser
5 min read
SQLite DROP VIEW SQLite is an embedded database that doesn't use a database like Oracle in the background to operate. It is written in C language and is used by developers who embed a lightweight database over the existing application, browser, or embedded systems. The main features of SQLite are that it is a tiny,
4 min read
How to use Flask-Session in Python Flask Sessions in Flask store user-specific data across requests, like login status, using cookies. Data is stored on the client side but signed with a secret key to ensure security. They help maintain user sessions without requiring constant authentication.This article demonstrates how to implement serve
4 min read
How to Alter a SQLite Table using Python ? In this article, we will discuss how can we alter tables in the SQLite database from a Python program using the sqlite3 module. We can do this by using ALTER statement. It allows to: Add one or more column to the tableChange the name of the table.Adding a column to a table The syntax of ALTER TABLE
3 min read