简体   繁体   English

python创建列表以通过SQL查询

[英]python create list to pass through SQL query

I have this list 我有这个清单

li=['beststreet','borocd','am_linkid3','bestfrom','bestto','boro','bestborost','sa18','resurf18','allpaving','nocstart','nocend','sa19','resurf19','addedbyrrm','rmmweekly']

I am looping through shp which is an orderded dict from the fiona module fiona.readthedocs.io/en/latest/manual.html 我正在遍历shp,这是来自fiona模块fiona.readthedocs.io/en/latest/manual.html的有序命令

sample code without typing out all the above columns: 示例代码,而无需输入上述所有列:

for r in shp:
    sql_qry='''insert into table (beststreet,borocd) values(%s,%s)'''
    values=[r['properties']['beststreet'],r['properties']['borocd']]
    cur.execute(sql_qry,values)

This method will work if I hard code the columns. 如果我对列进行硬编码,则此方法将起作用。

PROBLEM 问题

I am creating a class method where shp can contain any amount and any column name. 我正在创建一个类方法,其中shp可以包含任何数量和任何列名称。 So what I need to figure out is how to construct a proper list to pass through to the SQL query. 因此,我需要弄清楚的是如何构建适当的列表以传递给SQL查询。

r['properties'] will always be at the beginning to access each field r['properties']将始终位于访问每个字段的开头

So ideally I would like to do something like this: 因此,理想情况下,我想执行以下操作:

values= ["r['properties']["+"'"+l+"'"+"]" for l in li]

this prints out 打印出来

["r['properties']['beststreet']", "r['properties']['borocd']", "r['properties']['am_linkid3']", "r['properties']['bestfrom']", "r['properties']['bestto']", "r['properties']['boro']", "r['properties']['bestborost']", "r['properties']['sa18']", "r['properties']['resurf18']", "r['properties']['allpaving']", "r['properties']['nocstart']", "r['properties']['nocend']", "r['properties']['sa19']", "r['properties']['resurf19']", "r['properties']['addedbyrrm']", "r['properties']['rmmweekly']"]

which is not valid but I feel that I am close. 无效,但我觉得自己很亲近。 I'm using python2.7 to access a postgre instance through psycopg2 . 我正在使用python2.7通过psycopg2访问postgre实例。

sample OrderedDict 样本OrderedDict

shp={'properties': OrderedDict([(u'BestStreet', u'blah AV'), (u'BoroCD', 503L), (u'AM_LINKID3', 106881.0), (u'BestFrom', u'doubt it TER'), (u'BestTo', u'blah AV DEAD END'), (u'Boro', u'SI'), (u'BestBoroSt', u'SI - nuu AV'), (u'SA18', None), (u'resurf18', u'2019'), (u'AllPaving', None), (u'NOCstart', None), (u'NOCend', None), (u'SA19', u'S2305'), (u'resurf19', u'YES'), (u'addedbyRRM', None), (u'RMMweekly', None)])}

Consider the fact that operating this way is dangerous; 考虑这样一种事实很危险; it can lead to all sort of, lets call them issues.. Anyway if you have control over the input, supposing r has a method to retrieve keys and values (like dictionaries do) I think a solution could be easily achieved with code like this: 无论如何,如果您可以控制输入,假设r有一种检索键和值的方法(像字典一样),我认为可以使用这样的代码轻松实现解决方案:

table = 'your_table_here'
q = 'INSERT INTO {0} ({1}) VALUES ({2});'
f = lambda x: "'{}'".format(x) if isinstance(x, str) else str(x)

for r in shp:
    sql = q.format(table, ','.join(list(map(f, r.keys()))), ','.join(list(map(f, r.values()))))
    cur.execute(sql)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM