title: How to avoid globals with sqlalchemy
body: <p>In the general explanation of sqlalchemy there is some basic setup where some globals are used. But how to avoid these globals?
  <br> <br>
  This is the basic setup:</p>
  <pre><code>import sqlalchemy
  from sqlalchemy import create_engine
  from sqlalchemy import Column, Integer, String
  from sqlalchemy.ext.declarative import declarative_base
  from sqlalchemy.orm import sessionmaker
  
  databaseName = 'fixedDBName.db'
  engine = create_engine('sqlite:///' + databaseName, echo=True)
  Base = declarative_base()
  Base.metadata.create_all(engine)
  Session = sessionmaker(bind=engine)
  
  from sqlalchemy import Column, Integer, String
  class User(declarative_base()):
      __tablename__ = 'users'
  
      id = Column(Integer, primary_key=True)
      name = Column(String)
      fullname = Column(String)
      nickname = Column(String)
  
      def __repr__(self):
          return &quot;&lt;User(name='%s', fullname='%s', nickname='%s')&gt;&quot; % (
              self.name, self.fullname, self.nickname)
  
  #Now you can create a session
  Session = sessionmaker(bind=engine)
  session = Session()
  </code></pre>
  <p>Now this rises some questions for me:
  <br>
  Can i just start a database connection when i need it, close it afterwards and start a new one when i need it again?
  <br>
  How do you use it in your projects?</p>
  <p>Do you pack everything that has something to do with the database in a class. So that you can create an instance of that function? Like this:</p>
  <pre><code>class sqlcon():
      def __init__(self, databaseName):
          self.engine = create_engine('sqlite:///' + databaseName, echo=True)
          self.Session = sessionmaker(bind=self.engine)
          
      Base = declarative_base()
          
      class User(Base):
          __tablename__ = 'users'
  
          id = Column(Integer, primary_key=True)
          name = Column(String)
          fullname = Column(String)
          nickname = Column(String)
  
          def __repr__(self):
              return &quot;&lt;User(name='%s', fullname='%s', nickname='%s')&gt;&quot; % (
                  self.name, self.fullname, self.nickname)
  
      def createTables(self):
          Base.metadata.create_all(self.engine) #doesn´t work - Base is not Defined
          
      Base.metadata.create_all(self.engine) #doesn´t work - self is not Defined
  </code></pre>
  <p>Or
  Do you pack this inside a function:</p>
  <pre><code>def sqlcon(databaseName):
      
     engine = create_engine('sqlite:///' + databaseName, echo=True)
         
     Base = declarative_base()
         
     class User(Base):
         __tablename__ = 'users'
  
         id = Column(Integer, primary_key=True)
         name = Column(String)
         fullname = Column(String)
         nickname = Column(String)
  
         def __repr__(self):
             return &quot;&lt;User(name='%s', fullname='%s', nickname='%s')&gt;&quot; % (
                 self.name, self.fullname, self.nickname)
  
     Base.metadata.create_all(engine) 
  
  databaseName = &quot;test_3.db&quot;
  alchemy = sqlcon(databaseName)
  </code></pre>
  <p>But then how to create a session and how to access the class User?</p>
  <p>Here I need a push in the right direction :/</p>