简体   繁体   English

MySQL和web2py

[英]MySQL and web2py

I just started to develop with web2py and python stuff. 我刚刚开始使用web2py和python进行开发。 On web2py documentation i followed the instructions to connect to MySQL database. 在web2py文档中,我按照说明连接到MySQL数据库。

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: 例如:在MySQL上,我有一个名为“用户”的表。如果我定义表db.define_table('users'...),则会出现此错误:

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? 我的问题是:如何从远程数据库访问表和数据?

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). 如果是这种情况,那么您就会遇到这样一个事实,即默认情况下,web2py希望为您处理数据库(定义关系,属性等)。

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; 一种解决方法是,如果您知道数据库的布局,则首先要在web2py中定义模型以使其与表中的内容完全匹配。 run it once to generate the sql.log; 运行一次以生成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. 这意味着您失去了非常有用的自动迁移功能,并且必须自己执行ALTER语句。

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 无论您选择哪种方式,我都强烈建议您先快速阅读一下web2py手册第6章的“迁移”部分: http ://web2py.com/books/default/chapter/29/06/the-database -抽象层#迁移

I hope that helps 希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM