lobjc
lobjc

Reputation: 2961

How to pass sqlite db values to controller

Newbie in sqlite, web2py. Am trying to copy sqlite db contents to controller for another function. My codes are these: db.py

auth.define_tables(username=False, signature=False)

auth.settings.registration_requires_verification = False
auth.settings.registration_requires_approval = False
auth.settings.reset_password_requires_verification = True

db.define_table('chat',
        Field('me_from'),
        Field('me_body', 'text'),

default:

@auth.requires_login()
def index():
    chats.index(db)    
    body = db.chat.me_body()    
    rows = db(db.chat.me_body).select()#(orderby=~db.chat.me_body.created_on)    
    form1 = [body]    
    form5 = form1#.split()    
    name3 = ' '.join(form5)

I want t retrieve a string posted to db..

    chat.id chat.me_from    chat.me_body    chat.me_html
1                      
2       maurice         hi              <div class="m...
3       maurice         whats up        <div class="m...
4       maurice         where are you.  <div class="m...
5       maurice         3i5ejp[eoityjdt <div class="m...
6       maurice         how are you d...<div class="m...
7       maurice         britam          <div class="m...

From the table above, i want to retrieve from chat.me_body the posted words such as, 'hi', 'whats up', 'britam'... to default function. I instead keep getting the following errors: If I use: body = db.chat.me_body() the error is:

TypeError: 'Field' object is not callable

If I use:

`body = db.chat.me_body

The error is:

TypeError: sequence item 0: expected string, Field found

If I use rows:

rows = db(db.chat.me_body).select()

and

form1 = [rows]

the error is:

name3 = ' '.join(form5)
TypeError: sequence item 0: expected string, Rows found

I I'll appreciate your help

Upvotes: 0

Views: 178

Answers (1)

Anthony
Anthony

Reputation: 25536

The proper way to do a query to get records from the database is:

rows = db(db.chat).select(db.chat.me_body, db.chat.created_on,
                          orderby=~db.chat.created_on)

Note, db(db.chat) is shorthand for the query db(db.chat.id != None) (i.e., selection of all records in the table).

Then, to extract just the me_body field values into a list, you can use a list comprehension:

me_body_values = [r.me_body for r in rows]

Finally, you can join those values together:

name3 = ' '.join(me_body_values)

Upvotes: 1

Related Questions