rodrwan

db_handler

Nov 13th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. import sqlite3 as db
  2. import json
  3.  
  4. con = db.connect('sis_dis.db')
  5. cur = con.cursor()
  6.  
  7.  
  8. def selectAlumno(rut, tipo = "json"):
  9.     cur.execute("SELECT * FROM alumnos WHERE rut= '%s'" % rut)
  10.     info = cur.fetchone()
  11.     if tipo == "json":
  12.         msg = {"rut":info[0],"nombre":info[1],"apellido":info[2],"email":info[3]}
  13.         return json.dumps(msg, sort_keys=False, indent=4)
  14.     elif tipo == "xml":
  15.         from xml.dom.minidom import Document
  16.  
  17.         # Create the minidom document
  18.         doc = Document()
  19.  
  20.         # Create the <wml> base element
  21.         wml = doc.createElement("usuario")
  22.         wml.setAttribute("id", info[0])
  23.         doc.appendChild(wml)
  24.  
  25.         # Create the main <card> element
  26.         rut = doc.createElement("rut")
  27.         wml.appendChild(rut)
  28.         nombre = doc.createElement("nombre")
  29.         wml.appendChild(nombre)
  30.         apellido = doc.createElement("apellido")
  31.         wml.appendChild(apellido)
  32.         email = doc.createElement("email")
  33.         wml.appendChild(email)
  34.  
  35.         # Give the <p> elemenet some text
  36.         ptext = doc.createTextNode(info[0])
  37.         rut.appendChild(ptext)
  38.         # Give the <p> elemenet some text
  39.         ptext = doc.createTextNode(info[1])
  40.         nombre.appendChild(ptext)
  41.         # Give the <p> elemenet some text
  42.         ptext = doc.createTextNode(info[2])
  43.         apellido.appendChild(ptext)
  44.         # Give the <p> elemenet some text
  45.         ptext = doc.createTextNode(info[3])
  46.         email.appendChild(ptext)
  47.         # Print our newly created XML
  48.         return doc.toprettyxml(indent="  ")
Advertisement
Add Comment
Please, Sign In to add comment