-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.py
More file actions
21 lines (18 loc) · 767 Bytes
/
Database.py
File metadata and controls
21 lines (18 loc) · 767 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import sqlite3
class Database:
def __init__(self, dbname):
self.connection = sqlite3.connect(dbname)
self.cursor = self.connection.cursor()
def create_table(self, tablename, columns):
types = ",".join(["%s %s" % column for column in columns])
self.cursor.execute("CREATE TABLE IF NOT EXISTS {} ({})".format(tablename, types))
def insert(self, tablename, values):
marks = ",".join(["?" for i in values])
try:
self.cursor.execute("INSERT INTO {} VALUES ({})".format(tablename, marks), values)
except sqlite3.IntegrityError:
raise
else:
print("INSERT INTO {} VALUES ({})".format(tablename, *values))
def commit(self):
self.connection.commit()