iro/database.py
author Sandro Knauß <knauss@netzguerilla.net>
Sat, 10 Mar 2012 19:01:31 +0100
branchdevel
changeset 220 602720314930
parent 56 3718abbfffd6
permissions -rw-r--r--
adding config.Config.sampleConf for creating a sample configuration

#!/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