I have this code to create a database:
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
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:
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
What am I doing wrong? Thank you.
[SOLVED]
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.
This works now:
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()
You are not calling db_utils.db_open() before calling db_utils.db_create_voltdrop() if not file.
Hence the value of the db_utils.db is None when metadata.create_all(self.db) is called. Ensuring that you call db_utils.db_open() before you call db_utils.db_create_voltdrop() should fix your problem.
The reason that metadata.create_all(None) raises a sqlalchemy.exc.UnboundExecutionError, rather than an ValueError is that they allow you to do this:
metadata.bind = self.db
metadata.create_all()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With