1
2
3 """
4 [bd.py]
5 Contiene la clase BD que tiene metodos que crean, editan y consultan la base de
6 datos tiempos.dat, y tambien contiene la clase statistic que aprovechando
7 los métodos de la clase anterior, hace estadisticas con los datos y consultas.
8 """
9
10 __author__ = "Victor Ramirez de la Corte"
11 __date__ = "23/04/2009"
12 __version__ = "PyRubik v0.6.5"
13
14 import sqlite3 as dbapi
15 import time
16 from language import *
17 import os, sys
18 import pygame
19
21 """Creamos y definimos funciones para guardar y utilizar la base de datos"""
23 self.BD = dbapi.connect("Tiempos.dat")
24
25 self.c = self.BD.cursor()
26
27 self.c.execute("""create table if not exists tiempos(id int PRIMARY KEY,\
28 tipo FLOAT, tiempo FLOAT, fecha Timestamp)""")
29
30
32 """guarda un tiempo en la base de datos, pasándole
33 como parámetros un tiempo y el tipo de cubo"""
34
35 self.c.execute('select * from tiempos')
36 cont = len(self.c.fetchall()) + 1
37
38 date = time.time()
39
40
41 self.c.execute('insert into tiempos values (%d,"%s","%f","%s")' % (cont,tipo,tiempo,date))
42
43
44 self.BD.commit()
45
46
47
49 """Consulta la base de datos completa"""
50 self.c.execute('select * from tiempos')
51 todo = ""
52 for fila in self.c:
53 Min = fila[2]/60
54 Seg = fila[2]%60
55
56 tiempo = lll.time + "%02d:%05.2f" %(Min, Seg)
57
58 fecha = lll.date + time.strftime('%H:%M:%S, %d-%b-%Y', time.localtime(fila[3]))
59 todo += "%s %40s" % (tiempo, fecha) + "\n"
60 return todo
61
62
81
83 """Consulta el mejor tiempo"""
84
85 self.c.execute('select * from tiempos order by tiempo ASC')
86 for tupla in self.c.fetchall():
87 tiempo = tupla[2]
88 break
89 Min = tiempo/60
90 Seg = tiempo%60
91 return lll.record + "%02d:%05.2f" %(Min, Seg)
92
94 """Consulta los tiempos del tipo de cubo que le des en la entrada"""
95 self.c.execute("""select * from tiempos where tipo='%s'""" % tipo)
96 todo = ""
97 for fila in self.c:
98 Min = fila[2]/60
99 Seg = fila[2]%60
100
101 tiempo = lll.time + "%02d:%05.2f" %(Min, Seg)
102
103 fecha = lll.date + time.strftime('%H:%M:%S, %d-%b-%Y', time.localtime(fila[3]))
104 todo += "%s %40s" % (tiempo, fecha) + "\n"
105 return todo
106
107
109 """cerramos el cursor y la base de datos"""
110 self.c.close()
111 self.BD.close()
112