gpt4 book ai didi

python - SQLAlchemy 给出 "MetaData not bound to Engine or Connection"

转载 作者:太空宇宙 更新时间:2023-11-04 07:45:35 25 4
gpt4 key购买 nike

我有这段代码来创建一个数据库:

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

当它是我的 GUI 的 QMainWindow 类的一部分时,它工作正常,没有错误。但是当我将它从 GUI 分离到一个单独的模块时,它开始给我那个错误。这是一个回溯:

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

我做错了什么?谢谢。

[已解决]

谢谢。它是这样工作的:如果没有数据库文件,打开对话框并选择一个,如果对话框被取消,则创建一个空数据库。如果对话框选择了一个文件,则使用该数据库。否则,使用找到的数据库文件。

这现在有效:

    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()

最佳答案

如果不是文件,则在调用 db_utils.db_create_voltdrop() 之前不调用 db_utils.db_open()

因此,当调用 metadata.create_all(self.db) 时,db_utils.db 的值为 None。确保在调用 db_utils.db_create_voltdrop() 之前调用 db_utils.db_open() 应该可以解决您的问题。

metadata.create_all(None) 引发 sqlalchemy.exc.UnboundExecutionError 而不是 ValueError 的原因是它们允许您这样做:

metadata.bind = self.db
metadata.create_all()

关于python - SQLAlchemy 给出 "MetaData not bound to Engine or Connection",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8903343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com