2013-10-18 71 views
0

這裏是一個web2py和python新手。Web2Py MySQL數據檢索

我試圖做一個示例Web應用程序與MySQL DB作爲後端數據庫。我已經分別在MySQL中創建了一個表,並用值填充,我想在UI中顯示錶值。我有一個錯誤 -

「類 '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。謝謝。

回答

1

您有migrate=True,並且web2py沒有任何記錄表示它已經創建了表,因此它正在嘗試再次創建它。您可以通過臨時設置fake_migrate=True(或者設置migrate=False並且不要讓web2py處理遷移)來獲取web2py更新其關於表格的記錄。

另外,請注意,默認情況下,web2py希望每個表都包含一個名爲「id」的自動增量整數字段,因此您應該確保您的表包含這樣的字段。更好的是,如果表是新的,不要在MySQL中手動創建它。相反,讓web2py創建它(它將在第一次運行表定義時這樣做)。一旦它由web2py創建,你就可以添加你喜歡的任何記錄。