我该怎么做(如果扇区[1]=“l”和brcharacteritories[1]=“l”:)显示表中的所有结果?

2024-09-28 23:22:30 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: namebrstylekmlfslabelrowprint
1条回答
网友
1楼 · 发布于 2024-09-28 23:22:30

将基字符串更改为:

query='SELECT * FROM dbo.Services where 1=1' 

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

测试条件是否是给定的sentinel值,如果是空的menusearch项集,则不会附加任何其他内容。你知道吗

相关问题 更多 >