I cant access db data

55 views
Skip to first unread message

Maurice Waka

unread,
Apr 17, 2018, 12:04:35 AM4/17/18
to web2py-users
from this link, i tried accessing values in db as follows:


model:

db.define_table('abnvalinterprets',
   
Field('abnvalinterpret', length= 1000000),)


Module:

from gluon import current, DAL, Fielddef abnvalinterpret():
    db
= current.db
    db
.define_table('abnvalinterprets',Field('abnvalinterpret', 'text', length=1000000))
    rows  
= db().select(db.abnvalinterprets.abnvalinterpret.ALL)
   
for row in rows:
       
print row.abnvalinterpret
abnvalinterpret
()




I keep getting this error:
line 4, in abnvalinterpret
    db
= current.db
AttributeError: 'thread._local' object has no attribute 'db'

Am testing if my module can access/print out data from the database. I hope that is the correct function too.
Regards

Dave S

unread,
Apr 17, 2018, 12:26:46 AM4/17/18
to web2py-users


On Monday, April 16, 2018 at 9:04:35 PM UTC-7, Maurice Waka wrote:
from this link, i tried accessing values in db as follows:


model:

db.define_table('abnvalinterprets',
   
Field('abnvalinterpret', length= 1000000),)


Module:

from gluon import current, DAL, Field

[missing newline inserted]
 
def abnvalinterpret():
    db
= current.db
    db
.define_table('abnvalinterprets',Field('abnvalinterpret', 'text', length=1000000))
    rows  
= db().select(db.abnvalinterprets.abnvalinterpret.ALL)
   
for row in rows:
       
print row.abnvalinterpret
abnvalinterpret
()




I keep getting this error:
line 4, in abnvalinterpret
    db
= current.db
AttributeError: 'thread._local' object has no attribute 'db'

Am testing if my module can access/print out data from the database. I hope that is the correct function too.
Regards


Did you set current.db in the controller that calls your module?

Also, I think you shouldn't re-define the table in your module.

/dps

Maurice Waka

unread,
Apr 17, 2018, 12:31:24 AM4/17/18
to web...@googlegroups.com
I hadn't done that. I'll redo
Regards

--
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/wBVbeSdbc9o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Maurice Waka

unread,
Apr 17, 2018, 1:33:29 AM4/17/18
to web...@googlegroups.com
Sorry, am still experiencing the same issue.
This module is one of the many that I import to the controller .
i.e.

controller:
from gluon import current
db = current.db
from ..path/to/module import my_module
some code..
def access_this():
    class Filters(object):
        @staticmethod
        def Filter_read():
            for item in strings.split():
                for c in abnvalinterpret: here i access the data: strings, lists etc. The data is then accessed by the function below
                    return my_module()
    

Anthony

unread,
Apr 17, 2018, 8:37:09 AM4/17/18
to web2py-users
On Tuesday, April 17, 2018 at 1:33:29 AM UTC-4, Maurice Waka wrote:
Sorry, am still experiencing the same issue.
This module is one of the many that I import to the controller .
i.e.

controller:
from gluon import current
db = current.db

There is nothing in web2py that automatically adds the db object (or any object created by your own code) to the current object, so you must do this yourself. The above line should instead be the opposite assignment:

current.db = db

In other words, in a model file or controller, you can add objects to current, and then in a module, you can import current and access those objects.

Alternatively, any object in the web2py global environment (including any created in the model files) can also be accessed via the current.globalenv dictionary. For example:

current.globalenv['db']

Anthony

Maurice Waka

unread,
Apr 17, 2018, 12:10:35 PM4/17/18
to web...@googlegroups.com
Thanks. It worked. 

--
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/wBVbeSdbc9o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages