Question

I just started to develop with web2py and python stuff. On web2py documentation i followed the instructions to connect to MySQL database.

db = DAL('mysql://user:passs@host/test_base')

Ex: On MySQL i have a table named "users" If i define table db.define_table('users'...) i got this error:

class 'gluon.contrib.pymysql.err.Error'>
(<type 'exceptions.UnicodeDecodeError'>, 
UnicodeDecodeError('utf8', "Tabela 'users already exist", 19, 20, 
'invalid continuation byte'))

And if i dont define the table i got this:

"<type 'exceptions.AttributeError'> 'DAL' object has no attribute 'users'"

My question here is: how to acess the table and the data from the remote database?

Was it helpful?

Solution

If I'm understanding your correctly, you have a preexisting table called 'users'. If this is the case then you're running up against the fact that, by default, web2py expects to handle your database for you (defining relations, properties, etc).

One fix, if you know the layout of your database is to first define your models in web2py to match exactly what is in your tables; run it once to generate the sql.log; modify your models; run it again.

Another is to turn off migration so that it doesn't try to modify the database structure. This means that you loose the wonderfully useful auto-migrations and have to do the ALTER statements yourself.

Which ever you choose I'd strongly recommend that you first take a quick read through of the Migrations section of Chapter 6 of the web2py manual: http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Migrations

I hope that helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top