equal
deleted
inserted
replaced
1 #!/usr/bin/env python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 import MySQLdb |
|
5 import sqlite3 |
|
6 class Database(object): |
|
7 def __init__(self,connection): |
|
8 self.conn=None |
|
9 self.cursor=None |
|
10 self.connection=connection |
|
11 self.testConnection() |
|
12 |
|
13 def testConnection(self): |
|
14 self.connect() |
|
15 self.disconnect() |
|
16 |
|
17 def getConn(self): |
|
18 return self.conn |
|
19 |
|
20 def getCursor(self): |
|
21 return self.cursor |
|
22 |
|
23 def connect(self): |
|
24 if not self.getConn(): |
|
25 if (self.connection['type']=='mysql'): |
|
26 self.conn=MySQLdb.connect( |
|
27 host = self.connection["host"], |
|
28 db = self.connection["db"], |
|
29 user = self.connection["user"], |
|
30 passwd = self.connection["passwd"], |
|
31 ) |
|
32 if (self.connection['type']=='sqlite'): |
|
33 self.conn=sqlite3.connect(self.connection['path']) |
|
34 |
|
35 if not self.getCursor(): |
|
36 self.cursor = self.conn.cursor() |
|
37 |
|
38 def disconnect(self): |
|
39 if self.getCursor(): |
|
40 self.cursor.close() |
|
41 self.cursor=None |
|
42 if self.getConn(): |
|
43 self.conn.close() |
|
44 self.conn=None |
|
45 |
|
46 |
|