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 7821765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:44:38+00:00 2026-06-02T07:44:38+00:00

For my project, I need to connect to multiple databases and get information from

  • 0

For my project, I need to connect to multiple databases and get information from them. I didn’t think this would be a problem with web2py, but it was. I thought maybe I need to rebuild the db from scratch, but still had problems. Finally, I went through the introductory ‘images’ tutorial and changed it to use an alternate mysql database. I still got the same errors, below is the code:

db.py

db = DAL("mysql://root:@localhost/web2py")
images_db = DAL("mysql://root:@localhost/images_test")


images_db.define_table('image',
   Field('title', unique=True),
   Field('file', 'upload'),
   format = '%(title)s')


images_db.define_table('comment',
   Field('image_id', images_db.image),
   Field('author'),
   Field('email'),
   Field('body', 'text'))

Then I went to the admin page for ‘images’ and clicked the ‘shell’ link under ‘controllers’ and did the following: (after I went to the index page to generate the ‘images’:

Shell:

In [1] : print db(images_db.image).select()
Traceback (most recent call last):
  File "/home/cody/Downloads/web2py/gluon/contrib/shell.py", line 233, in run
    exec compiled in statement_module.__dict__
  File "<string>", line 1, in <module>
  File "/home/cody/Downloads/web2py/gluon/dal.py", line 7577, in select
    fields = adapter.expand_all(fields, adapter.tables(self.query))
  File "/home/cody/Downloads/web2py/gluon/dal.py", line 1172, in expand_all
    for field in self.db[table]:
  File "/home/cody/Downloads/web2py/gluon/dal.py", line 6337, in __getitem__
    return dict.__getitem__(self, str(key))
KeyError: 'image'


In [2] : print images_db.has_key('image')
True


In [3] : print images_db
<DAL {'_migrate_enabled': True, '_lastsql': "SET sql_mode='NO_BACKSLASH_ESCAPES';", '_db_codec': 'UTF-8', '_timings': [('SET FOREIGN_KEY_CHECKS=1;', 0.00017380714416503906), ("SET sql_mode='NO_BACKSLASH_ESCAPES';", 0.00016808509826660156)], '_fake_migrate': False, '_dbname': 'mysql', '_request_tenant': 'request_tenant', '_adapter': <gluon.dal.MySQLAdapter object at 0x2b84750>, '_tables': ['image', 'comment'], '_pending_references': {}, '_fake_migrate_all': False, 'check_reserved': None, '_uri': 'mysql://root:@localhost/images_test', 'comment': <Table {'body': <gluon.dal.Field object at 0x2b844d0>, 'ALL': <gluon.dal.SQLALL object at 0x2b84090>, '_fields': ['id', 'image_id', 'author', 'email', 'body'], '_sequence_name': 'comment_sequence', '_plural': 'Comments', 'author': <gluon.dal.Field object at 0x2b84e10>, '_referenced_by': [], '_format': None, '_db': <DAL {...}>, '_dbt': 'applications/images/databases/e1e448013737cddc822e303fe20f8bec_comment.table', 'email': <gluon.dal.Field object at 0x2b84490>, '_trigger_name': 'comment_sequence', 'image_id': <gluon.dal.Field object at 0x2b84050>, '_actual': True, '_singular': 'Comment', '_tablename': 'comment', '_common_filter': None, 'virtualfields': [], '_id': <gluon.dal.Field object at 0x2b84110>, 'id': <gluon.dal.Field object at 0x2b84110>, '_loggername': 'applications/images/databases/sql.log'}>, 'image': <Table {'ALL': <gluon.dal.SQLALL object at 0x2b84850>, '_fields': ['id', 'title', 'file'], '_sequence_name': 'image_sequence', 'file': <gluon.dal.Field object at 0x2b847d0>, '_plural': 'Images', 'title': <gluon.dal.Field object at 0x2b84610>, '_referenced_by': [('comment', 'image_id')], '_format': '%(title)s', '_db': <DAL {...}>, '_dbt': 'applications/images/databases/e1e448013737cddc822e303fe20f8bec_image.table', '_trigger_name': 'image_sequence', '_loggername': 'applications/images/databases/sql.log', '_actual': True, '_tablename': 'image', '_common_filter': None, 'virtualfields': [], '_id': <gluon.dal.Field object at 0x2b848d0>, 'id': <gluon.dal.Field object at 0x2b848d0>, '_singular': 'Image'}>, '_referee_name': '%(table)s', '_migrate': True, '_pool_size': 0, '_common_fields': [], '_uri_hash': 'e1e448013737cddc822e303fe20f8bec'}>

Now I don’t quite understand why I am getting errors here, everything appears to be in order. I thought web2py supported multiple databases? Am I doing it wrong? The appadmin works fine, perhaps I’ll edit it and get it to raise an error with the code it’s generating… any help would be appreciated.

  • Cody

UPDATE:

I just tried this:

MODELS/DB.PY

db = DAL("mysql://root:@localhost/web2py")

images_db = DAL("mysql://root:@localhost/images_test")


images_db.define_table('image',
   Field('title', unique=True),
   Field('file', 'upload'),
   format = '%(title)s')


images_db.define_table('comment',
   Field('image_id', images_db.image),
   Field('author'),
   Field('email'),
   Field('body', 'text'))

CONTROLLERS/DEFAULT.PY

def index():
    """
    example action using the internationalization operator T and flash
    rendered by views/default/index.html or views/generic.html
    """
    if images_db.has_key('image'):
        rows = db(images_db.image).select()
    else:
        rows = 'nope'
    #rows = dir(images_db)
    return dict(rows=rows)

VIEWS/DEFAULT/INDEX.HTML

{{left_sidebar_enabled,right_sidebar_enabled=False,True}}
{{extend 'layout.html'}}


these are the rows:
{{=rows }}

Again, very confused by all of this. Appreciate any help.

  • 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-06-02T07:44:40+00:00Added an answer on June 2, 2026 at 7:44 am

    If you want to query the images_db connection, you have to call images_db(), not db(). So, it would be:

    images_db(images_db.image).select()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got an upcoming project wherein I will need to connect our website (
my project need to read multiple web pages at a time(eg: for a particular
The Junits I have in my project need to load property files from the
In a php project I need to add items to database, list them &
I am working on a simple hobby project that checks when users connect/disconnect from
I need to connect a SQL Server 2008 database to an asp.net website project.
In my project i need to switch between databases during runtime. I tried to
If i want to use WebAPI as a service to connect to multiple databases
I need to connect to Amazon's SimpleDB in my Android app. The sample project
I am working on a project in which I need to connect to pointbase

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.