Here i am defining how to create a Python-Flask application using database Sqlite3.First we should know what is Flask?What is the use of Flask?
Flask is a framework in python for Web development.It has specific structure.
Folders should be like this structure.
Flaskapp /
/ templates / # contains html files
/ statics / # contains js,images,css
/ static / css
/ .py files # main files for application routing and database management
Flask can be install using:
sudo pip install Flask
In a python-Flask application, a routing python program is needed which determines the url routing.We can define sql query inside this or outside as a single python file.
To start the routing program,let route.py. First an instance of Flask is to be created inside route.py
app=Flask(__name__)
Then defines the routing functions.Here i am showing how does this works:
Let me define how the html and python program interacts:
we want to do a python function or change url when a button in html press.This done by
html
<a href='/post'>button name</a>
Here button name will see as a link,but it can modify as button using css
route.py
@app.route('/post')
def func():
-----------
-----------
Here it is...when you click the link that function works.
Here created blog.db (database name),created table posts
I think this much knowledge is needed for accessing database.you can refer the codes through the link i am added in last.
To view program code: Click here
Flask is a framework in python for Web development.It has specific structure.
Folders should be like this structure.
Flaskapp /
/ templates / # contains html files
/ statics / # contains js,images,css
/ static / css
/ .py files # main files for application routing and database management
Flask can be install using:
sudo pip install Flask
In a python-Flask application, a routing python program is needed which determines the url routing.We can define sql query inside this or outside as a single python file.
To start the routing program,let route.py. First an instance of Flask is to be created inside route.py
app=Flask(__name__)
Then defines the routing functions.Here i am showing how does this works:
Let me define how the html and python program interacts:
we want to do a python function or change url when a button in html press.This done by
html
<a href='/post'>button name</a>
Here button name will see as a link,but it can modify as button using css
route.py
@app.route('/post')
def func():
-----------
-----------
Here it is...when you click the link that function works.
Accessing Database:
We can add separate python file for creating database.
let datadb.py
To create sqlite3 database
import sqlite3 as lite
import sys
con=lite.connect("blog.db")
with con:
cur=con.cursor()
cur.execute("DROP TABLE IF EXISTS posts")
cur.execute("CREATE TABLE posts(id integer primary key autoincrement,title TEXT,text INT)")
import sys
con=lite.connect("blog.db")
with con:
cur=con.cursor()
cur.execute("DROP TABLE IF EXISTS posts")
cur.execute("CREATE TABLE posts(id integer primary key autoincrement,title TEXT,text INT)")
I think this much knowledge is needed for accessing database.you can refer the codes through the link i am added in last.
To view program code: Click here
No comments:
Post a Comment