Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果扇区[1]=“l”和属性[1]=“l”:(如果扇区[1]=“l”)显示表中的所有结果,我该怎么做?_Python_Sql_Sql Server 2008 - Fatal编程技术网

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

Python 如果扇区[1]=“l”和属性[1]=“l”:(如果扇区[1]=“l”)显示表中的所有结果,我该怎么做?,python,sql,sql-server-2008,Python,Sql,Sql Server 2008,将基本字符串更改为: 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

将基本字符串更改为:

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
现在不需要基于if addition=1逻辑的where初始化;您可以将所有菜单搜索项作为和进行迭代

测试条件是否为给定的sentinel值,如果为空,则为menusearch术语集,因此不会附加任何其他内容

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