Yes you need db.commit() after db.Table1.insert(...) To my understanding you also need in models/file_name.py
# from gluon import current # current.db = db Where you add db to current... Not sure if it still the case with new web2py version but with 2.9.5 I need to do that... Richard On Tue, Dec 8, 2015 at 1:28 PM, Aydin S <[email protected]> wrote: > Here is what I have: > > In model db.py I have: > > db.define_table('Table1', > Field('var1', 'integer'), > from module1 import insert_record > insert_record(db) > > and in a module1 I write on the database and also get access to the > database: > > from gluon import current > def insert_record(db): > db = current.db > test1 = 2 #for example > db.Table1.insert(var=test1) > test2 = db().select(db.Table1.var1, orderby=~db.Table1.id, > limitby=(0, 1))[0].var1 > > Then test2 is used to show up on a display. test2 changes because var1 > changes. The problem is that test2 does seem to get updated when var1 > changes without refreshing the website (I refresh the databases). Is it > because this action is in the models? Should I use scheduler to do this? > > On Tuesday, 8 December 2015 13:15:38 UTC-5, Richard wrote: >> >> Difficult to say without seeing the code, you may need db.commit() or not. >> >> Richard >> >> On Tue, Dec 8, 2015 at 1:05 PM, Aydin S <[email protected]> wrote: >> >>> I got another question if you could help. >>> The database does not get updated without I refresh the website in this >>> scenario. I don't know why it is, do I need to use commit() somewhere or >>> since this is in the models it will not run as the variable inside the >>> module changes? Do I have to use scheduler? Any idea? >>> >>> On Monday, 7 December 2015 18:56:10 UTC-5, Hans Soflao wrote: >>>> >>>> Thanks Anthony and Richard. >>>> Yes, my variable was inside the module and was not a global one. Thanks >>>> for the link, I am running the variable inside the function and it is >>>> local. >>>> I tested the code and it worked. >>>> >>>> On Monday, December 7, 2015 at 12:44:40 PM UTC-5, Richard wrote: >>>>> >>>>> @Hans, what is your question about variable in module exactly? >>>>> >>>>> What you did seems correct to me... >>>>> >>>>> About "db = current.db" be aware of that : >>>>> http://web2py.com/books/default/chapter/29/04/the-core#Warning--Do-not-use-the-current-object-in-global-scope-in-a-module >>>>> >>>>> On Mon, Dec 7, 2015 at 12:13 PM, Hans Soflao <[email protected]> >>>>> wrote: >>>>> >>>>>> Thank you, I used the method Anthony mentioned and it worked. >>>>>> Now a follow up question, in order to have access to database in this >>>>>> module to assign var1 (defined in the database) to var2 (defined in the >>>>>> module) what is the best method? >>>>>> >>>>>> For instance to have access to the latest row of data in the table1 >>>>>> defined in the model as >>>>>> db.define_table('Table1', >>>>>> Field('F1', 'integer')) >>>>>> >>>>>> should I have in the model the following?: >>>>>> >>>>>> from gluon import current >>>>>> current.db = db >>>>>> >>>>>> and in the module module1.py?: >>>>>> >>>>>> def module1(): >>>>>> db = current.db >>>>>> var2=db().select(db.Table1.var1, orderby=~db.Table1.id, >>>>>> limitby=(0, 1))[0].var1 >>>>>> >>>>>> Thanks >>>>>> >>>>>> >>>>>> >>>>>> On Friday, December 4, 2015 at 9:39:35 AM UTC-5, Aydin S wrote: >>>>>> >>>>>>> This might be asked before and I already found a similar question in >>>>>>> here: https://groups.google.com/forum/#!topic/web2py/0k0Fvw6fmb8 >>>>>>> but this seemed a bit confusing. How to write on database from >>>>>>> within a module? >>>>>>> I have a table defined in db.py (model) as follows: >>>>>>> db.define_table('Table1', >>>>>>> Field('F1', 'integer')) >>>>>>> >>>>>>> Now I have a variable var1 in the module module1.py that I want to >>>>>>> write it into F1. >>>>>>> >>>>>>> var1=2 >>>>>>> >>>>>>> Is this similar approach to read a variable from database in a >>>>>>> module? >>>>>>> >>>>>>> >>>>>>> >>>>>>> -- >>>>>> 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 the Google >>>>>> Groups "web2py-users" group. >>>>>> To unsubscribe from this group and stop receiving emails from it, >>>>>> send an email to [email protected]. >>>>>> For more options, visit https://groups.google.com/d/optout. >>>>>> >>>>> >>>>> -- >>> 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 the Google >>> Groups "web2py-users" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> -- > 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 the Google Groups > "web2py-users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- 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 the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.

