top of page

Code 1: Info.py

  • Foto del escritor: Kassandra Castillo
    Kassandra Castillo
  • 3 oct 2021
  • 1 Min. de lectura
This is the file in charge of starting the application, it is the main file of the server, in this file is all the logic written in Python language, based on routes, functions and access to links for the call of other files.
from flask import Flask,render_template,request,redirect,url_for,flash 
import pymysql

con = pymysql.connect(host='localhost', port=3306, user='root', passwd='coronita', db='information') 
cursor = con.cursor()

app = Flask(__name__)  

app.secret_key = "mysecretkey"     

@app.route('/')  
def Home():      
     
    cursor.execute("SELECT * FROM rinformation")            
    data = cursor.fetchall()                          
    con.commit()                                      
    return render_template('info.html',rinformation = data)

@app.route('/add', methods = ['POST'])           
def add_book2():                                 
    if request.method == "POST":                 
        user = str(request.form["user"])      
        time = str(request.form["time"])     
        location = str(request.form["location"])
        description = str(request.form["description"])
        samplecollected = str(request.form["samplecollected"])
        date = str(request.form["date"])                          
        sql = "INSERT INTO rinformation (user,time,location,description,samplecollected,date) VALUES (' " + user + " ',' " + time + " ',' " + location + " ',' " + description + " ',' " + samplecollected + " ',' " + date + " ') " 
        cursor.execute(sql)                       
        con.commit()                              
        flash("information added")                       
        return redirect(url_for("Home"))

@app.route('/edit/<id>')                          
def get_book(id):                                                         
    cursor.execute("SELECT * FROM rinformation WHERE id = %s", (id)) 
    data = cursor.fetchall()                                    
    return render_template("edits.html", rinformations = data[0]) 

@app.route('/update/<id>', methods = ['POST'])     
def update(id):                                     
    if request.method == "POST":                   
        user = str(request.form["user"])      
        time = str(request.form["time"])     
        location = str(request.form["location"])
        description = str(request.form["description"])
        samplecollected = str(request.form["samplecollected"])
        date = str(request.form["date"]) 
        cursor.execute("""                         
            UPDATE rinformation
            SET user = %s,
                time = %s,
                location = %s,
                description = %s,
                samplecollected = %s,
                date = %s
            WHERE id = %s
        """, (user, time, location,description,samplecollected,date, id))        
        con.commit()                               
        flash("information update")                       
        return redirect(url_for("Home"))  

@app.route('/delete/<string:id>')                  
def delete_book(id):                             
    cursor.execute("DELETE FROM rinformation WHERE id = {0}".format(id))  
    con.commit()                                   
    flash("information removed")                          
    return redirect(url_for("Home")) 

if __name__ == '__main__':                         
    app.run(debug=True)

Comentarios


Publicar: Blog2_Post
  • Facebook
  • Twitter
  • LinkedIn

©2021 por VALHALLA. Creada con Wix.com

bottom of page