Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sql_class.py
blob: d170367493e0ea80ac7178033226b26ad277d61c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#! /usr/bin/env python

#  PROBLEMAS APARENTE CON EL LOAD_CONTACT

###########################################################################
# authors = {"pynash- Walter Danilo Vargas": "pynash AT gmail DOT com"}   #
#                                                                         #
# date = "January 30th, 2011"                                             #
#                                                                         #
# version = ":~$ revision 1.0$"                                           #
###########################################################################


import os.path
import sqlite3
import os.path, subprocess as sp
from random import randint

	
class Conexion(object):
	"""Class bd's conection, basic operation"""

	def __init__(self, id = "", f_name = " ", l_name = " ", address = " ", phone = " ", ocupation=" ", email=" ", category=" "):
		"""Initialiting var and conecting to bd"""
		self.id = id
		self.f_name = f_name
		self.l_name = l_name
		self.address = address
		self.phone = phone
		self.ocupation = ocupation
		self.email = email
		self.categoria = category
		self.imagen = ""

		if not (os.path.exists('sqlite3_localdb.db')):
			self.conn  = sqlite3.connect("sqlite3_localdb.db")
			self.c = self.conn.cursor()
			self.c.execute("""create table User (id INTEGER, \
							f_name text,\
							l_name text,\
							address text,\
							phone text,\
							ocupation text,\
							email text,\
							categoria text,\
							imagen BLOB)""")
			#cargamos la imagen
			imgdata = open('./varios/3.bmp', 'r').read()
			self.imagen = sqlite3.Binary(imgdata)
			
			simbol1 = (1, 'carlos', 'vargas', '', '12345', 'Estudiante', '', 'amigos', self.imagen, )
			simbol2 = (2, 'walter danilo', 'vargas', 'Masaya', '12345', 'Estudiante', 'pynash@gmail.com', 'otros', self.imagen, )
			self.c.execute("""insert into User values(?, ?, ?, ?, ?, ?, ?, ?, ?)""", simbol1)
			self.c.execute("""insert into User values(?, ?, ?, ?, ?, ?, ?, ?, ?)""", simbol2)
			self.conn.commit()
		else:
			self.conn  = sqlite3.connect("sqlite3_localdb.db")
			self.c = self.conn.cursor()

	def _exists_user(self, name):
		self.c.execute("select f_name, l_name from User ")
		for i in self.c:
			if (i[0].lower() + " " + i[1].lower()) == name:
				return 1

	def _operation_image(self, image):
		if os.path.exists('./images/show.jpg'):
			sp.call('rm ./images/show.jpg', shell=True)

		with  open('./images/show.jpg', 'w') as f:
			f.write(image)

	def _show_default(self, metodo=1):
		self.conn.row_factory = sqlite3.Row
		t = (self.f_name, self.l_name, )
		if metodo:
			self._randint()
			self.c.execute('select * from User where id=?', (self.id, ))
		else:
			self.c.execute('select * from User where f_name=? and l_name=?', t)
		r = self.c.fetchone()

		return [r[1], r[2], r[3], r[4], r[5], r[6], r[7]]

	def _add_user(self):
		"""register a new user to bd"""
		self.id = self._id_calc()
		imgdata = open('./noavailable.png', 'r').read()
		self.imagen = sqlite3.Binary(imgdata)
			
		simbol = (self.id, self.f_name, self.l_name, self.address, self.phone, self.ocupation, self.email, self.categoria, self.imagen, )
		self.c.execute('insert into User \
				 values (?, ?, ?, ?, ?, ?, ?, ?, ?)', simbol)
		self.conn.commit()

	def _return_id(self, fname, lname):
		t = (fname, lname, )
		self.conn.row_factory = sqlite3.Row
		self.c.execute('select id from User where f_name=? and l_name = ?', t)
		id = self.c.fetchone()
		return id[0]

	def _update_user(self):
		"""udpate a user in the bd"""
		t = (self.f_name, self.l_name, self.address, self.phone, self.ocupation, self.email, self.categoria, self.id)

		self.c.execute('update User \
				set f_name=?, l_name=?, address=?, phone=?, ocupation=?, email=?, categoria=?\
				 where id=?', t)
		self.conn.commit()

	def _del_user(self):
		"""delete a user"""
		t = (self.id, )
		self.c.execute('delete from User where id=?', t)
		self.conn.commit()

	def _id_calc(self):
		self.conn.row_factory = sqlite3.Row
		self.c.execute('select max(id) from user')
		id = self.c.fetchone()
		return id[0]+1

	def _randint(self):
		self.conn.row_factory = sqlite3.Row
		self.c.execute('select id from User')
		list1 = []
		for i in self.c:
			list1.append(i)
		list2 = []
		for i in list1:
			list2.append(i[0])
		self.id = list2[randint(1, len(list2)-1)]
		
	def _display_on_tree(self, cat):
		self.c.execute('select f_name, l_name from User where categoria=?', (cat, ))
		lista = []
		for i in self.c:
			lista.append([i[0], i[1]])
		return lista

		#lista = []
		#for i in self.c:
		#	lista.append(i[0] + " " + i[1])
		#return lista