复制行和更改值 MySQL

Copying rows and changing values MySQL

使用下面的代码,我倾向于复制 Country = MoroccoModel = S-Class 的行。从这个复制的行中,我需要更改值 Fuel to PetrolStatus to Average.

奇怪的是,当我在 workbench 中传递此查询时它似乎有效,但是当我想在 Python 中完成它时,我收到以下错误:OperationalError: Lost connection to MySQL server at 'localhost:3306', system error: Connection not available.问题出在哪里?

import mysql.connector

mydb = mysql.connector.connect(
        host= 'localhost',
        user = 'root',
        passwd = '*****.',
        database= 'my_db'
    )
cursor = mydb.cursor(buffered = True)

q4 = ("INSERT INTO my_cars(Car, Model, Country, System, Fuel, Status) (SELECT Car, Model, Country, System,'Petrol', 'Average' FROM my_cars WHERE Model = 'S-class' AND Country= 'Morocco')")

mydb.commit()
mydb.close()
                            
cursor.execute(q4)

您在关闭数据库连接后执行了查询 (cursor.execute(q4))。 mydb.close()。关闭连接后,您将无法再使用数据库连接,除非您重新连接它。