title: SQLAlchemy gives "MetaData not bound to Engine or Connection"
body: <p>I have this code to create a database:</p>
  
  <pre><code>from sqlalchemy import create_engine
  from sqlalchemy import Table, Column, Integer, String, Float, MetaData, ForeignKey
  from sqlalchemy.sql import select, and_
  from PyQt4 import QtGui, QtCore
  
  class DbUtils(object):
      def __init__(self, db_file = None, parent = None):
  
          self.db = None
          self.db_connection = None
          self.db_file = str(db_file)
  
      def db_open(self):
          self.db = create_engine('sqlite:///' + self.db_file)
          self.db_connection = self.db.connect()
  
      def db_close(self):
          self.db_connection.close()
  
      def db_create_voltdrop(self):
          metadata = MetaData()
  
          tb_cable_brands = Table('cable_brands', metadata,
              Column('id', Integer, primary_key=True),
              Column('brand', String)
              )
          tb_cable_types = Table('cable_types', metadata,
              Column('id', Integer, primary_key=True),
              Column('brand_id', None, ForeignKey('cable_brands.id')),
              Column('type', String),
              Column('alpha', String)
              )
          tb_cable_data = Table('cable_data', metadata,
              Column('id', Integer, primary_key=True),
              Column('type_id', None, ForeignKey('cable_types.id')),
              Column('size', String),
              Column('resistance', Float)
              )
          metadata.create_all(self.db) # db_utils.py", line 67
  </code></pre>
  
  <p>When it was part of my GUI's QMainWindow class, it worked fine, no errors. But when I separated it from GUI into a separate module, it started giving me that error. Here's a traceback: </p>
  
  <pre><code>Traceback (most recent call last):
  File "C:\Temp\xxx\applications\voltdrop\mainwindow.py", line 52, in __init__ 
      self.main_ui()
  File "C:\Temp\xxx\applications\voltdrop\mainwindow.py", line 212, in main_ui 
      self.db_utils.db_create_voltdrop()
  File "C:\Temp\xxx\scripts\db_utils.py", line 67, in db_create_voltdrop 
      metadata.create_all(self.db)
  File "C:\Temp\PortablePython\App\lib\site-packages\sqlalchemy\schema.py", line 2511, in create_all 
      bind = _bind_or_error(self)
  File "C:\Temp\PortablePython\App\lib\site-packages\sqlalchemy\schema.py", line 3124, in _bind_or_error 
      raise exc.UnboundExecutionError(msg)
  
  sqlalchemy.exc.UnboundExecutionError: 
  The MetaData is not bound to an Engine or Connection.  
  Execution can not proceed without a database to execute against.  
  Either execute with an explicit connection or assign the MetaData's .bind to enable implicit execution.
  
  
      self.db_utils = DbUtils(self.cabledata_db, self)
      self.main_ui() # mainwindow.py", line 52
  
  def main_ui(self):
  ...
      if not self.find_data_file(self.cabledata_db):
          file = self.select_data_file("cable")
          if file:
              self.cabledata_db = file
              self.db_utils.db_open()
          else:
              self.db_utils.db_create_voltdrop() # mainwindow.py", line 212
  </code></pre>
  
  <p>What am I doing wrong? Thank you.</p>
  
  <p>[SOLVED]</p>
  
  <p>Thank you. It works like this: if there's no DB file, open dialog and select one, if the dialog is cancelled, create an empty DB. If the dialog selects a file, use that DB. Otherwise, use the found DB file.</p>
  
  <p>This works now:</p>
  
  <pre><code>    if not self.find_data_file(self.cabledata_db):
          file = self.select_data_file("cable")
          if file:
              self.cabledata_db = file
              self.db_utils.db_open()
          else:
              self.db_utils.db_open()
              self.db_utils.db_create_voltdrop()
      else:
          self.db_utils.db_open()
  </code></pre>