[Python] selecting data from postgresql using flask and python

Discussão em 'Python' iniciado por Stack, Março 17, 2025.

  1. Stack

    Stack Membro Participativo

    let us suppose we have following database : [​IMG]

    if i select only HP rows , it works fine : [​IMG]

    but what i want is to use flask to read the product name from userform and return database using python read_sql, so here is my code :

    from flask import Flask,render_template,request
    import psycopg2 as pg
    import psycopg2
    import pandas as pd
    app =Flask('app')
    @app.route('/')
    def hello_database():
    return render_template('index.html',name='database')

    @app.route('/Search',methods =['POST','GET'])
    def Search():
    name =request.form['name_input']
    engine = pg.connect("dbname='postgres' user='postgres' host='localhost' port='5432' password='Saqartvelo1989'")
    df = pd.read_sql(f'select * from Leptop where name={name}', con=engine)
    print(df)

    return render_template('index.html', name=name)

    app.run()


    and result is :

    [​IMG]

    but when i type HP, it returns following mistake :

    Internal Server Error
    The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.


    also pycharm returns this error :

    pandas.errors.DatabaseError: Execution failed on sql 'select * from Leptop where name=HP': column "hp" does not exist
    LINE 1: select * from Leptop where name=HP


    so it means that f format has problem right? how to solve it? please give me some hint ^

    Continue reading...

Compartilhe esta Página