rodrwan

SQLManager.py

Oct 5th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import sqlite3 as sql
  5. import sys
  6.  
  7. class SQLManager():
  8.     def __init__(self, db):
  9.         self.db = db
  10.         self.con = sql.connect(self.db)
  11.         self.cur = None
  12.  
  13.     def select(self):
  14.         self.cur = self.con.cursor()
  15.         self.cur.execute("SELECT * FROM songs")
  16.        
  17.         while True:
  18.             row = self.cur.fetchone()
  19.             if row == None:
  20.                 break
  21.             print "ID: {0}, Nombre: {1}, Duracion: {2}".format(row[0], row[1], row[2])
  22.         self.con.close()
  23.     def insert(self):
  24.         songs = (
  25.             (1, "dubstep1", "10:00"),
  26.             (2, "dubstep2", "05:40")
  27.         )
  28.         try:
  29.             self.cur = self.con.cursor()
  30.    
  31.             self.cur.execute("DROP TABLE IF EXISTS songs")
  32.             self.cur.execute("CREATE TABLE songs (ID INT, NAME TEXT, LENGTH TEXT)")
  33.             self.cur.executemany("INSERT INTO songs VALUES (?, ?, ?)", songs)
  34.             self.con.commit()
  35.         except sql.Error, e:
  36.             if self.con:
  37.                 self.con.rollback()
  38.             print "Error %s:" % e.args[0]
  39.             sys.exit(1)
  40.         finally:
  41.             if self.con:
  42.                 self.con.close()
Advertisement
Add Comment
Please, Sign In to add comment