Monday, November 22, 2010

PyGame and GUI Programming - Multiple Database Access - Part I

OK!! now that I have started with the actual coding part of it. Here is my folder structure

scripts/m_db_access/
server.py
connect_server.py

the server.py file will contain the scripts that i am using to identify the type of database that i will connect, I have just made a sample of MYSQL(because that is the only database that I have). here is the coding part of it



class init_connection():
    '''
    This class is used to initialize the connection to any kind of database,based on the database that we set in the variable "server_type" the respective module will be loaded
    '''
    def get_connection(self,server_type):
        '''
        server_type -> the type of the server to connect (MYSQL/ORACLE/MSSQL)
        '''
        if server_type == 'MYSQL':
            import connect_server
            return connect_server.databaseOps('localhost','root','xxxx','DBNAME')
        else:
            import somethingelse #some other import statements

       
def main():
    '''
    Test method to check the module is working or not, we can add more cases as the class grows
    '''
    conn_type = init_connection()
    conn = conn_type.get_connection('MYSQL')

    print conn.db
    print 'Connection successful !!!'

#this peice of code is to tell the interpreter that execute the MAIN method only# when accessed through command line and not during IMPORT
if __name__ == '__main__':
    main()

No comments:

Post a Comment