Nate Nate - 5 months ago 42
SQL Question

Error when inserting row into MySQL database from Python

I'm trying to update an empty mySQL database using Python.

This is the error I receive:


pymysql.err.InternalError: (1416, 'Cannot get geometry object from data you send to the GEOMETRY field')


Here's the problematic code:

try:
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `projects` (`name`, `country`, `activation_date`, `active`) VALUES (%s, %s, %s, %s)"
cursor.execute(sql, ('Nate', 'USA', '2016-06-23 09:11:32', '1'))

connection.commit()

finally:
connection.close()


Here is what the table looks like in the database (it's empty):

enter image description here

enter image description here

Answer

It looks like you're using the MySQL type linestring to represent a string, while in MySQL the correct type to represent a string of characters would be text or varchar(<some_length>). linestring is a representation of a geometric line, as you can see here: https://dev.mysql.com/doc/refman/5.7/en/gis-linestring-property-functions.html

To fix this, simply change the column types of name and country to varchar(<some-length>) or text. See: http://dev.mysql.com/doc/refman/5.7/en/char.html

Comments