Trying to use databases

35 views
Skip to first unread message

Tomás Acauan Schertel

unread,
Oct 11, 2011, 3:03:32 PM10/11/11
to we...@googlegroups.com
Hi there.

I'm having problems to make databasing work (sqlite).
I have a class with this function on it:
def POST():
    i = web.input()
    db.insert('users', name=i.name, email=i.email, password=i.password, groups=i.groups[])

I can't even see the page.
Browser says:

serve.py", line 37
    db.insert('users', name=i.name, email=i.email, password=i.password, groups=i.groups[])
                                                                                        ^
SyntaxError: invalid syntax

What could be wrong?
I checked field names from html page and it's all right.


--
Tomás A. Schertel
----------------------------------------------
Linux Registered User #304838
Arch Linux User
http://www.archlinux-br.org/
----------------------------------------------

Alice Atlas

unread,
Oct 11, 2011, 4:08:40 PM10/11/11
to we...@googlegroups.com
I think you'll want to change "i = web.input()" to "i = web.input(groups=[])", and "groups=i.groups[]" to "groups=i.groups", if I'm understanding the intention correctly. The former change tells web.input that "groups" may have multiple values and should be given as a list; the latter fixes the syntax error (i.groups[] is not valid python; you just use i.groups to refer to the object attached to the key "groups" in i, whether that's a list or a scalar object).

If you actually have the form variable named "groups[]" in your HTML, you should change it to just "groups".

Alice Atlas

unread,
Oct 11, 2011, 4:10:41 PM10/11/11
to we...@googlegroups.com

Anand Chitipothu

unread,
Oct 11, 2011, 7:47:12 PM10/11/11
to we...@googlegroups.com
2011/10/12 Tomás Acauan Schertel <tsch...@gmail.com>:

> Hi there.
>
> I'm having problems to make databasing work (sqlite).
> I have a class with this function on it:
> def POST():
>     i = web.input()
>     db.insert('users', name=i.name, email=i.email, password=i.password,
> groups=i.groups[])
>
> I can't even see the page.
> Browser says:
>
> serve.py", line 37
>     db.insert('users', name=i.name, email=i.email, password=i.password,
> groups=i.groups[])
>
> ^
> SyntaxError: invalid syntax
>
> What could be wrong?

i.groups[] is not a valid python syntax. You need to do it like this:

i = web.input(groups=[])


db.insert('users', name=i.name, email=i.email, password=i.password,

groups=i.groups)

Anand

Tomás Acauan Schertel

unread,
Oct 12, 2011, 6:39:03 PM10/12/11
to we...@googlegroups.com
Hey Anand. Thanks for your reply.
But I'm still getting error when I submit my form.

<type 'exceptions.TypeError'> at /users/
POST() takes no arguments (1 given)

Thank you.

--
Tomás A. Schertel
----------------------------------------------
Linux Registered User #304838
Arch Linux User
http://www.archlinux-br.org/
----------------------------------------------



--
You received this message because you are subscribed to the Google Groups "web.py" group.
To post to this group, send email to we...@googlegroups.com.
To unsubscribe from this group, send email to webpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webpy?hl=en.


Ben Corneau

unread,
Oct 12, 2011, 10:14:30 PM10/12/11
to we...@googlegroups.com
POST is a member function so you must include self.

def POST(self):

Tomás Acauan Schertel

unread,
Oct 13, 2011, 12:35:15 PM10/13/11
to we...@googlegroups.com
Thank you Ben
The error is gone.




--
Tomás A. Schertel
----------------------------------------------
Linux Registered User #304838
Arch Linux User
http://www.archlinux-br.org/
----------------------------------------------


Tomás Acauan Schertel

unread,
Oct 18, 2011, 10:05:58 PM10/18/11
to we...@googlegroups.com
Hi There, me again.

When I POST data to my db.insert, I get this error message:
Error binding parameter 2 - probably unsupported type

I've tried remove fields that has default values from insertion, but no way to figure how solve this.
I've reviewed my form fields vs batabase fields, but I presume it's all right.
Is there any how I can discover what's wrong??



--
Tomás A. Schertel
----------------------------------------------
Linux Registered User #304838
Arch Linux User
http://www.archlinux-br.org/
----------------------------------------------


Tomas Schertel

unread,
Oct 19, 2011, 1:40:06 PM10/19/11
to we...@googlegroups.com
Just tried changing database from sqlite to mysql and aI got this error:

<class '_mysql_exceptions.OperationalError'> at /users/
(1241, 'Operand should contain 1 column(s)')

I'm sending a form with few fields: name, email, password, an array...
<form action="/users/" method="post">
<fieldset  style="width: 500px; height: 500px">
<legend>Informacoes pessoais:</legend>

<label for="name">Nome:</label>
    <input type="text" name="name" /><br />

<label for="email">Email:</label>
    <input type="text" name="email" /><br />

<label for="password">Senha:</label>
    <input type="password" name="password" /><br /><br />

<fieldset  style="width: 300px; height: 70px">
<legend>Informacoes pessoais:</legend>
<input type="checkbox" name="groups" value="administradores" />Administradores<br />

<input type="checkbox" name="groups" value="operadores" />Operadores<br />

<input type="checkbox" name="groups" value="usuarios" checked/>Usu&aacute;rios<br />
</fieldset>
<br/>
<br />
<input type="checkbox" name="is_active" value="1" />Ativo<br />
<input type="submit" value="Enviar" />

This is my code:
def POST(self):
        i = web.input(groups=[])
        db.insert("users", email = i.email, name = i.name, password = i.password, groups = i.groups)
        raise web.seeother('/users/')

Is there any thing wrong here?

Tomas Schertel

unread,
Oct 19, 2011, 1:54:22 PM10/19/11
to we...@googlegroups.com
Found where was the error.
I removed groups from form (and from db.insert) and it's working.
But how can I work with checkboxes?
Isn't a checkbox (with more than one option) an array?
Reply all
Reply to author
Forward
0 new messages