-
Notifications
You must be signed in to change notification settings - Fork 445
/
database.py
executable file
·150 lines (125 loc) · 5.58 KB
/
database.py
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
147
148
149
150
import sqlite3 as db
try:
global connect, cursor, rows
connect = db.connect("fb_db.db",check_same_thread = False)
cursor = connect.cursor()
except:
print 'Error handling Database'
def insertTestUsers(userId,name,email,password):
for i in range(len(email)):
try:
cursor.execute("INSERT INTO testUsers (id, name, email, password,logged, blocked) VALUES(?, ?, ?, ?,0,0)", (userId[i], name[i], email[i], password[i]))
except:
print 'No se realizo la iteracion %d \n' % i
connect.commit()
def insertTestUsersDev(userId,name):
for i in range(len(userId)):
try:
cursor.execute("INSERT INTO testUsers (id, name, email, password,logged, blocked) VALUES(?, ?, 0, 1234567890,0,0)", (userId[i], name[i]))
except:
print 'No se realizo la iteracion %d \n' % i
connect.commit()
def removeTestUsers(userId):
query = "DELETE FROM testUsers WHERE id = %d;" % int(userId)
try:
cursor.execute(query)
print '\rSuccesfull deleted %d \r' % int(userId),
except:
print 'No se pudo eliminar el id de usuario %d \r' % int(userId),
print ''
connect.commit()
def getUsers():
cursor.execute("SELECT * FROM testUsers;")
rows = cursor.fetchall()
return rows
def setLogged(c_user):
query = "UPDATE testUsers SET logged=1 WHERE id = %d;" % int(c_user)
try:
cursor.execute(query)
except:
print 'Error en setLogged() \n'
connect.commit()
def setLoggedOut(c_user):
query = "UPDATE testUsers SET logged=0 WHERE id = %d;" % int(c_user)
try:
cursor.execute(query)
except:
print 'Error en setLogged() \n'
connect.commit()
def status():
queries = ["SELECT count(*) FROM testUsers;","SELECT count(*) FROM testUsers WHERE logged=0;","SELECT count(*) FROM testUsers WHERE logged=1;",
"SELECT count(*) FROM testUsers WHERE blocked=1;"]
try:
for query in queries:
cursor.execute(query)
row = cursor.fetchall()
for rows in row:
print 'The query: ' + query + ' dump the result: %s' % (rows,) + '\n'
except:
print 'Error in status() \n'
def getUsersNotLogged():
cursor.execute("SELECT * FROM testUsers WHERE logged=0;")
rows = cursor.fetchall()
return rows
def createVictimTable(victim):
try:
victim = victim.replace(".","_")
cursor.execute("CREATE TABLE "+str(victim)+"_nodes(friendName text, friendId text)")
cursor.execute("CREATE TABLE "+str(victim)+"_friends_edges(friendName text, friendId text, edges text, edgesIDS text)")
except:
print 'Error al crear la tabla'
return -1
connect.commit()
def addNode(victim,friendName, friendId):
try:
victim = victim.replace(".","_")
if (checkNodeExistence(victim, friendName, friendId) == False):
cursor.execute("INSERT INTO "+str(victim)+"_nodes (friendName, friendId) VALUES(?, ?)", (friendName, friendId))
cursor.execute("INSERT INTO "+str(victim)+"_friends_edges (friendName, friendId) VALUES(?, ?)", (friendName, friendId))
except:
print 'Error al ingresar el nodo %s' %friendName
connect.commit()
def addEdge(victim,friendName, friendId, edge, edgeID):
if checkNodeExistence(victim,friendName, friendId) == True:
try:
victim = victim.replace(".","_")
cursor.execute("SELECT edges FROM "+str(victim)+"_friends_edges WHERE friendName=\""+str(friendName)+"\" OR friendId=\""+str(friendId)+"\";")
rows = str(cursor.fetchone()).strip("(None,)").strip("'")
rows = rows.encode('ascii','replace') + edge + ";"
cursor.execute("UPDATE "+str(victim)+"_friends_edges SET edges=\""+rows+"\" WHERE friendName=\""+str(friendName)+"\" OR friendId=\""+str(friendId)+"\";")
cursor.execute("SELECT edgesIDS FROM "+str(victim)+"_friends_edges WHERE friendName=\""+str(friendName)+"\" OR friendId=\""+str(friendId)+"\";")
rows = str(cursor.fetchone()).strip("(None,)").strip("'")
rows = rows.encode('ascii','replace') + edgeID + ";"
cursor.execute("UPDATE "+str(victim)+"_friends_edges SET edgesIDS=\""+rows+"\" WHERE friendName=\""+str(friendName)+"\" OR friendId=\""+str(friendId)+"\";")
except db.Error as e:
print "An error occurred:", e.args[0]
except:
print 'Error al hacer update para el nodo %s' %edge
connect.commit()
else:
return -1
def getNodes(victim):
victim = victim.replace(".","_")
cursor.execute("SELECT * FROM "+str(victim)+"_nodes;")
rows = cursor.fetchall()
return rows
def getEdges(victim, friendName, friendId):
victim = victim.replace(".","_")
cursor.execute("SELECT * FROM "+str(victim)+"_friends_edges WHERE friendName=\""+str(friendName)+"\" OR friendId=\""+str(friendId)+"\";")
rows = cursor.fetchall()
return rows
def checkNodeExistence(victim, friendName, friendId):
victim = victim.replace(".","_")
cursor.execute("SELECT * FROM "+str(victim)+"_nodes WHERE friendName=\""+str(friendName)+"\" OR friendId=\""+str(friendId)+"\";")
rows = cursor.fetchall()
if rows != []:
return True
else:
return False
def checkTableExistence(victim):
try:
cursor.execute("SELECT count(*) FROM "+victim.replace(".","_")+"_nodes;")
res = cursor.fetchone()
return bool(res[0])
except:
return False