iro/database.py
author Sandro Knauß <knauss@netzguerilla.net>
Wed, 21 Dec 2011 22:07:48 +0100
changeset 90 eb04ac3a8327
parent 56 3718abbfffd6
permissions -rw-r--r--
merging

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import MySQLdb
import sqlite3
class Database(object):
    def __init__(self,connection):
        self.conn=None
        self.cursor=None
        self.connection=connection
        self.testConnection()

    def testConnection(self):
        self.connect()
        self.disconnect()

    def getConn(self):
        return self.conn

    def getCursor(self):
        return self.cursor

    def connect(self):
        if not self.getConn():
            if (self.connection['type']=='mysql'):
                self.conn=MySQLdb.connect(
                    host   = self.connection["host"],
                    db     = self.connection["db"],
                    user   = self.connection["user"],
                    passwd = self.connection["passwd"],
                    )
            if (self.connection['type']=='sqlite'):
                self.conn=sqlite3.connect(self.connection['path'])

        if not self.getCursor():
            self.cursor = self.conn.cursor()

    def disconnect(self):
        if self.getCursor():
            self.cursor.close()
        self.cursor=None
        if self.getConn():
            self.conn.close()
        self.conn=None