1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
from contextlib import closing
import MySQLdb
class DatabaseConnection:
def __init__(self, conn):
self.conn = conn
def insert_file(self, path, group_id):
with closing(self.conn.cursor()) as c:
c.execute("insert into `files` (`path`, `group_id`) values (%s, %s)", (path, group_id))
self.conn.commit()
return c.lastrowid
def insert_group(self, name, provider, date):
with closing(self.conn.cursor()) as c:
c.execute("insert into `groups` (`name`, `provider`, `date`) values (%s, %s, %s)", (name, provider, date))
self.conn.commit()
return c.lastrowid
def get_connection(user, passwd, db):
conn = MySQLdb.connect(user=user, passwd=passwd, db=db)
return DatabaseConnection(conn)
|