-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabasesrc.py
More file actions
60 lines (43 loc) · 1.62 KB
/
Copy pathdatabasesrc.py
File metadata and controls
60 lines (43 loc) · 1.62 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
import sqlite3
from sqlite3 import Error
# Function to set up database connection
def sql_connection():
try:
con = sqlite3.connect('contents.db')
return con
except Error:
print(Error)
# Function to create Table in database
def sql_table(con):
cursorObj = con.cursor()
cursorObj.execute("CREATE TABLE contents(barcode integer PRIMARY KEY, name text)")
con.commit()
class Database:
def __init__(self, db):
self.conn = sqlite3.connect(db)
self.cur = self.conn.cursor()
self.cur.execute(
"CREATE TABLE IF NOT EXISTS routers (id INTEGER PRIMARY KEY, hostname text, brand text, ram integer, flash integer)")
self.conn.commit()
def fetch(self, hostname=''):
self.cur.execute(
"SELECT * FROM routers WHERE hostname LIKE ?", ('%'+hostname+'%',))
rows = self.cur.fetchall()
return rows
def fetch2(self, query):
self.cur.execute(query)
rows = self.cur.fetchall()
return rows
def insert(self, hostname, brand, ram, flash):
self.cur.execute("INSERT INTO routers VALUES (NULL, ?, ?, ?, ?)",
(hostname, brand, ram, flash))
self.conn.commit()
def remove(self, id):
self.cur.execute("DELETE FROM routers WHERE id=?", (id,))
self.conn.commit()
def update(self, id, hostname, brand, ram, flash):
self.cur.execute("UPDATE routers SET hostname = ?, brand = ?, ram = ?, flash = ? WHERE id = ?",
(hostname, brand, ram, flash, id))
self.conn.commit()
def __del__(self):
self.conn.close()