iro/database.py
author Sandro Knauß <knauss@netzguerilla.net>
Sat, 08 Oct 2011 12:32:58 +0200
changeset 66 eabb8ead183d
parent 56 3718abbfffd6
permissions -rw-r--r--
Added dump_test_log.py from http://hieuhacking.blogspot.com/2009/09/log-rotation-for-python-process.html Added testloglock.py from http://cherrypy.org/attachment/ticket/679/testloglock.py

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