首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(如果Sector[1]=="l“和BRCTerritories[1]==".":)在表格中调出所有结果,我该怎么做呢?

(如果Sector[1]=="l“和BRCTerritories[1]==".":)在表格中调出所有结果,我该怎么做呢?
EN

Stack Overflow用户
提问于 2011-04-19 23:55:05
回答 1查看 60关注 0票数 0
代码语言:javascript
复制
print 'Content-Type: application/vnd.google-earth.kml+xml'
print 'Content-Disposition: attachment; filename=BRC_Services.kml\n'
import _mssql
import cgi

fs = cgi.FieldStorage()

BRCTerritories=str(fs['BRCTerritories'].value)
Sector=str(fs['Sector'].value)

if BRCTerritories[1]==".":
    menusearch = {"BRC_Area":BRCTerritories[0:3],"Sector":str(fs['Sector'].value)}

else:
    menusearch = {"BRC_Territory":BRCTerritories[0:1],"Sector":str(fs['Sector'].value)} 

if BRCTerritories[1]=="l":
    menusearch = {"Sector":str(fs['Sector'].value)}

if Sector[1]=="l" and BRCTerritories[1]=="-":
    menusearch = {"BRC_Territory":BRCTerritories[0:1]}  

if Sector[1]=="l" and BRCTerritories[1]==".":
    menusearch = {"BRC_Area":BRCTerritories[0:3]}   

query='SELECT * FROM dbo.Services'

conn = _mssql.connect(server='gisdata', user='gisdata', password='gisdata', database='GISData')
addition=1

## KML Header

kmlheader = (
    '<?xml version="1.0" encoding="UTF-8"?>\n'
    '<kml xmlns="http://www.opengis.net/kml/2.2">\n'
    '<Document>\n'
    '<StyleMap id="msn_label">\n'
    '<Pair>\n'
    '<key>normal</key>\n'
    '<styleUrl>#sn_label</styleUrl>\n'
    '</Pair>\n'
    '<Pair>\n'
    '<key>highlight</key>\n'
    '<styleUrl>#sh_label</styleUrl>\n'
    '</Pair>\n'
    '</StyleMap>\n'

    '<Style id="sn_label">\n'
    '<LabelStyle>\n'
    '<scale>1</scale>\n'
    '</LabelStyle>\n'
    '<IconStyle><scale>1</scale><Icon><href>http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png</href></Icon></IconStyle>\n'
    '</Style>\n'

    '<Style id="sh_label">\n'
    '<LabelStyle>\n'
    '<scale>1</scale>\n'
    '</LabelStyle>\n'
    '<IconStyle><scale>2</scale><Icon><href>http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png</href></Icon></IconStyle>\n'
    '</Style>\n'
    )

name="<name>BRC Services Search Results</name>"

print kmlheader
print name

for term in menusearch:

    if addition==1:
        addition=0
        add= (
            " WHERE %s='%s'"
            ) %(term,menusearch[term])
    else:
        add= (
            " AND %s='%s'"
            ) %(term,menusearch[term])
    query=query+add

results = [ row for row in conn ] 

print "<Folder><name>BRC Services</name>"
print "<name><b>Search=</b>"
print menusearch
print "</name>"

for row in results:

    description = (
        '<h2>%s</h2><br /><br />\n'
        '<b>Postcode:</b> %s<br />\n'
        '<b>Sector:</b> %s<br />\n'
        '<b>Phone:</b> %s<br />\n'
        '<b>BRC_Territory:</b> %s<br />\n'
        '<b>BRC_Area:</b> %s<br />\n'   
        '<b>Web:</b> %s<br />\n'    
        ) %(row['Sector'],row['postcode'],row['Sector'],row['phone'],row['BRC_Territory'],row['BRC_Area'],row['web'])

    placemark = (
        '<Placemark>\n'
        '<name>%s</name>\n'
        '<description><![CDATA[%s]]></description>\n'
        '<styleUrl>#msn_label</styleUrl>\n'
        '<Style>\n'
        '<BalloonStyle>\n'
        '<color>ffffffff</color>'
        '<text><![CDATA[<font color="Black">$[description]</font>]]></text>'
        '</BalloonStyle>\n'
        '</Style>\n'
        '<Point>\n'
        '<coordinates>%s,%s</coordinates>\n'
        '</Point>\n'
        '</Placemark>\n\n'
        ) %(row['Sector'],description,row['Lat'],row['Lon'])
    print placemark

print "</Folder>"

kmlfooter = '</Document>\n</kml>'

print kmlfooter

conn.close
EN

回答 1

Stack Overflow用户

发布于 2011-04-20 00:16:35

将基本字符串更改为:

代码语言:javascript
复制
query='SELECT * FROM dbo.Services where 1=1' 

现在,您不需要基于"if addition=1“逻辑的where初始化;您只需将所有菜单搜索项迭代为"And”即可。

测试条件是否为给定的标记值,如果是,则空出一组menusearch术语,这样就不会追加任何其他内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5719172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档