Python Forum
Error 1064 (42000) when executing UPDATE SQL
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error 1064 (42000) when executing UPDATE SQL
#1
Good afternoon:

I am trying to run an UPDATE query in SQL from python. I think the statement is correct, but it constantly returns an error. I do not know what to do...

---------------------------------------------------------
UPDATE ps_product_lang SET description = '--', name = 'Ratón óptico Standard USB' WHERE id_product = 278335 AND id_lang = 1;
---------------------------------------------------------
this is the sql i am using


Can somebody help me?

Thank you so much!
Reply
#2
sql_actualizar_ficha_es = "UPDATE ps_product_lang SET description = '--', name = '" + titulo + "' WHERE id_product = " + id_producto + " AND id_lang = 1;"
    sql_actualizar_ficha_pt = "UPDATE ps_product_lang SET description = '--', name = '" + titulo_pt + "' WHERE id_product = " + id_producto + " AND id_lang = 2;"
    sql_registrar_id_producto = "INSERT INTO fichas_actualizadas (id_product) VALUES ('" + id_producto + "');"   

    connection = create_connection("######", "######", "########", "######") #
    
    consulta1 = execute_query(connection, sql_actualizar_ficha_es) 
    consulta2 = execute_query(connection, sql_actualizar_ficha_pt)
buran write May-22-2023, 07:47 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#3
Which database are you using? Please show us your program and the complete error message.
You are calling execute_query(). Where is it defined?
Reply
#4
(May-22-2023, 07:18 AM)ibreeden Wrote: Which database are you using? Please show us your program and the complete error message.
You are calling execute_query(). Where is it defined?


Hi ibreeden!

I am using MariaDB.

def create_connection(host_name, user_name, user_password, db_name):
    connection = None
    try:
        connection = mysql.connector.connect(
            host=host_name,
            user=user_name,
            passwd=user_password,
            database=db_name
        )
        
    except Error as e:
        falla_conexion = 1
        print("lo que esta fallando es la conexion")
       

    return connection

def execute_query(connection, query):
    cursor = connection.cursor()
    try:
        cursor.execute(query)
        print("La consulta se ejecutó correctamente.")

        return 1
    except Error as e:
        print("Hubo un error al ejecutar la consulta: " + str(e))
        return 0
    finally:
        cursor.close()

def execute_read_query(connection, query):
    cursor = connection.cursor()
    result = None
    try:
        cursor.execute(query)
        result = cursor.fetchall()
        return len(result)
    except Error as e:
        print("The error consulta numerica occurred")



sql_actualizar_ficha_es = "UPDATE ps_product_lang SET description = '--', name = '" + titulo + "' WHERE id_product = " + id_producto + " AND id_lang = 1;"
    sql_actualizar_ficha_pt = "UPDATE ps_product_lang SET description = '--', name = '" + titulo_pt + "' WHERE id_product = " + id_producto + " AND id_lang = 2;"
    sql_registrar_id_producto = "INSERT INTO fichas_actualizadas (id_product) VALUES ('" + id_producto + "');"   

    connection = create_connection("?????", "¿¿¿¿¿", "???????", "???????") 
    
    consulta1 = execute_query(connection, sql_actualizar_ficha_es) 
    consulta2 = execute_query(connection, sql_actualizar_ficha_pt) 

    print("-----------------------------------------")
    print(sql_actualizar_ficha_es)
    if consulta1 == 1 and consulta2 == 1 :
        consulta3 = execute_query(connection, sql_registrar_id_producto) 
The error output that appears when running the script is:

Hubo un error al ejecutar la consulta: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
Hubo un error al ejecutar la consulta: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
-----------------------------------------
UPDATE ps_product_lang SET description = '--', name = 'Teclado Value con cable' WHERE id_product = 278333 AND id_lang = 1;

Something that really catches my attention is that if I take the same sql query that I'm trying to execute in the script with the error and paste it into phpmyadmin, it runs without any problem.

I am completely lost.....

Thank you so much!
Reply
#5
That is weird, everything seems to be correct.
Perhaps you should not use the semicolon ( ; ) at the end. The semicolon is primarily used as a statement terminator in client mode. I think you do not need it in this case because the string just ends which is a clear ending of the query.
Reply
#6
Yeah. I had also thought about it and tried with ";" and without ";". But nothing, it looks like the same error... I'll keep testing until I find the error...

Thank you very much for your help. If I find the solution I will reply with it!
Reply
#7
Solved!!!!!! It was the mixture of 2 errors:

On the one hand, the product_id was a text string that had characters that were not numbers.

On the other hand I had forgotten to use connection.commit() in the function execute_query()

Finally solved... Thank you very much!
ibreeden likes this post
Reply
#8
You shouldn't be using string concatenation to construct SQL statements
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error while executing the Python code in Linux DivAsh 8 1,623 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  error 1102 (42000) incorrect database name 's' Anldra12 4 1,716 Jun-08-2022, 09:00 AM
Last Post: Anldra12
  SSL error while trying to update conda ilango 0 2,183 Nov-04-2021, 08:38 AM
Last Post: ilango
  Eliminating error in Python update-alternatives Led_Zeppelin 2 3,008 Apr-14-2021, 12:49 PM
Last Post: Led_Zeppelin
  Facing error while executing below Python code ramu4651 1 5,741 Jan-26-2021, 06:40 PM
Last Post: ibreeden
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,698 Dec-16-2020, 05:26 AM
Last Post: Vokofe
  Error when executing pytesseract to get the text from image bvdinesh 3 17,936 Oct-19-2019, 05:01 PM
Last Post: cwco00
  pymysql.err.ProgrammingError : 1064 You have an error in your SQL syntax pradeepkumarbe 1 12,563 Jul-01-2019, 08:05 AM
Last Post: pradeepkumarbe
  Error executing Jupyter command 'notebook': [Errno 'jupyter-notebook' not found] 2 Newtopython123 10 31,347 Apr-25-2019, 07:30 AM
Last Post: banu0395
  Cannot update or install anything with pip, SSL error nickystixx45 6 178,728 Mar-26-2019, 03:42 AM
Last Post: nickystixx45

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020