MySQL не подключается(MySQL Connection not available.)

Есть код:

from mysql.connector import connect, Error
import csv

with connect(
    host="localhost",
    user='plat',
    password='1233'
) as connection:
    try:
        drop_db = "DROP DATABASE IF EXISTS movie"
        create_db = """CREATE DATABASE movie;
        USE movie"""
        drop_table_movies = "DROP TABLE IF EXISTS movies"
        create_table_movies = """
            CREATE TABLE movies(
            movieId INT PRIMARY KEY,
            title VARCHAR(100),
            genre VARCHAR(100)
            )"""

        with connection.cursor() as cursor:
            cursor.execute(drop_db)
            cursor.execute(create_db)
            cursor.execute(drop_table_movies)
            cursor.execute(create_table_movies)
            connection.commit()
            result = cursor.fetchall()

    except Error as e:
        print(e)

    try:
        with open("movies.csv") as csv_file:
            reader = csv.DictReader(csv_file, delimiter=",")
            for row in reader:
                values = [
                    row["movieId"],
                    row["title"],
                    row["genres"]
                ]
                insert_movies = """INSERT INTO movies (movieId, title, genres) 
                VALUES (%s, %s, %s)""", values
                with connection.cursor() as cursor:
                    cursor.execute(insert_movies)
                    result = cursor.fetchall()
                    for line in result:
                        print(line)
    except Error as e:
        print(e)
cursor.close()

В нем я создаю бд, таблицу и закидываю данные из csv файла, но выскакивает ошибка:

2014 (HY000): Commands out of sync; you can't run this command now
MySQL Connection not available.

В чем моя ошибка?

Заранее большое Спасибо!


Ответы (0 шт):