简体   繁体   English

从 csv 文件导入到 mysql 时不断出现错误

[英]Keep getting errors while importing from csv file to mysql

import csv
import mysql.connector as mysql

marathons = []

with open ("marathon_results.csv") as file:
    data = csv.reader(file)
    next(data)
    for rij in data:
        year = rij[0],
        winner = rij[1],
        gender = rij[2],
        country = rij[3],
        time = rij[4],
        marathon = rij[5],
        marathons.append((year, winner, gender, country, time, marathon))

conn = mysql.connect(
     host="localhost",
     user="root",
     password=""
)

c = conn.cursor()

create_database_query = 'CREATE DATABASE IF NOT EXISTS marathon_file'
c.execute(create_database_query)

c.execute('USE marathon_file')
c.execute("""CREATE TABLE IF NOT EXISTS winners(
                year INT(100),
                winner VARCHAR(255),
                gender VARCHAR(255),
                country VARCHAR(255),
                time TIME,
                marathon VARCHAR(255)
                )
            """)
print('CSV-bestand in de MySQL-database aan het laden...')

insert_query = "INSERT INTO winners(year, winner, gender, country, time, marathon) VALUES (%s, %s, %s, %s, %s, &s);"

c.executemany(insert_query, marathons)
c.commit()

print('Bestand succesvol geladen!')

So i have this code above trying to get a certain.csv file from my venv to mysql. made a list from the data and skipped the first line (since those were headers) and tried to import it to mysql. But i keep getting the following Error:所以我上面的这段代码试图从我的 venv 获取某个 .csv 文件到 mysql。从数据中列出并跳过第一行(因为那些是标题)并尝试将其导入到 mysql。但我不断得到以下错误:

CSV-bestand in de MySQL-database aan het laden...
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/conversion.py in to_mysql(self, value)
    179         try:
--> 180             return getattr(self, "_{0}_to_mysql".format(type_name))(value)
    181         except AttributeError:

AttributeError: 'MySQLConverter' object has no attribute '_tuple_to_mysql'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/cursor.py in _process_params(self, params)
    430 
--> 431             res = [to_mysql(i) for i in res]
    432             res = [escape(i) for i in res]

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/cursor.py in <listcomp>(.0)
    430 
--> 431             res = [to_mysql(i) for i in res]
    432             res = [escape(i) for i in res]

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/conversion.py in to_mysql(self, value)
    181         except AttributeError:
--> 182             raise TypeError("Python '{0}' cannot be converted to a "
    183                             "MySQL type".format(type_name))

TypeError: Python 'tuple' cannot be converted to a MySQL type

During handling of the above exception, another exception occurred:

ProgrammingError                          Traceback (most recent call last)
/var/folders/yc/mz4bq04s7wngrglphldwpwfc0000gn/T/ipykernel_17482/929148642.py in <module>
     38 insert_query = "INSERT INTO winners(year, winner, gender, country, time, marathon) VALUES (%s, %s, %s, %s, %s, &s);"
     39 
---> 40 c.executemany(insert_query, marathons)
     41 c.commit()
     42 

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/cursor.py in executemany(self, operation, seq_params)
    665                 self._rowcount = 0
    666                 return None
--> 667             stmt = self._batch_insert(operation, seq_params)
    668             if stmt is not None:
    669                 self._executed = stmt

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/cursor.py in _batch_insert(self, operation, seq_params)
    607                         tmp, self._process_params_dict(params))
    608                 else:
--> 609                     psub = _ParamSubstitutor(self._process_params(params))
    610                     tmp = RE_PY_PARAM.sub(psub, tmp)
    611                     if psub.remaining != 0:

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/cursor.py in _process_params(self, params)
    433             res = [quote(i) for i in res]
    434         except Exception as err:
--> 435             raise errors.ProgrammingError(
    436                 "Failed processing format-parameters; %s" % err)
    437         else:

ProgrammingError: Failed processing format-parameters; Python 'tuple' cannot be converted to a MySQL type

I probably missed some () or brackets or am i missing something else?我可能错过了一些 () 或括号,或者我错过了其他东西吗? Thanks谢谢

The problem is in these lines:问题出在这些行中:

        year = rij[0],
        winner = rij[1],
        gender = rij[2],
        country = rij[3],
        time = rij[4],
        marathon = rij[5],

The trailing commas cause year , winner , gender and so on to be created as 1-tuples.尾随逗号导致yearwinnergender等被创建为 1 元组。 It's the same as writing这和写作一样

        year = (rij[0],)
        winner = (rij[1],)
        # and so on...

Delete the trailing commas and try again.删除尾随逗号并重试。

Your sql comad had a & instead of a %.你的 sql comad 有一个 & 而不是 %。

I additionally simplified the data loop我还简化了数据循环

import csv
import mysql.connector as mysql

marathons = []


with open ("test2.csv") as file:
    data = csv.reader(file)
    next(data)
    marathons =  [tuple(row) for row in data]

conn = mysql.connect(
     host="localhost",
     user="root",
     password=""
)

c = conn.cursor()

create_database_query = 'CREATE DATABASE IF NOT EXISTS marathon_file'
c.execute(create_database_query)

c.execute('USE marathon_file')
c.execute("""CREATE TABLE IF NOT EXISTS winners(
                year INT(100),
                winner VARCHAR(255),
                gender VARCHAR(255),
                country VARCHAR(255),
                time TIME,
                marathon VARCHAR(255)
                )
            """)
print('CSV-bestand in de MySQL-database aan het laden...')

insert_query = "INSERT INTO winners(year, winner, gender, country, time, marathon) VALUES (%s, %s, %s, %s, %s, %s);"

c.executemany(insert_query, marathons)
conn.commit()

print('Bestand succesvol geladen!')

You dont have to write code in order to import a csv file into mysql.您不必编写代码即可将 csv 文件导入 mysql。 Once the table is defined you can use LOAD DATA in order to import the data.定义表后,您可以使用LOAD DATA导入数据。

See here for more.请参阅此处了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM