Answer the question
In order to leave comments, you need to log in
How to pass select query result from mysql to xml?
@app.route('/getxml')
def getxml():
g.cur.execute("""select * from table""")
o = g.cur.fetchall()
mysql --xml -e 'use dbname; SELECT * from tb'
use XML::Dumper;
print new XML::Dumper->pl2xml($o);
Answer the question
In order to leave comments, you need to log in
A bike
def fetchall_xml(cur, pretty_print=True):
import lxml.etree
fields_value = cur.fetchall()
fields_name = tuple(i[0] for i in cur.description)
elements = lxml.etree.Element('elements')
for el in fields_value:
element = lxml.etree.SubElement(elements, 'element', attrib={'foo': 'bar'})
for j in range(len(fields_name)):
lxml.etree.SubElement(element, str(fields_name[j])).text = '{}'.format(el[j])
return lxml.etree.tounicode(elements, pretty_print=pretty_print)
def main():
conn = sqlite3.connect('compare.db')
cur = conn.cursor()
cur.execute('SELECT * FROM PRODUCT_EXCHANGERATE')
xml = fetchall_xml(cur)
print(xml)
main()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question