/
database.py
79 lines (73 loc) · 2.11 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
'''
Created on Apr 2, 2017
@author: sginne
'''
import sqlite3
from router import Router
from port import Port
from wire import Wire
from package import Package
DbConnection = None
DbCursor= None
#database initialization and sql command execution.
def InitSQL(FileName):
DbConnection = sqlite3.connect('main.db')
DbCursor=DbConnection.cursor()
DatabaseFile=open(FileName,'r')
SqlFile=DatabaseFile.read()
DatabaseFile.close()
SqlCommands = SqlFile.split(';')
for command in SqlCommands:
DbCursor.execute(command)
DbConnection.commit()
DbConnection.close()
# Loading all ports available for each router.
def ListPorts(Where=''):
out=[]
DbConnection = sqlite3.connect('main.db')
DbCursor=DbConnection.cursor()
#cursor=DbCursor.execute("SELECT * FROM PORTS "+Where)
for row in DbCursor.execute("SELECT * FROM PORTS "+Where).fetchall():
Obj=Port()
Obj.Id=row[0]
Obj.RouterId=row[1]
Obj.Ip=row[2]
out.append(Obj)
DbConnection.close()
return out
# Load the list of routers conneted from database.
def ListRouters(Where=''):
out=[]
DbConnection = sqlite3.connect('main.db')
DbCursor=DbConnection.cursor()
for row in DbCursor.execute("SELECT * FROM ROUTERS "+Where).fetchall():
Obj=Router()
Obj.Id=row[0]
Obj.Name=row[1]
out.append(Obj)
DbConnection.close()
return out
def ListWires(Where=''):
out=[]
DbConnection = sqlite3.connect('main.db')
DbCursor=DbConnection.cursor()
for row in DbCursor.execute("SELECT * FROM WIRES "+Where).fetchall():
Obj=Wire()
Obj.Id=row[0]
Obj.Port1Id=row[1]
Obj.Port2Id=row[2]
out.append(Obj)
DbConnection.close()
return out
def ListPackages(Where=''):
out=[]
DbConnection = sqlite3.connect('main.db')
DbCursor=DbConnection.cursor()
for row in DbCursor.execute("SELECT * FROM PACKAGES "+Where).fetchall():
Obj=Wire()
Obj.Id=row[0]
Obj.Position=row[1]
Obj.Destination=row[14]
out.append(Obj)
DbConnection.close()
return out