Multiple connections/ databases with same app

1,227 views
Skip to first unread message

Simon Ashley

unread,
Jan 2, 2013, 9:56:50 AM1/2/13
to web...@googlegroups.com
Quick question - is this do able i.e. some tables - e.g. db = DAL('sqlite://storage.sqlite....),  other tables - db1 = DAL('mysql://root: ....) etc, within the same application/controller/function?
If so, will write up an issue we're having.

TIA

Alec Taylor

unread,
Jan 2, 2013, 10:23:55 AM1/2/13
to web...@googlegroups.com
Yep, that's possible.

Feature trivial to utilise in web2py; difficult or unimplemented with other frameworks.

--
 
 
 

Message has been deleted

Alec Taylor

unread,
Jan 2, 2013, 5:05:27 PM1/2/13
to web...@googlegroups.com


On Wed, Jan 2, 2013 at 10:53 PM, Simon Ashley <greg...@gmail.com> wrote:
Ok then, should this work?
(Version 2.3.2 (2012-12-17 15:03:30) stable, source, nightly build, from 12/31)

Connection
db = DAL('sqlite://storage.sqlite',pool_size=1,check_reserved=['all'])
db1
= DAL('mysql://root:www@localhost:3306/xxx', migrate=True, fake_migrate=False, check_reserved=['common','mysql'])  #www and xxx's have been blanked (place markers)

Model
db.define_table('luPace',
 
Field('Description','string', label='Pace'),
 
Field('icon','string', length=100),
 
Field('bgcolor','string'),
 
Field('fgcolor','string'))

db1
.define_table('luStart1',
 
Field('Description','string', label='Start'),
 
Field('icon','string'),
 
Field('bgcolor','string'),
 
Field('fgcolor','string'))

Controller
def pace():
    tablel
= SQLFORM.grid(db.luPace)
   
return locals()

def start():
    tablel
= SQLFORM.grid(db.luStart1)
   
return locals()

You'll want to change the first line of that second function:

    tablel = SQLFORM.grid(db1.luStart1)

 
Execute the following:

pace ..

as expected


start ....
Traceback (most recent call last):
 
File "C:\web2py15\gluon\restricted.py", line 212, in restricted
 
exec ccode in environment
 
File "C:/web2py15/applications/test1/controllers/test.py", line 13, in <module>
 
File "C:\web2py15\gluon\globals.py", line 193, in <lambda>
 
self._caller = lambda f: f()
 
File "C:/web2py15/applications/test1/controllers/test.py", line 9, in start
 tablel
= SQLFORM.grid(db.luStart1)
 
File "C:\web2py15\gluon\dal.py", line 7255, in __getattr__
 
return ogetattr(self, key)
AttributeError: 'DAL' object has no attribute 'luStart1'

luStart1 works from appadmin

--
 
 
 

Simon Ashley

unread,
Jan 2, 2013, 11:14:40 PM1/2/13
to web...@googlegroups.com
Sorry Alec,

I saw that just as posted it and tried to delete it. It came through again afterwards. 
This wasn't the issue, just an example returning the same error. 
Solved it ... was in the query=db(....) statements, which should have been changed to query=db1(....) etc.

Thanks for your reply.


Shayn Raney

unread,
Mar 12, 2016, 2:10:33 PM3/12/16
to web...@googlegroups.com
I would like to implement something like this to use two sqlite3 databases. I've tried to create the following within a new model via the web admin gui and did not see the new tables under the 'Available Databases and Tables' appadmin page.  Does the 'Available Databases and Tables' page only show the items within the 'storage.sqlite' file?

db-cats = DAL('sqlite://cats.sqlite', poolsize = 1)

db
-cats.define_table('names',
   
Field('name', 'string'),
   
Field('age', 'integer'),
   
Field('account', 'integer')
   
)

db
-cats.define_table('owner',
   
Field('fname', 'string'),
   
Field('lname', 'string'),
   
Field('phone', 'string')
)
       

Paolo Caruccio

unread,
Mar 13, 2016, 11:08:24 AM3/13/16
to web2py-users
Try to replace dash ("-") with uderscore ("_") in the variable names i.e. db_cats

Shane Raney

unread,
Mar 13, 2016, 4:12:17 PM3/13/16
to web...@googlegroups.com
Even If I rename the db's to just 'Cats' I see the same issue. 
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/IbBzVyeCSFg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paolo Caruccio

unread,
Mar 13, 2016, 4:31:21 PM3/13/16
to web...@googlegroups.com

Shane Raney

unread,
Mar 13, 2016, 4:34:32 PM3/13/16
to web...@googlegroups.com
Opps, that was a typo on my behalf while reproducing the code on my iPad. Good catch though. 


On Sun, Mar 13, 2016 at 9:31 AM, Paolo Caruccio <paolo.ca...@gmail.com> wrote:
and by replacing "poolsize" to "pool_size"? 



--
---------------------------------------
1. break nothing but silence
2. take nothing but pictures
3. leave nothing but footprints
----------------------------------------

Shane Raney

unread,
Mar 13, 2016, 4:35:44 PM3/13/16
to web...@googlegroups.com
I did notice, if I pull the DB generation from a .py file in modules and move it into the the db.py file, I see it inside the web management gui. 

Sankhajit Das

unread,
Jan 17, 2017, 3:29:44 PM1/17/17
to web2py-users

hey. I have created a mysql database in my web2py app.

now i want to create another sqlite database such that whatever data i enter 
in sqlite database. it automatically gets copied in mysql database.
Pls somebody help me do ds with full code.
  

Dave S

unread,
Jan 17, 2017, 9:13:54 PM1/17/17
to web2py-users
Assignment due soon?  See my comments in the other thread.

/dps
 
Reply all
Reply to author
Forward
0 new messages