skip to Main Content

Trying to do a shopping cart in telegram bot using psycopg2 and aiogram, but the data does not appear in the "korzina" column
code:

connection.cursor(f'INSERT INTO bot_users(korzina) VALUES({id_tovara}) FROM bot_user WHERE user_id=           {us_id}')
    connection.commit()

types of column’s:

user_id      | bigint 
korzina      | text

I’m using postgresql.

Nothing happend with "korzina" column. All data write correct (name of column’s and table name)

2

Answers


  1. You are calling connection.cursor with a very long name parameter, which is most likely not what you had in mind.

    You probably want to create an unnamed cursor object, and then call its execute method:

    c = connection.cursor()
    c.execute("INSERT INTO blabla ... ")
    

    You may also want to look at the behaviour of connections and cursors when used as context managers.

    Login or Signup to reply.
  2. The INSERT INTO command does not include the FROM clause, you should instead modify your query to use UPDATE, something like this;

    sql_query = "UPDATE bot_users SET korzina = %s WHERE user_id = %s"
    cursor.execute(sql_query, (id_tovara, us_id))
    connection.commit()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search