Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error when trying to insert values into MySQL table with python

Tags:

python

mysql

I'm trying to insert this row of data (i parse from a list of tuples)

(0L, u'2012-11-06T16:23:36-05:00', 0L, None, 23759918L, u'baseline', u'0 to 100', nan, 105114L, 2009524L, True, u'charge', u'Charge')

into a mySQL database table, but i seem to have problems with the null value (NaN), because i receive this error:

OperationalError: (1054, "Unknown column 'nan' in 'field list'")

I've tried making columns nullable in mySQL, and also making all text fields, but still no dice..

like image 852
Matt Avatar asked Nov 06 '12 21:11

Matt


People also ask

Is Python compatible with MySQL?

Connector/Python does not support the old MySQL Server authentication methods, which means that MySQL versions prior to 4.1 will not work. On macOS x86_64 ARM: Python 3.7 is not supported with the c-ext implementation; note this is a non-default version of Python on macOS.

How could you write a Pandas Dataframe in Python to a MySQL table?

Create a dataframe by calling the pandas dataframe constructor and passing the python dict object as data. Invoke to_sql() method on the pandas dataframe instance and specify the table name and database connection. This creates a table in MySQL database server and populates it with the data from the pandas dataframe.


1 Answers

The problem is that nan is not a valid value for a MySQL column. It is complaining just about that. Your values string should be:

(0L, u'2012-11-06T16:23:36-05:00', 0L, None, 23759918L, u'baseline', u'0 to 100', null, 105114L, 2009524L, True, u'charge', u'Charge')

To achieve this, add some logic to your script to change all nan values to NULL string.

like image 158
Oscar Pérez Avatar answered Oct 08 '22 14:10

Oscar Pérez