Python和MySql-can';无法获取几何体对象
我有一个python程序可以解析文件。这些文件中的数据如下所示:Python和MySql-can';无法获取几何体对象,python,mysql,geometry,Python,Mysql,Geometry,我有一个python程序可以解析文件。这些文件中的数据如下所示: Type=0x21 Label=2428 Data1=(54.67346,59.00001),(54.67415,59.00242),(54.67758,59.00001) sql="""INSERT INTO `polylines` (Type, Label, Data) VALUES ('%(Type)s', '%(Label)s', (GeomFromText('%(Data)s')))"""%{"Typ
Type=0x21
Label=2428
Data1=(54.67346,59.00001),(54.67415,59.00242),(54.67758,59.00001)
sql="""INSERT INTO `polylines` (Type, Label, Data) VALUES ('%(Type)s', '%(Label)s', (GeomFromText('%(Data)s')))"""%{"Type":type, "Label":label, "Data":data}
这是我的代码,它将解析后的数据发送到MySql数据库
import MySQLdb
f = open('test.mp', 'r')
db = MySQLdb.connect(host="127.0.0.1", user="root", passwd="", db="gis", charset='utf8')
cursor = db.cursor()
i=0
for line in f.readlines():
if (line.startswith("Type")):
type=line[5:]
if (line.startswith("Label")):
label=line[6:]
if (line.startswith("Data")):
data=line[6:]
sql="""INSERT INTO `polylines` (Type, Label, Data) VALUES ('%(Type)s', '%(Label)s', '%(Data)s')"""%{"Type":type, "Label":label, "Data":data}
cursor.execute(sql)
db.commit()
db.close()
f.close()
我总是犯同样的错误-
_mysql_exceptions.OperationalError: (1416, 'Cannot get
geometry object from data you send to the GEOMETRY field')
我认为这是因为我将数据变量中的数据发送到数据库中的Linestring字段。我试图将发送的日期更改为(11,2,3),但再次出现此错误。如何更改数据并避免此错误?经过一些研究和测试,我终于找到了答案。这个问题不是python的问题,而是mysql的问题 首先,我们需要数据变量看起来像
Data=“LineString(1 1,2,3)”
。然后,在Insert函数中,我们应该编写(GeomFromText('%(Data)s'))
,以帮助mysql从文本中获取几何体。因此,整个插入行如下所示:
Type=0x21
Label=2428
Data1=(54.67346,59.00001),(54.67415,59.00242),(54.67758,59.00001)
sql="""INSERT INTO `polylines` (Type, Label, Data) VALUES ('%(Type)s', '%(Label)s', (GeomFromText('%(Data)s')))"""%{"Type":type, "Label":label, "Data":data}
现在它工作了