Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7082733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:05:27+00:00 2026-05-28T07:05:27+00:00

I have this code to create a database: from sqlalchemy import create_engine from sqlalchemy

  • 0

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:05:28+00:00Added an answer on May 28, 2026 at 7:05 am

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im create this code to store page view in database, but i have problem
I have this code to create a webapp in my server: import web urls
So, I have this code that grabs a bunch of data from the database,
I try to create database from entity framework code first follow with this tutorial
I have to create JSON string from database values and push it back to
I have this code in my DataBase Helper: public ArrayList<ArrayList<Object>> getAllRowsAsArrays() { // create
I have this code to connect to mysql and get data from one table
I have made this code to open a database(created in SQLite browser) stored in
I have this code to create an ATOM feed Dim xmlResult As New StringBuilder
I have this code which should create a splash image with either no animation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.