@denislysenko
data engineer

Как заполнить таблицу sql, из файла csv используя cursor.execute?

В бд есть таблица movies

есть csv файл movies.csv на 10 000 строк :
movieId,title,genres
1,Toy Story (1995),Adventure|Animation|Children|Comedy|Fantasy
2,Jumanji (1995),Adventure|Children|Fantasy
3,Grumpier Old Men (1995),Comedy|Romance
4,Waiting to Exhale (1995),Comedy|Drama|Romance
5,Father of the Bride Part II (1995),Comedy
6,Heat (1995),Action|Crime|Thriller
7,Sabrina (1995),Comedy|Romance
8,Tom and Huck (1995),Adventure|Children
9,Sudden Death (1995),Action
10,GoldenEye (1995),Action|Adventure|Thriller
11,"American President, The (1995)",Comedy|Drama|Romance
12,Dracula: Dead and Loving It (1995),Comedy|Horror
13,Balto (1995),Adventure|Animation|Children
14,Nixon (1995),Drama
...

вот код:
import mysql.connector
from mysql.connector import Error
import re
import csv


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
        )
        print("Connection to MySQL DB successful")
    except Error as e:
        print(f"The error '{e}' occurred")

    return connection

def create_table(connection, sql_script_path):
    with connection.cursor() as cursor:
        with open(sql_script_path) as file:
            script = file.read()
            cursor.execute(script)
        connection.commit()


def fill_table_with_movies(connection, movies_csv_file, sql_script_path):
        for movie_id, title, genres in  movies_csv_file[1302:]:
            if re.search(r'\(\d{4}\)', title) is not None:
                year = re.search(r'\(\d{4}\)', title)
                a = year.group(0)[1:-1]
                int_year = int(a)
            else:
                int_year = 0

            title = title.replace("'", '`')
            title = title.replace('"', '`')

            with connection.cursor() as cursor:
                with open(sql_script_path) as file:
                    filling_script = file.read()
                    cursor.execute(filling_script.format(int(movie_id), title[:-6], int_year))
                connection.commit()


def read_movies_file():
    data_m = []
    with open('./files/csv/movies.csv', encoding='utf-8') as file:
        reader = csv.reader(file, delimiter=',')
        for row in reader:
            data_m.append(row)
    data_movies = data_m[1:]
    return data_movies


# read ratings.csv
def read_rating_file():
    data_r = []
    with open('./files/csv/ratings.csv', encoding='utf-8') as file:
        reader = csv.reader(file, delimiter=',')
        for row in reader:
            data_r.append(row)
    data_rating = data_r[1:]
    return data_rating







def main():
    #create_connection_and_create_database("localhost", "root", "12345678", './files/sql/CREATE_DATABASE_movies_db.sql')
    connection = create_connection("localhost", "root", "12345678", 'movies_db')
    #create_table(connection, './files/sql/USE_DATABASE_movies_db.sql')
    #create_table(connection, './files/sql/CREATE_TABLE_movies.sql')
    #create_table(connection, './files/sql/CREATE_TABLE_rating.sql')
    #create_table(connection, './files/sql/CREATE_TABLE_genres.sql')
    data_movies = read_movies_file()
    data_rating = read_rating_file()
    fill_table_with_movies(connection, data_movies, './files/sql/FILL_MOVIES_TABLE.sql')


main()


когда запускаю файл питона, то в воркбенче эта таблица заполняется только до 1301 строки, но в этом файле 10 000 строк и мне нужно импортировать все эти 10 000 строк в таблицу movies

Почему данные ипортируются только до 1301 строки? Как это исправить?

вот как выглядит запрос на создание таблицы movies:
CREATE TABLE movies (
movie_id        INT,
title           VARCHAR(256),
year            INT,
PRIMARY KEY     (movie_id)
);


вот как выглядит запрос на на добавление данных в таблицу movies:
INSERT   IGNORE   INTO      movies     (movie_id, title, year)
VALUES                                 ({}, '{}', {})


Вот как выглядит сроки рядом с 1301 строкой в csv файле:
1298,Pink Floyd: The Wall (1982),Drama|Musical
1299,"Killing Fields, The (1984)",Drama|War
1300,My Life as a Dog (Mitt liv som hund) (1985),Comedy|Drama
1301,Forbidden Planet (1956),Drama|Sci-Fi
1302,Field of Dreams (1989),Children|Drama|Fantasy
1303,"Man Who Would Be King, The (1975)",Adventure|Drama
1304,Butch Cassidy and the Sundance Kid (1969),Action|Western
1305,"Paris, Texas (1984)",Drama|Romance
  • Вопрос задан
  • 228 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы