python对MySQL的操作

代码如下:
import os
import os.path
import sys
import MySQLdb

conn = MySQLdb.connect(host='10.100.1.95', user='leyuyin', passwd='leyuyin', port =3308, db='weather', charset='utf8')
cursor = conn.cursor()
count = cursor.execute('select distinct province from CITY_CODE')
print '总共有%s条记录' %count

command = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
command += "<dicts>\n"

result = cursor.fetchall();
for i in result:
    cursor.execute("select code from CITY_CODE where  province = '%s' and province = name" %i[0])
    command += "<province code=\""
    province = cursor.fetchone();
    command += str(province[0])
    command += "\"  name=\""
    command += i[0]
    command += "\">"
    command +="\n"
    print "select distinct city  from CITY_CODE where province = '%s'  and city != province "  %i[0]
    count = cursor.execute("select distinct city  from CITY_CODE where province = '%s' and city != province " %i[0])
    if(count == 0):
        count = cursor.execute("select distinct name  from CITY_CODE where province = '%s'  and name != province " %i[0])
        for  m in cursor.fetchall():
            cursor.execute("select code from CITY_CODE where  name = '%s' and city = '%s'" %(m[0],i[0]))
            command += "<town code=\""
            town = cursor.fetchone();
            command += str(town[0])
            command += "\" name=\""
            command += m[0]
            command += "\"/>"
            command +="\n"
    print '共有%s条记录'  %count
    cityresult=cursor.fetchall()
    for  n in cityresult:
        print "select code from CITY_CODE where  city = '%s' and city = name" %n[0]
        citycount = cursor.execute("select code from CITY_CODE where  city = '%s' and city = name" %n[0])      
        if(citycount >=0):
            if(citycount == 0):
                command += "<city code=\""
                command += "\"  name=\""
                command += n[0]
                command += "\">"
                command +="\n"
            else:
                command += "<city code=\""
                city = cursor.fetchone();
                command += str(city[0])
                command += "\"  name=\""
                command += n[0]
                command += "\">"
                command +="\n"
            count = cursor.execute("select distinct name  from CITY_CODE where city = '%s' and city != name" %n[0])
            for  m in cursor.fetchall():
                cursor.execute("select code from CITY_CODE where  name = '%s' and city = '%s'" %(m[0], n[0]))
                command += "<town code=\""
                town = cursor.fetchone();
                command += str(town[0])
                command += "\" name=\""
                command += m[0]
                command += "\"/>"
                command +="\n"
            command += "</city>"
            command +="\n"
    command += "</province>"
    command +="\n"
command += "</dicts>"
command +="\n"
print command
file_object = open('city.xml', 'w')
file_object.writelines(command.encode('utf-8'))
file_object.close()
cursor.close() 
conn.close() 
        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值