【问题标题】:Web2Py MySQL data retrievalWeb2Py MySQL 数据检索
【发布时间】:2023-03-21 18:35:02
【问题描述】:

这里是 web2py 和 python 新手。

我尝试使用 MySQL DB 作为后端 DB 来制作示例 Web 应用程序。我在 MySQL 中分别创建了一个表并填充了值,我想在 UI 中显示表值。我有一个错误 -

"class 'gluon.contrib.pymysql.err.InternalError'> (1050, u"表''已经存在")"

我的配置文件如下:

db.py

if not request.env.web2py_runtime_gae:
    db = DAL('mysql://xxxxx',pool_size=1,check_reserved=['all'])
else:

    session.connect(request, response, db=db)

response.generic_patterns = ['*'] if request.is_local else []


from gluon.tools import Auth, Crud, Service, PluginManager, prettydate
auth = Auth(db)
crud, service, plugins = Crud(db), Service(), PluginManager()

## create all tables needed by auth if not custom tables
auth.define_tables(username=False, signature=False)


## after defining tables, uncomment below to enable auditing
# auth.enable_record_versioning(db)
db.define_table('user_details',
   Field('user_id', 'text'),
    Field('first_name', 'text'),
    Field('last_name', 'text'),
    Field('city', 'text'),
    Field('user_st', 'text'),migrate=True)

我的主页是这样的

{{ rows = db(db.user_details).select() }}
{{if len(rows):}}
<ul>
{{ for r in rows: }}
  <li>

        {{=r.name}}

  </li>
{{pass}}
</ul>
{{pass}}

我不确定我错过了什么。任何帮助表示赞赏,谢谢。


我现在明白了。只需要更改 migrate=False。谢谢。

【问题讨论】:

    标签: mysql web2py


    【解决方案1】:

    你有migrate=True,而web2py没有任何记录表明它已经创建了表,所以它正在尝试再次创建它。你可以通过临时设置fake_migrate=True(或者设置migrate=False并且不要让web2py处理迁移)来让web2py更新它关于表的记录。

    另外,请注意,默认情况下,web2py 期望每个表都包含一个名为“id”的自增整数字段,因此您应该确保您的表包含这样一个字段。更好的是,如果表是新的,不要在 MySQL 中手动创建它。相反,只需让 web2py 创建它(它会在第一次运行表定义时创建它)。一旦它由 web2py 创建,您就可以添加任何您喜欢的记录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-08
      • 2012-07-27
      • 2012-04-07
      相关资源
      最近更新 更多