亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python使用MYSQLDB實(shí)現(xiàn)從數(shù)據(jù)庫中導(dǎo)出XML文件的方法

 更新時(shí)間:2015年05月11日 10:36:04   作者:蛇小狼  
這篇文章主要介紹了Python使用MYSQLDB實(shí)現(xiàn)從數(shù)據(jù)庫中導(dǎo)出XML文件的方法,涉及Python使用MYSQLDB操作數(shù)據(jù)庫及XML文件的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了Python使用MYSQLDB實(shí)現(xiàn)從數(shù)據(jù)庫中導(dǎo)出XML文件的方法。分享給大家供大家參考。具體分析如下:

這里需要給前端以xml格式提供一些數(shù)據(jù),這些數(shù)據(jù)在目前的數(shù)據(jù)庫中已經(jīng)存在。

如果使用django返回xml數(shù)據(jù)的話,需要包裝下頭信息:

復(fù)制代碼 代碼如下:
r = HttpResponse(str_xml)
r.mimetype = "text/xml"
r['Content-Type'] = "application/xml"

另外,使用group by可以使用以下方式來查詢。
復(fù)制代碼 代碼如下:
objs = Fish.objects.raw("SELECT  id, almanac_name, style , almanac_code,almanac_description FROM ppy_fish WHERE almanac_name != ''  GROUP BY almanac_code")

簡單的舉個(gè)例子:

# -*- coding: utf-8 -*-
from xml.dom import minidom
import MySQLdb
conn = MySQLdb.connect(host='localhost',user='root',passwd='xxx',db='my_xml',charset="utf8")
cursor = conn.cursor()
cursor.execute('select id, name, style, description, family from ppy_fish')
res_list = cursor.fetchall()
print len(res_list)
doc = minidom.Document()
root = doc.createElement("data")
doc.appendChild(root)
ATTRIBUTE = {"n":1, "d":3}
for res in res_list:
  node = doc.createElement(res[2])
  for i in ATTRIBUTE:
    id_node = doc.createElement("%s" % i)
    data = doc.createTextNode("%s" % res[ATTRIBUTE[i]])
    id_node.appendChild(data)
    node.appendChild(id_node)
  root.appendChild(node)
str_xml = doc.toxml("utf-8")
f = open('fish.xml', 'w')
f.write(str_xml)
f.close()
cursor.close()
conn.close()

希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論