2013-04-12 3 views
2

음, 파일을 분석하는 Python 프로그램이 있습니다. 이러한 파일의 데이터는 다음과 같습니다Python과 MySql - 기하학 객체를 얻을 수 없습니다.

Type=0x21  
Label=2428  
Data1=(54.67346,59.00001),(54.67415,59.00242),(54.67758,59.00001) 

이 내 코드, 그것은 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') 

데이터 변수의 데이터를 데이터베이스의 선 스트링 필드로 보내기 때문이라고 생각합니다. 나는 (1, 2, 3, 3)처럼 보일 날을 바꿔 보려고했지만이 오류가 다시 나타납니다. 데이터를 변경하고이 오류를 방지하려면 어떻게해야합니까?

답변

1

글쎄, 일부 연구 및 일부 테스트 후 나는 마침내 대답을 발견했다. 이 문제는 python이 아니라 mysql과 관련이 있습니다.

우선 데이터 변수는 Data="LineString(1 1,2 2,3 3)"이어야합니다. 그런 다음 Insert 함수에서 우리가 문자로 도형을 가져올 수 있도록 (GeomFromText('%(Data)s'))을 작성하십시오. 따라서 전체 삽입 선은 다음과 같습니다.

sql="""INSERT INTO `polylines` (Type, Label, Data) VALUES ('%(Type)s', '%(Label)s', (GeomFromText('%(Data)s')))"""%{"Type":type, "Label":label, "Data":data} 

이제 작동합니다.

관련 문제