Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
chinoogawa
GitHub Repository: chinoogawa/fbht
Path: blob/master/database.py
206 views
1
import sqlite3 as db
2
3
try:
4
global connect, cursor, rows
5
connect = db.connect("fb_db.db",check_same_thread = False)
6
cursor = connect.cursor()
7
except:
8
print 'Error handling Database'
9
10
def insertTestUsers(userId,name,email,password):
11
for i in range(len(email)):
12
try:
13
cursor.execute("INSERT INTO testUsers (id, name, email, password,logged, blocked) VALUES(?, ?, ?, ?,0,0)", (userId[i], name[i], email[i], password[i]))
14
except:
15
print 'No se realizo la iteracion %d \n' % i
16
17
connect.commit()
18
19
def insertTestUsersDev(userId,name):
20
for i in range(len(userId)):
21
try:
22
cursor.execute("INSERT INTO testUsers (id, name, email, password,logged, blocked) VALUES(?, ?, 0, 1234567890,0,0)", (userId[i], name[i]))
23
except:
24
print 'No se realizo la iteracion %d \n' % i
25
26
connect.commit()
27
28
def removeTestUsers(userId):
29
query = "DELETE FROM testUsers WHERE id = %d;" % int(userId)
30
try:
31
cursor.execute(query)
32
print '\rSuccesfull deleted %d \r' % int(userId),
33
except:
34
print 'No se pudo eliminar el id de usuario %d \r' % int(userId),
35
print ''
36
connect.commit()
37
38
def getUsers():
39
cursor.execute("SELECT * FROM testUsers;")
40
rows = cursor.fetchall()
41
return rows
42
43
def setLogged(c_user):
44
query = "UPDATE testUsers SET logged=1 WHERE id = %d;" % int(c_user)
45
try:
46
cursor.execute(query)
47
except:
48
print 'Error en setLogged() \n'
49
50
connect.commit()
51
52
def setLoggedOut(c_user):
53
query = "UPDATE testUsers SET logged=0 WHERE id = %d;" % int(c_user)
54
try:
55
cursor.execute(query)
56
except:
57
print 'Error en setLogged() \n'
58
59
connect.commit()
60
61
def status():
62
queries = ["SELECT count(*) FROM testUsers;","SELECT count(*) FROM testUsers WHERE logged=0;","SELECT count(*) FROM testUsers WHERE logged=1;",
63
"SELECT count(*) FROM testUsers WHERE blocked=1;"]
64
try:
65
for query in queries:
66
cursor.execute(query)
67
row = cursor.fetchall()
68
for rows in row:
69
print 'The query: ' + query + ' dump the result: %s' % (rows,) + '\n'
70
except:
71
print 'Error in status() \n'
72
73
def getUsersNotLogged():
74
cursor.execute("SELECT * FROM testUsers WHERE logged=0;")
75
rows = cursor.fetchall()
76
return rows
77
78
def createVictimTable(victim):
79
try:
80
victim = victim.replace(".","_")
81
cursor.execute("CREATE TABLE "+str(victim)+"_nodes(friendName text, friendId text)")
82
cursor.execute("CREATE TABLE "+str(victim)+"_friends_edges(friendName text, friendId text, edges text, edgesIDS text)")
83
except:
84
print 'Error al crear la tabla'
85
return -1
86
connect.commit()
87
88
def addNode(victim,friendName, friendId):
89
90
try:
91
victim = victim.replace(".","_")
92
if (checkNodeExistence(victim, friendName, friendId) == False):
93
cursor.execute("INSERT INTO "+str(victim)+"_nodes (friendName, friendId) VALUES(?, ?)", (friendName, friendId))
94
cursor.execute("INSERT INTO "+str(victim)+"_friends_edges (friendName, friendId) VALUES(?, ?)", (friendName, friendId))
95
except:
96
print 'Error al ingresar el nodo %s' %friendName
97
98
connect.commit()
99
100
def addEdge(victim,friendName, friendId, edge, edgeID):
101
if checkNodeExistence(victim,friendName, friendId) == True:
102
try:
103
victim = victim.replace(".","_")
104
cursor.execute("SELECT edges FROM "+str(victim)+"_friends_edges WHERE friendName=\""+str(friendName)+"\" OR friendId=\""+str(friendId)+"\";")
105
rows = str(cursor.fetchone()).strip("(None,)").strip("'")
106
rows = rows.encode('ascii','replace') + edge + ";"
107
cursor.execute("UPDATE "+str(victim)+"_friends_edges SET edges=\""+rows+"\" WHERE friendName=\""+str(friendName)+"\" OR friendId=\""+str(friendId)+"\";")
108
cursor.execute("SELECT edgesIDS FROM "+str(victim)+"_friends_edges WHERE friendName=\""+str(friendName)+"\" OR friendId=\""+str(friendId)+"\";")
109
rows = str(cursor.fetchone()).strip("(None,)").strip("'")
110
rows = rows.encode('ascii','replace') + edgeID + ";"
111
cursor.execute("UPDATE "+str(victim)+"_friends_edges SET edgesIDS=\""+rows+"\" WHERE friendName=\""+str(friendName)+"\" OR friendId=\""+str(friendId)+"\";")
112
113
except db.Error as e:
114
print "An error occurred:", e.args[0]
115
116
except:
117
print 'Error al hacer update para el nodo %s' %edge
118
119
connect.commit()
120
else:
121
return -1
122
123
def getNodes(victim):
124
victim = victim.replace(".","_")
125
cursor.execute("SELECT * FROM "+str(victim)+"_nodes;")
126
rows = cursor.fetchall()
127
return rows
128
129
def getEdges(victim, friendName, friendId):
130
victim = victim.replace(".","_")
131
cursor.execute("SELECT * FROM "+str(victim)+"_friends_edges WHERE friendName=\""+str(friendName)+"\" OR friendId=\""+str(friendId)+"\";")
132
rows = cursor.fetchall()
133
return rows
134
135
def checkNodeExistence(victim, friendName, friendId):
136
victim = victim.replace(".","_")
137
cursor.execute("SELECT * FROM "+str(victim)+"_nodes WHERE friendName=\""+str(friendName)+"\" OR friendId=\""+str(friendId)+"\";")
138
rows = cursor.fetchall()
139
if rows != []:
140
return True
141
else:
142
return False
143
144
def checkTableExistence(victim):
145
try:
146
cursor.execute("SELECT count(*) FROM "+victim.replace(".","_")+"_nodes;")
147
res = cursor.fetchone()
148
return bool(res[0])
149
except:
150
return False
151