{"id":1183,"date":"2023-08-06T18:40:00","date_gmt":"2023-08-06T13:10:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=1183"},"modified":"2024-03-01T17:09:03","modified_gmt":"2024-03-01T11:39:03","slug":"connect-sqlite-database-with-flask-app","status":"publish","type":"post","link":"https:\/\/geekpython.in\/connect-sqlite-database-with-flask-app","title":{"rendered":"How to Create and Connect an SQLite Database with Flask App using Python"},"content":{"rendered":"\n<p>This article will guide you step by step in making a database using&nbsp;<strong>Flask-SQLAlchemy<\/strong>. It will show you how to work with an&nbsp;<strong>SQLite database<\/strong>&nbsp;in your&nbsp;<strong>Flask app<\/strong>, and then how to&nbsp;<strong>make a form on the website to collect user information and put it into the database<\/strong>.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-installing-flask-sqlalchemy-lib\"><a href=\"https:\/\/geekpython.in\/connect-sqlite-database-with-flask-app#heading-installing-flask-sqlalchemy-lib\"><\/a>Installing Flask-SQLAlchemy Lib<\/h1>\n\n\n\n<p>Flask-SQLAlchemy uses&nbsp;<strong>SQLAlchemy<\/strong>, a powerful ORM (object-relational mapping) library for Python, allowing interaction with databases using Python.<\/p>\n\n\n\n<p>Open the terminal window and install the library using&nbsp;<code>pip<\/code>&nbsp;in your project environment.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">pip install Flask-SQLAlchemy<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-creating-sqlalchemy-instance\"><a href=\"https:\/\/geekpython.in\/connect-sqlite-database-with-flask-app#heading-creating-sqlalchemy-instance\"><\/a>Creating SQLAlchemy Instance<\/h1>\n\n\n\n<p>Create a&nbsp;<code>database.py<\/code>&nbsp;file and add the following code inside the file to create an instance of SQLAlchemy.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"database.py\">\"\"\" database.py file \"\"\"\nfrom flask_sqlalchemy import SQLAlchemy\n\n\"\"\" SQLAlchemy Instance \"\"\"\ndb = SQLAlchemy()<\/pre><\/div>\n\n\n\n<p>This instance will be used to create the database and manage database operations.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-creating-models\"><a href=\"https:\/\/geekpython.in\/connect-sqlite-database-with-flask-app#heading-creating-models\"><\/a>Creating Models<\/h1>\n\n\n\n<p>SQLAlchemy provides an object-oriented approach to creating database tables and columns, defining relationships, and setting constraints using Python classes.<\/p>\n\n\n\n<p><strong>Models<\/strong>&nbsp;let you define a database table with the fields you need in your database with constraints and relationships with other tables and fields by using only Python code.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"models.py\">\"\"\" models.py file\"\"\"\n\n# SQLAlchemy Instance Is Imported\nfrom database import db\n\n# Declaring Model\nclass Vehicle(db.Model):\n    __tablename__ = \"vehicle\"\n\n    id = db.Column(db.Integer, primary_key=True)\n    name = db.Column(db.String(150), unique=True, nullable=False)\n    price = db.Column(db.Integer, nullable=False)\n    created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.current_timestamp())<\/pre><\/div>\n\n\n\n<p>Inside the&nbsp;<code>models.py<\/code>&nbsp;file, the SQLAlchemy instance (<code>db<\/code>) is imported to facilitate interactions with the SQLite database.<\/p>\n\n\n\n<p>A class named&nbsp;<code>Vehicle<\/code>&nbsp;is declared, which inherits from&nbsp;<code>db.Model<\/code>, effectively making it an SQLAlchemy model. Inside this class, the table name and structure are specified with the following details and fields:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>__tablename__<\/code>: This attribute sets the table name in the database, which, in this case, is&nbsp;<code>\"vehicle\"<\/code>.<\/li>\n\n\n\n<li><code>id<\/code>: The&nbsp;<code>id<\/code>&nbsp;column serves as a primary key with an Integer data type, ensuring that each row has a unique id.<\/li>\n\n\n\n<li><code>name<\/code>: The&nbsp;<code>name<\/code>&nbsp;column, represented by a&nbsp;<code>String<\/code>&nbsp;type, will be used to store the vehicle&#8217;s name with a maximum length of 150 characters. The&nbsp;<code>unique<\/code>&nbsp;constraint is enabled to enforce uniqueness, and&nbsp;<code>nullable=False<\/code>&nbsp;ensures that the title must be provided for each entry.<\/li>\n\n\n\n<li><code>price<\/code>: The&nbsp;<code>price<\/code>&nbsp;column, of&nbsp;<code>String<\/code>&nbsp;type, will be used to store the vehicle&#8217;s price, and it cannot be left empty.<\/li>\n\n\n\n<li><code>created_at<\/code>: This column, with a&nbsp;<code>Datetime<\/code>&nbsp;type, captures the timestamp when an entry is created. The current timestamp will be automatically generated using&nbsp;<code>current_timestamp()<\/code>&nbsp;from the&nbsp;<code>func<\/code>&nbsp;module in SQLAlchemy.<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-setup\"><a href=\"https:\/\/geekpython.in\/connect-sqlite-database-with-flask-app#heading-setup\"><\/a>Setup<\/h1>\n\n\n\n<p><strong>Flask-SQLAlchemy<\/strong>&nbsp;can work with a variety of databases, but by default, if you don&#8217;t specify a database URI in your Flask app configuration, it will use&nbsp;<strong>SQLite<\/strong>, a lightweight and simple database suitable for small-scale or demo projects.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"app.py\">from flask import Flask\nfrom database import db\n\n# Creating Flask App\napp = Flask(__name__)\n# Database Name\ndb_name = 'vehicle.db'\n\n# Configuring SQLite Database URI\napp.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:\/\/\/' + db_name\n\n# Suppresses warning while tracking modifications\napp.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False\n\n# Initialising SQLAlchemy with Flask App\ndb.init_app(app)<\/pre><\/div>\n\n\n\n<p>First, the code imports the&nbsp;<code>Flask<\/code>&nbsp;class from the&nbsp;<code>flask<\/code>&nbsp;module, enabling app creation, and the&nbsp;<code>SQLAlchemy<\/code>&nbsp;instance from&nbsp;<code>database<\/code>, facilitating database management.<\/p>\n\n\n\n<p>The Flask app instance is created using&nbsp;<code>Flask(__name__)<\/code>, and the instance is stored in the variable&nbsp;<code>app<\/code>. The use of&nbsp;<code>__name__<\/code>&nbsp;is to determine the root path of the module.<\/p>\n\n\n\n<p>The database name,&nbsp;<code>vehicle.db<\/code>, is defined and stored in the variable&nbsp;<code>db_name<\/code>. The&nbsp;<code>SQLALCHEMY_DATABASE_URI<\/code>&nbsp;config variable is then set, which contains the SQLite database connection string&nbsp;<code>'sqlite:\/\/\/' + db_name<\/code>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>For other database engines, the connection URL formats differ. For example, for MySQL and PostgreSQL, the URL formats are provided as follows.<\/p>\n\n\n\n<p>mysql:\/\/<code>username<\/code>:<code>password<\/code>@<code>host<\/code>:<code>port<\/code>\/<code>database_name<\/code><\/p>\n\n\n\n<p>postgresql:\/\/<code>username<\/code>:<code>password<\/code>@<code>host<\/code>:<code>port<\/code>\/<code>database_name<\/code><\/p>\n<\/blockquote>\n\n\n\n<p>The&nbsp;<code>SQLALCHEMY_TRACK_MODIFICATIONS<\/code>&nbsp;config variable is set to&nbsp;<code>False<\/code>, disabling modification tracking of objects and suppressing warnings.<\/p>\n\n\n\n<p>Finally, an instance of SQLAlchemy is initialized and binds with the Flask app by calling&nbsp;<code>db.init_app(app)<\/code>.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-creating-database\"><a href=\"https:\/\/geekpython.in\/connect-sqlite-database-with-flask-app#heading-creating-database\"><\/a>Creating Database<\/h1>\n\n\n\n<p>There are two approaches, you can perform either one to create the database and a table with the defined fields.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-approach-1-manually-pushing-the-app-context\"><a href=\"https:\/\/geekpython.in\/connect-sqlite-database-with-flask-app#heading-approach-1-manually-pushing-the-app-context\"><\/a>Approach 1 &#8211; Manually Pushing the App Context<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"app.py\">\"\"\" app.py file \"\"\"\n# Previous Code Here\n\n\"\"\" Creating Database with App Context\"\"\"\ndef create_db():\n    with app.app_context():\n        db.create_all()\n\nif __name__ == \"__main__\":\n    from models import Vehicle\n    create_db()<\/pre><\/div>\n\n\n\n<p>The code defines a function named&nbsp;<code>create_db()<\/code>, which utilizes the&nbsp;<code>app_context()<\/code>&nbsp;of the Flask app instance (<code>app<\/code>) with a&nbsp;<code>with<\/code>&nbsp;statement.<\/p>\n\n\n\n<p>In the app context, the&nbsp;<code>create_all()<\/code>&nbsp;method of the&nbsp;<code>db<\/code>&nbsp;object, provided by SQLAlchemy, is invoked to create all the tables specified in the&nbsp;<code>models<\/code>&nbsp;module.<\/p>\n\n\n\n<p>In the&nbsp;<code>if __name__ == \"__main__\"<\/code>&nbsp;block, the&nbsp;<code>Vehicle<\/code>&nbsp;model is imported and&nbsp;<code>create_db()<\/code>&nbsp;function is called to generate the table.<\/p>\n\n\n\n<p>To execute the app, either run it from your Integrated Development Environment (IDE) or use the terminal by entering the following command.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">python app.py<\/pre><\/div>\n\n\n\n<p>Successful execution will result in the creation of a&nbsp;<code>\"vehicle.db\"<\/code>&nbsp;database within the&nbsp;<code>instance<\/code>&nbsp;directory at the root level.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-approach-2-flask-shell\"><a href=\"https:\/\/geekpython.in\/connect-sqlite-database-with-flask-app#heading-approach-2-flask-shell\"><\/a>Approach 2 &#8211; Flask Shell<\/h3>\n\n\n\n<p>When using the&nbsp;<strong>Flask shell<\/strong>, the commands are executed within the context of the Flask app. Below are the CLI commands to run in your terminal.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">D:\\SACHIN\\Pycharm\\FlaskxDB&gt;flask shell\nPython 3.10.5 (tags\/v3.10.5:f377153, Jun  6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32\nApp: app\nInstance: D:\\SACHIN\\Pycharm\\FlaskxDB\\instance\n&gt;&gt;&gt; from database import db\n&gt;&gt;&gt; from models import Vehicle\n&gt;&gt;&gt; db.create_all()\n&gt;&gt;&gt; exit()<\/pre><\/div>\n\n\n\n<p>The SQLAlchemy instance,&nbsp;<code>db<\/code>&nbsp;and the model,&nbsp;<code>Vehicle<\/code>&nbsp;are imported from the&nbsp;<code>database<\/code>&nbsp;and&nbsp;<code>models<\/code>&nbsp;module respectively. Then, the&nbsp;<code>db.create_all()<\/code>&nbsp;command is executed to create a table based on the defined model. Finally, you can exit the shell using the&nbsp;<code>exit()<\/code>&nbsp;command.<\/p>\n\n\n\n<p>The database&nbsp;<code>vehicle.db<\/code>&nbsp;would be created in the instance directory at the project&#8217;s root directory.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-creating-frontend\"><a href=\"https:\/\/geekpython.in\/connect-sqlite-database-with-flask-app#heading-creating-frontend\"><\/a>Creating Frontend<\/h1>\n\n\n\n<p>To create a frontend and collect data from users via form, the following HTML files inside the templates folder need to be created.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tex decode:true \">root_dir(project)\n|_templates\/\n|    |_base.html\n|    |_home.html\n|    |_vehicle.html\n|\n|_app.py\n|_database.py\n|_models.py<\/pre><\/div>\n\n\n\n<p>The layout and Bootstrap CSS and JS will be stored in&nbsp;<code>base.html<\/code>, the vehicle data from the database will be displayed in&nbsp;<code>home.html<\/code>, and a form for adding vehicle data will be stored in&nbsp;<code>vehicle.html<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-basehtml\"><a href=\"https:\/\/geekpython.in\/connect-sqlite-database-with-flask-app#heading-basehtml\"><\/a>base.html<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:xhtml decode:true \" title=\"base.html\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;title&gt;Vehicle&lt;\/title&gt;\n    &lt;link href=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@5.1.3\/dist\/css\/bootstrap.min.css\" rel=\"stylesheet\" integrity=\"sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3\" crossorigin=\"anonymous\"&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n{% block body %} {% endblock %}\n\n&lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@5.1.3\/dist\/js\/bootstrap.bundle.min.js\" integrity=\"sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p\" crossorigin=\"anonymous\"&gt;&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-homehtml\"><a href=\"https:\/\/geekpython.in\/connect-sqlite-database-with-flask-app#heading-homehtml\"><\/a>home.html<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:xhtml decode:true \" title=\"home.html\">{% extends \"base.html\" %}\n{% block body %}\n&lt;h1 class=\"text-center my-5\"&gt;\ud83d\ude97Vehicles\ud83d\ude97&lt;\/h1&gt;\n&lt;div class=\"container d-flex justify-content-center align-items-center\"&gt;\n    &lt;a class=\"btn btn-outline-info mb-3\" href=\"{{ url_for('add_vehicle') }}\"&gt;Add Vehicles&lt;\/a&gt;\n&lt;\/div&gt;\n&lt;div class=\"container\"&gt;\n    &lt;table class=\"table table-dark table-striped\"&gt;\n        &lt;thead&gt;\n        &lt;tr&gt;\n            &lt;th scope=\"col\"&gt;ID&lt;\/th&gt;\n            &lt;th scope=\"col\"&gt;Vehicle&lt;\/th&gt;\n            &lt;th scope=\"col\"&gt;Price(INR)&lt;\/th&gt;\n            &lt;th scope=\"col\"&gt;Created on&lt;\/th&gt;\n        &lt;\/tr&gt;\n        &lt;\/thead&gt;\n        {% if not details%}\n        &lt;div class=\"text-center\"&gt;\n            &lt;h3 class=\"my-5\"&gt;No Records to Display!&lt;\/h3&gt;\n        &lt;\/div&gt;\n        {% else %}\n        &lt;tbody&gt;\n        {% for data in details %}\n        &lt;tr&gt;\n            &lt;th scope=\"row\"&gt;{{data.id}}&lt;\/th&gt;\n            &lt;td&gt;{{data.name}}&lt;\/td&gt;\n            &lt;td&gt;\u20b9{{data.price}}&lt;\/td&gt;\n            &lt;td&gt;{{data.created_at}}&lt;\/td&gt;\n        &lt;\/tr&gt;\n        {% endfor %}\n        &lt;\/tbody&gt;\n        {% endif %}\n    &lt;\/table&gt;\n&lt;\/div&gt;\n{% endblock %}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-vehiclehtml\"><a href=\"https:\/\/geekpython.in\/connect-sqlite-database-with-flask-app#heading-vehiclehtml\"><\/a>vehicle.html<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:xhtml decode:true \" title=\"vehicle.html\">{% extends \"base.html\" %}\n{% block body %}\n&lt;h1 class=\"text-center my-5\"&gt;\ud83d\ude97Vehicle Details\ud83d\ude97&lt;\/h1&gt;\n&lt;div class=\"container\"&gt;\n    &lt;a class=\"btn mb-3 btn-outline-info\" href=\"{{ url_for('home') }}\"&gt;Go to Home&lt;\/a&gt;\n    &lt;form action=\"\/add-vehicle\" method=\"POST\"&gt;\n        &lt;div class=\"mb-3\"&gt;\n            &lt;label class=\"form-label\" for=\"vehicle\"&gt;Vehicle&lt;\/label&gt;\n            &lt;input class=\"form-control\" id=\"vehicle\" name=\"vehicle\" placeholder=\"Vehicle name with model and brand\"\n                   required type=\"text\"&gt;\n        &lt;\/div&gt;\n        &lt;div class=\"mb-3\"&gt;\n            &lt;label class=\"form-label\" for=\"price\"&gt;Price&lt;\/label&gt;\n            &lt;input class=\"form-control\" id=\"price\" name=\"price\" placeholder=\"Price of the vehicle in INR\" \n                   required type=\"text\"&gt;\n        &lt;\/div&gt;\n        &lt;button class=\"btn mt-3 btn-outline-success\" type=\"submit\"&gt;Add Vehicle&lt;\/button&gt;\n    &lt;\/form&gt;\n&lt;\/div&gt;\n\n{% endblock%}<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-creating-routes-and-logic\"><a href=\"https:\/\/geekpython.in\/connect-sqlite-database-with-flask-app#heading-creating-routes-and-logic\"><\/a>Creating Routes and Logic<\/h1>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"app.py\">\"\"\" app.py file \"\"\"\n# Previous Code Here\n\n\"\"\" Creating Routes \"\"\"\n@app.route(\"\/\")\ndef home():\n    details = Vehicle.query.all()\n    return render_template(\"home.html\", details=details)\n\n@app.route(\"\/add-vehicle\", methods=['GET', 'POST'])\ndef add_vehicle():\n    if request.method == 'POST':\n        v_name = request.form.get('vehicle')\n        price = request.form.get('price')\n\n        add_detail = Vehicle(\n            name=v_name,\n            price=price\n        )\n        db.session.add(add_detail)\n        db.session.commit()\n        return redirect(url_for('home'))\n\n    return render_template(\"vehicle.html\")\n\nif __name__ == \"__main__\":\n    from models import Vehicle\n    create_db()\n    app.run(debug=True) # This line of code is added in this section<\/pre><\/div>\n\n\n\n<p>First of all, add the necessary imports in the&nbsp;<code>app.py<\/code>&nbsp;file:&nbsp;<code>render_template<\/code>,&nbsp;<code>request<\/code>,&nbsp;<code>redirect<\/code>, and&nbsp;<code>url_for<\/code>.<\/p>\n\n\n\n<p>The provided code establishes two distinct routes: the&nbsp;<code>home<\/code>&nbsp;or&nbsp;<code>\"\/\"<\/code>&nbsp;route and the&nbsp;<code>\/add-vehicle<\/code>&nbsp;route.<\/p>\n\n\n\n<p>The main page route (<code>@app.route(\"\/\")<\/code>) uses a function called&nbsp;<code>home()<\/code>. This function retrieves all vehicle information from the database using the&nbsp;<code>Vehicle<\/code>&nbsp;model and shows it on the&nbsp;<code>home.html<\/code>&nbsp;page.<\/p>\n\n\n\n<p>The&nbsp;<code>\/add-vehicle<\/code>&nbsp;route can handle both&nbsp;<code>GET<\/code>&nbsp;and&nbsp;<code>POST<\/code>&nbsp;requests. The&nbsp;<code>add_vehicle()<\/code>&nbsp;function is responsible for displaying the&nbsp;<code>vehicle.html<\/code>&nbsp;page. It also handles the process of taking user data from a submitted form, adding it to the database using&nbsp;<code>db.session.add()<\/code>, confirming the changes with&nbsp;<code>db.session.commit()<\/code>, and sending the user back to the main page through&nbsp;<code>redirect(url_for('home'))<\/code>.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-testing\"><a href=\"https:\/\/geekpython.in\/connect-sqlite-database-with-flask-app#heading-testing\"><\/a>Testing<\/h1>\n\n\n\n<p>Run the Flask app directly from the IDE or as a Python module to see the app hosted on&nbsp;<code>http:\/\/127.0.0.1:5000<\/code>, or manually enter&nbsp;<code>http:\/\/localhost:5000<\/code>&nbsp;in the browser&#8217;s address bar.<\/p>\n\n\n\n<p>The following image shows the first glimpse of the homepage.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1366\" height=\"448\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/homepage.png\" alt=\"Homepage\" class=\"wp-image-1186\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/homepage.png 1366w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/homepage-300x98.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/homepage-1024x336.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/homepage-768x252.png 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/figure>\n\n\n\n<p>To add vehicle information, use the&nbsp;<code>\"Add Vehicles\"<\/code>&nbsp;button or the&nbsp;<code>\/add-vehicle<\/code>&nbsp;route.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1365\" height=\"468\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/vehicle-data.png\" alt=\"Adding vehicle details\" class=\"wp-image-1187\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/vehicle-data.png 1365w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/vehicle-data-300x103.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/vehicle-data-1024x351.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/vehicle-data-768x263.png 768w\" sizes=\"auto, (max-width: 1365px) 100vw, 1365px\" \/><\/figure>\n\n\n\n<p>After you submit the form, the vehicle information will be displayed on the homepage, to which you will be automatically redirected.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1366\" height=\"361\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/dataa-added.png\" alt=\"Added data showing on homepage\" class=\"wp-image-1188\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/dataa-added.png 1366w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/dataa-added-300x79.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/dataa-added-1024x271.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/dataa-added-768x203.png 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-source-code\"><a href=\"https:\/\/geekpython.in\/connect-sqlite-database-with-flask-app#heading-source-code\"><\/a>Source Code<\/h1>\n\n\n\n<p>Access the full source code written in this article from the following GitHub repository and clone or download it and run the application to try it out.<\/p>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/github.com\/Sachin-crypto\/FlaskxSQLiteDB_Vehicle_App\" rel=\"noreferrer noopener\">FlaskxSQLiteDB_Vehicle_App<\/a><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/connect-sqlite-database-with-flask-app#heading-conclusion\"><\/a>Conclusion<\/h1>\n\n\n\n<p>SQLAlchemy is used to create an SQLite database and integrated with the Flask app to interact with the database. A simple application was created in which a form is integrated to get the data from the user and add it to the database and then display it on the homepage of the application.<\/p>\n\n\n\n<p>This article has walked you through the steps in making the SQLite database using Flask-SQLAlchemy and integrating it with the Flask app.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might be interested in if you liked this one<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/integrate-postgresql-database-in-python\" rel=\"noreferrer noopener\">How to connect the PostgreSQL database with Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/database-in-appwrite-using-python\" rel=\"noreferrer noopener\">How to create a database in Appwrite using Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/render-images-from-flask\" rel=\"noreferrer noopener\">Upload and display images on the frontend using Flask in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/flask-app-for-image-recognition\" rel=\"noreferrer noopener\">Building a Flask image recognition webapp using a deep learning model<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/merging-tailwindcss-into-flask-apps\" rel=\"noreferrer noopener\">How to Integrate TailwindCSS with Flask<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/access-modifiers-in-python\" rel=\"noreferrer noopener\">What are Public, Protected, and Private access modifiers in Python<\/a>?<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article will guide you step by step in making a database using&nbsp;Flask-SQLAlchemy. It will show you how to work with an&nbsp;SQLite database&nbsp;in your&nbsp;Flask app, and then how to&nbsp;make a form on the website to collect user information and put it into the database. Installing Flask-SQLAlchemy Lib Flask-SQLAlchemy uses&nbsp;SQLAlchemy, a powerful ORM (object-relational mapping) library [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1185,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2,37,5],"tags":[38,36,12,31],"class_list":["post-1183","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-database","category-flask","tag-database","tag-flask","tag-python","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=1183"}],"version-history":[{"count":5,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1183\/revisions"}],"predecessor-version":[{"id":1645,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1183\/revisions\/1645"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/1185"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=1183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=1183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=1183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}