pythonsqlpostgresqlpsycopg2

inserting values in database from sentiment analysis


I have little project created by my self:

user is entering some comments on website, then using flask+python we are getting those text, send it to sentiment analysis with help of transformers and all results(text, label , score) is inserted into Postgresql

this project is divided into two main part, first i have created empty table in database :

CREATE TABLE Message_sentiment (
    mytext text,
    text_label text,
    score numeric
);

Based on second part, here is my little code :

if request.method=='POST':
    text =request.form.get('user_comment')
    label =model(text)[0]['label']
    score =model(text)[0]['score']
    # print(f'{text} has  following label {label} with score {score}')
    curr.execute("""INSERT INTO message_sentiment(mytext,text_label,score) VALUES
    (text,label, score)
    """)

when i am running this code , getting following error :

UndefinedColumn
psycopg2.errors.UndefinedColumn: column "text" does not exist
LINE 2:         (text,label, score)
                 ^
HINT:  Perhaps you meant to reference the column "message_sentiment.mytext".

so i think problem is here :

 curr.execute("""INSERT INTO message_sentiment(mytext,text_label,score) VALUES
        (text,label, score)
        """)

i can't use {} brackets, please help me how to modify this line? thanks in advance


Solution

  • You should always used parameters when insert data, or querying or uodating

    curr.execute("""
        INSERT INTO message_sentiment(mytext,text_label,score)
        VALUES (%s, %s, %s);
        """,
        (text,label, score))