0

This is my code:

import mysql.connector 
mydb = mysql.connector.connect(host='localhost', 
                        database='glabo', 
                        user='root', 
                        password='.........') 
mycursor = mydb.cursor()
mycursor.execute("INSERT INTO orders (o_userid, o_labo, o_jour, o_heure) SELECT users.user_id, labos.l_id, jours.j_id, heures.h_id FROM users, labos, jours, heures WHERE users.user_name = 'Kbayad' AND labos.l_nom = 'LABO 1' AND jours.j_jour = 'Mardi' AND heures.h_heure = '10-12';")

After running this python code, I don't get any error but nothing changes in the database

7
  • maybe a commit? Commented Jun 18, 2024 at 21:33
  • What does it mean? Commented Jun 18, 2024 at 21:35
  • Are you sure the INSERT's SELECT returns any data? Try INSERT with VALUES instead. Commented Jun 18, 2024 at 21:35
  • 1
    Tip of today: Switch to modern, explicit JOIN syntax. Easier to write (without errors), easier to read (and maintain), and easier to convert to outer join if needed. (Not the answer, but still a good advice.) Commented Jun 18, 2024 at 21:37
  • 1
    check any tutorial, for ex here and search for "commit" Commented Jun 18, 2024 at 21:37

1 Answer 1

1

this is probably because MySQL transactions in Python are not auto-committed.

in MySQL, when you execute a query that modifies data (like INSERT), those changes are not automatically saved (or "committed") to the database.

you need to explicitly tell the database to save those changes by using the commit() method.

import mysql.connector

mydb = mysql.connector.connect(
    host='localhost', 
    database='glabo', 
    user='root', 
    password='.........'
)

mycursor = mydb.cursor()

mycursor.execute("""
    INSERT INTO orders (o_userid, o_labo, o_jour, o_heure) 
    SELECT users.user_id, labos.l_id, jours.j_id, heures.h_id 
    FROM users, labos, jours, heures 
    WHERE users.user_name = 'Kbayad' 
    AND labos.l_nom = 'LABO 1' 
    AND jours.j_jour = 'Mardi' 
    AND heures.h_heure = '10-12';
""")

mydb.commit()
mycursor.close()
mydb.close()
Sign up to request clarification or add additional context in comments.

1 Comment

OOH THANKS IT'S WORKING NOW THANK YOU A LOT!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.