0% found this document useful (0 votes)
131 views4 pages

Flask SQLite Integration Guide

This document provides a comprehensive guide on integrating Python, SQLite, and Flask to create a web application. It covers connecting to an SQLite database, creating a Flask app, and handling user input through an HTML form to store data in the database. Additionally, it includes routes for adding users and viewing all stored data in a table format.

Uploaded by

nabiswaj8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views4 pages

Flask SQLite Integration Guide

This document provides a comprehensive guide on integrating Python, SQLite, and Flask to create a web application. It covers connecting to an SQLite database, creating a Flask app, and handling user input through an HTML form to store data in the database. Additionally, it includes routes for adding users and viewing all stored data in a table format.

Uploaded by

nabiswaj8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python + SQLite + Flask Full Notes with HTML Form Integration

1. Connecting to SQLite in Python

---------------------------------

import sqlite3

conn = [Link]("[Link]")

cursor = [Link]()

# Create a table

[Link]('''

CREATE TABLE IF NOT EXISTS users (

id INTEGER PRIMARY KEY AUTOINCREMENT,

name TEXT NOT NULL,

age INTEGER NOT NULL

''')

[Link]()

[Link]()

2. Creating a Flask Web Application

-----------------------------------

from flask import Flask, request, render_template

import sqlite3

app = Flask(__name__)

# Function to connect to database


def get_db_connection():

conn = [Link]('[Link]')

conn.row_factory = [Link]

return conn

3. Creating a Simple HTML Form (save as 'templates/[Link]')

--------------------------------------------------------------

<html>

<body>

<h2>Add User</h2>

<form action="/add_user" method="post">

Name: <input type="text" name="name"><br><br>

Age: <input type="number" name="age"><br><br>

<input type="submit" value="Submit">

</form>

<br>

<a href="/users">View All Users</a>

</body>

</html>

4. Route to Display the Form

----------------------------

@[Link]('/')

def form():

return render_template('[Link]')

5. Route to Handle Form Submission and Store in SQLite


------------------------------------------------------

@[Link]('/add_user', methods=['POST'])

def add_user():

name = [Link]['name']

age = [Link]['age']

conn = get_db_connection()

[Link]("INSERT INTO users (name, age) VALUES (?, ?)", (name, age))

[Link]()

[Link]()

return "User added successfully! <a href='/'>Add Another</a>"

6. Route to View All Data in a Table

------------------------------------

@[Link]('/users')

def users():

conn = get_db_connection()

rows = [Link]("SELECT * FROM users").fetchall()

[Link]()

html = "<h2>All Users</h2><table border='1'><tr><th>ID</th><th>Name</th><th>Age</th></tr>"

for row in rows:

html += f"<tr><td>{row['id']}</td><td>{row['name']}</td><td>{row['age']}</td></tr>"

html += "</table><br><a href='/'>Back to Form</a>"

return html
7. Running the Flask App

------------------------

if __name__ == '__main__':

[Link](debug=True)

Explanation:

------------

- You create an SQLite database using `[Link]()`.

- Use Flask to handle web routes and show HTML forms.

- The HTML form sends data to Flask via POST.

- Flask takes that form data and inserts it into SQLite.

- You can view the stored data in an HTML table using another Flask route.

Make sure you have a 'templates' folder in the same directory as your Python file. Inside it, save the

[Link] file.

You might also like