-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDBHandler.py
More file actions
75 lines (60 loc) · 2.15 KB
/
Copy pathDBHandler.py
File metadata and controls
75 lines (60 loc) · 2.15 KB
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
##
## JS8msg is a copyrighted program written by Thomas Kocourek, N4FWD
## This program is released under the GPL v3 license
##
## a class object to handle all sql processing
import sqlite3
class DB_object():
## When we create a new object, we pass the database file name to this class
## The reason for the set* and exec* methods is for assisting with debugging.
## 'db_file' is a string
def __init__ (self,db_file):
super().__init__()
self.sql_result = None
self.db_file = db_file
## init a handler for SQL messages
try:
self.SQL_message = ""
self.conn = sqlite3.connect(self.db_file)
## FOREIGN KEYS not supported
self.cur = self.conn.cursor()
return None
except:
return sqlite3.DatabaseError
def get_connection(self):
## return the connection handler for the opened database
return self.conn
def get_cursor(self):
## return the SQL cursor for the opened database
return self.cur
def set_SQL(self, message):
## 'message' will be a SQL command string
self.SQL_message = message
def exec_SQL(self):
## this method is when you want to do selective inserting or replacing
## kind of a SQL 'catch-all' method
try:
self.sql_result = self.cur.execute(self.SQL_message)
self.conn.commit()
return self.sql_result
except:
## return SQL error
return self.sql_result
def fetch_all_SQL(self):
try:
self.cur.execute(self.SQL_message)
self.sql_result = self.cur.fetchall()
return self.sql_result
except:
## return SQL error
return self.sql_result
def fetch_once_SQL(self):
try:
self.cur.execute(self.SQL_message)
self.sql_result = self.cur.fetchone()
return self.sql_result
except:
## return SQL error
return self.sql_result
def close_SQL(self):
self.conn.close()