Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python和MySql-can';无法获取几何体对象_Python_Mysql_Geometry - Fatal编程技术网

Python和MySql-can';无法获取几何体对象

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

我有一个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')))"""%{"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} 
现在它工作了