I am using Python to insert some columns from a pandas dataframe to MySQL. I'm using pandas 15.2. and mysql.connector.
import mysql.connector
cte = mysql.connector.connect(user=xxxxxxxxx, password=xxxxxxx, host=xxxxxx)
cursor = cte.cursor()
for index, row in pets.iterrows():
home_pets = (row['cats'], row['dogs'], row['goldfish'], row['rabbits'], row['mice'], row['parrots'])
add_pets = """
INSERT INTO pets
(cats, dogs, goldfish, rabbits, mice, parrots)
VALUES (%s, %s, %s, %s, %s, %s)
"""
cursor.execute(add_pets, home_pets)
cte.commit()
One of my integer columns has 'NaN' values in it. I've just added this and as everything was working before am sure that this is the column causing issues. This is a varchar(5) (was int(5) ) column and is not set to "NOT NULL".
Edit: This column (parrots) contains numbers 0 to 5 as responses. It also has "NaN" where the respondent didn't answer the question.
I'm getting the following error message when trying to insert the data in the table:
ProgrammingError Traceback (most recent call last)
<ipython-input-54-59a05bb50318> in <module>()
6 VALUES (%s, %s, %s, %s, %s, %s)
7 """
----> 8 cursor.execute(add_pets, home_pets)
9 cte.commit()
C:\Python27\lib\site-packages\mysql\connector\cursor.pyc in execute(self, operation, params, multi)
505 self._executed = stmt
506 try:
--> 507 self._handle_result(self._connection.cmd_query(stmt))
508 except errors.InterfaceError:
509 if self._connection._have_next_result: # pylint: disable=W0212
C:\Python27\lib\site-packages\mysql\connector\connection.pyc in cmd_query(self, query)
720 if not isinstance(query, bytes):
721 query = query.encode('utf-8')
--> 722 result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
723
724 if self._have_next_result:
C:\Python27\lib\site-packages\mysql\connector\connection.pyc in _handle_result(self, packet)
638 return self._handle_eof(packet)
639 elif packet[4] == 255:
--> 640 raise errors.get_exception(packet)
641
642 # We have a text result set
ProgrammingError: 1054 (42S22): Unknown column 'nan' in 'field list'
I saw this question but they are not using mysql.connector. Python Pandas write to sql with NaN values.
I would prefer to keep the int column as int and not have to change it to object if possible (and then go back and forth every time I read the data into Python from my MySQL db. Is this possible? Appreciate your assistance.