-
Notifications
You must be signed in to change notification settings - Fork 1
/
SQLConnection.py
157 lines (109 loc) · 4.11 KB
/
SQLConnection.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
151
152
153
154
155
156
157
from PyQt4.QtSql import *
import sqlite3
class SQLConnection:
"""Handles the conncetion to the SQL database"""
def __init__(self,path):
self.path = path
self.db = None
def create_database(self):
with sqlite3.connect(self.path) as db:
cursor = db.cursor()
sql = """ CREATE TABLE Proxies(
ProxyID integer,
ProxyIP text,
ProxyPort text,
ProxyConnectionType text,
ProxySpeed text,
ProxyType text,
ProxyCountry text,
ProxyCity text,
Primary Key(ProxyID));
"""
self.create_table("Proxies",sql)
self.db = QSqlDatabase.addDatabase("QSQLITE")
self.db.setDatabaseName(self.path)
opened_ok = self.db.open()
return opened_ok
def create_table(self,table_name,sql):
with sqlite3.connect(self.path) as db:
cursor = db.cursor()
cursor.execute("select name from sqlite_master where name=?",(table_name,))
result = cursor.fetchall()
if len(result) == 1:
pass
else:
cursor.execute(sql)
db.commit()
def open_database(self):
if self.db:
self.close_database()
self.db = QSqlDatabase.addDatabase("QSQLITE")
self.db.setDatabaseName(self.path)
opened_ok = self.db.open()
return opened_ok
def close_database(self):
if self.db:
if self.db.isOpen() == True:
self.db.close()
#remove the database from the QSqlDatabase object - "conn" is the default
#database name
QSqlDatabase.removeDatabase("conn")
closed = self.db.open()
print("closed con")
self.db = None
else:
print("No connection open")
else:
print("No connection to close!")
def numberOfProxies(self):
query = QSqlQuery()
query.prepare(""" SELECT * FROM Proxies """)
query.exec_()
return query.size()
def getAllProxies(self):
query = QSqlQuery()
query.prepare("SELECT * FROM Proxies")
query.exec_()
results = []
header = self.selected_columns
query.first()
while query.isValid():
record = [query.value(index).toString() for index in range(len(header))]
results.append(record)
query.next()
print(results)
def addProxy(self, proxy):
query = QSqlQuery()
query.prepare("""INSERT INTO Proxies(ProxyIP,ProxyPort,ProxyConnectionType,
ProxySpeed,ProxyType,ProxyCountry,ProxyCity) VALUES(:ip,:port,:connType,
:speed,:type,:country,:city)""")
proxyAddr = proxy[0]
proxyAddrSplit = proxyAddr.split(":")
proxyIP = proxyAddrSplit[0]
proxyPort = proxyAddrSplit[1]
proxyConnection = proxy[1]
proxySpeed = proxy[2]
proxyType = proxy[3]
proxyCountry = proxy[4]
proxyCity = proxy[5]
query.bindValue(":ip",proxyIP)
query.bindValue(":port",proxyPort)
query.bindValue(":connType",proxyConnection)
query.bindValue(":speed",proxySpeed)
query.bindValue(":type",proxyType)
query.bindValue(":country",proxyCountry)
query.bindValue(":city",proxyCity)
query.exec_()
def closeEvent(self,event):
self.close_database()
def show_all_products(self):
query = QSqlQuery()
query.prepare(""" SELECT * FROM Product""")
query.exec_()
return query
def find_products_by_number(self,values):
query = QSqlQuery()
query.prepare(""" SELECT * FROM Product WHERE ProductID =? """)
query.addBindValue(values[0])
query.exec_()
return query