【问题标题】:Error when inserting row into MySQL database from Python从 Python 将行插入 MySQL 数据库时出错
【发布时间】:2016-10-25 09:48:38
【问题描述】:

我正在尝试使用 Python 更新一个空的 mySQL 数据库。

这是我收到的错误:

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

这是有问题的代码:

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()

这是表在数据库中的样子(它是空的):

【问题讨论】:

  • 能否包含表的定义(包括索引和触发器)?从您的帖子中看不出是什么引发了错误。
  • 是的,我认为您以错误的方式使用数据类型 LineString,并且意味着使用 varchar 或 text 类型。 A LineString consists of Points 用于空间数据,而不是文本。

标签: python mysql pymysql


【解决方案1】:

看起来您正在使用 MySQL 类型 linestring 来表示字符串,而在 MySQL 中表示字符串的正确类型应该是 textvarchar(<some_length>)linestring 是几何线的表示,您可以在此处看到:https://dev.mysql.com/doc/refman/5.7/en/gis-linestring-property-functions.html

要解决此问题,只需将namecountry 的列类型更改为varchar(<some-length>)text。见:http://dev.mysql.com/doc/refman/5.7/en/char.html

【讨论】:

  • 谢谢大家,这已经解决了!
猜你喜欢
  • 2013-04-25
  • 2014-11-15
  • 2020-10-06
  • 1970-01-01
  • 1970-01-01
  • 2016-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多