8/9/2023 0 Comments Python web app sqlite tutorial![]() ![]() The table showing the sample data will be displayed. Go back to the home page and click ‘Show List’ link. The underlying function inserts the record in the students table. Visit in browser which displays a simple menu like this −Ĭlick ‘Add New Record’ link to open the Student Information Form.įill the form fields and submit it. Run this script from Python shell and as the development server starts running. Return render_template("result.html",msg = list(): Here is the complete code of Flask-SQLite application.įrom flask import Flask, render_template, request This list.html is a template, which iterates over the row set and renders the data in an HTML table.įinally, the ‘/’ URL rule renders a ‘home.html’ which acts as the entry point of the home(): In this tutorial, you will create a database of Monty Python movies using basic sqlite3 functionality. Return render_template("list.html",rows = rows) This object is passed to the list.html list(): It populates ‘rows’ as a MultiDict object containing all records in the students table. The application contains another list() function represented by ‘/list’ URL. The HTML script of result.html contains an escaping statement that displays the result of Insert operation. Return render_template("result.html",msg = msg) Message corresponding to success or error in insert operation is rendered to = )Ĭur.execute("INSERT INTO students (name,addr,city,pin) This addrec() function retrieves the form’s data by POST method and inserts in students table. The HTML script for ‘student.html’ is as follows −Īs it can be seen, form data is posted to the ‘/addrec’ URL which binds the addrec() function. It renders an HTML file containing student information new_student(): ![]() Our Flask application has three View functions.įirst new_student() function is bound to the URL rule (‘/addnew’). In this section we shall see how a Flask application interacts with SQLite.Ĭreate an SQLite database ‘database.db’ and create a students’ table in it.Ĭonn.execute('CREATE TABLE students (name TEXT, addr TEXT, city TEXT, pin TEXT)') For a detailed tutorial on using SQLite database in Python, please refer to this link. SQlite3 module is shipped with Python distribution. Now the webapp should also show the list of logged users.Python has an in-built support for SQlite. If you complete and submit the form, the results will be also reflected in the terminal (pay attention at the POST method): * Running on (Press CTRL+C to quit)ġ27.0.0.1 - "POST / HTTP/1.1" 200. Open your browser and visit 0.0.0.0:5000 (or 127.0.0.1:5000), the html will render and show up the login form. Output : * Running on (Press CTRL+C to quit) To run the Flask app (server) type into your terminal: python main.py All together: Testing and explaining data's flow Note: The host parameter can be changed to 127.0.0.1 (localhost) instead of 0.0.0.0. The Database Handler (models.py) methods are called when main.py has to acces and/or modify the database: dbHandler.insertUser(username, password), users = dbHandler.retrieveUsers(). This app makes database calls in order to insert new users and to return the logged users list requested by the POST method. Develop Python applications with SQLite database. Return render_template( 'index.html', users = users)Īpp. route( '/', methods =) def home():ĭbHandler. In the case of a POST request (created after the form submission) it returns the list of logged users.įrom flask import Flask from flask import render_template from flask import request import models as dbHandler app = Flask( app. ![]() In this guide, well see how to connect to a database, create tables. If the HTTP request is GET, the app returns the HTML form. Instead, we only need to import the built-in Python library sqlite3 to use this database. This application works with a single flask function/route/url (views for convention) which answers to two types of HTTP methods (GET, POST). Flask Appįlask is a python microframework intended for the developement of web applications (server side). The html contains a simple html form with two text type inputs: username and password, and a submit type input (Login button). It uses the Flask template engine to render the 'logged users' list. The html code used for this app can be found in index.html file (in this same git). We'll later call these methods from main.py to handle the database. execute( "SELECT username, password FROM users") execute( "INSERT INTO users (username,password) VALUES (?,?)", ( username, password))Ĭur. Import sqlite3 as sql def insertUser( username, password):Ĭur. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |