将数据连接到数据库,字符串错误

时间:2018-08-19 04:50:32

标签: python database postgresql postgis psycopg2

我已经编写了以下所有代码...试图获取URL的数据并在将其连接到数据库后对其进行查询。粗体字是一般错误所在的部分。请参阅最后一行以获取正在显示的错误消息...顺便说一句,我已经使用PostGIS和postgreSQl等制作了数据库本身...我正在阅读教科书“精通地理空间分析”

第1部分-连接数据库

import psycopg2

connection = psycopg2.connect(database="pythonspatial",user="#####",password="######")

cursor = connection.cursor()

cursor.execute("CREATE TABLE art_pieces (id SERIAL PRIMARY KEY, code VARCHAR(255), location GEOMETRY)")

connection.commit()

PART 2-查询网站数据

#requests is a package installed from FILE>SETTINGS>Project interpreter>'+' symbol

import requests
from psycopg2.psycopg1 import cursor, connection

#Getting data from URL

url='http://coagisweb.cabq.gov/arcgis/rest/services/public/PublicArt/MapServer/0/query'

查询数据

params={"where":"1=1","outFields":"*","outSR":"4326","f":"json"}

r=requests.get(url,params=params)

data=r.json()

data["features"][0]

PART 3-为检索到的每个数据提供WKT文件

for a in data["features"]:
    code=a["attributes"]["ART_CODE"]
    wkt="POINT("+str(a["geometry"]["x"])+" "+str(a["geometry"]["y"])+")"
    if a["geometry"]["x"]=='NaN':
        pass
    else:
    **cursor.execute("INSERT INTO art_pieces (code, location) VALUES ({}, 
    ST_GeomFromText('{}'))".format(code, wkt))**
        connection.commit()

这是错误:

Traceback (most recent call last):
  File "C:/Users/lewis/PycharmProjects/TRYING_THIS/DO THIS.py", line 52, in <module>
    cursor.execute("INSERT INTO art_pieces (code, location) VALUES ({}, ST_GeomFromText('{}'))".format(code, wkt))
TypeError: descriptor 'execute' requires a 'psycopg2.extensions.cursor' object but received a 'str'

0 个答案:

没有答案