2017-10-21 47 views
1

如何使用python中的數據庫Sqlite中的行的值來創建數組?Python中的數據庫數據中的數組

def dropname(): 
    db = get_db() 
    dropname = db.execute("SELECT name FROM names WHERE ID_dol = 1") 
    dropname = cur.fetchall() 
    return render_template('form_names.html', dropname=dropname) 

回答

-1

的問題是,你不取的變量 「dropname」 查詢

就應該更換

dropname = cur.fetchall() 
return render_template('form_names.html', dropname=dropname) 

通過

data = dropname.fetchall() 
return render_template('form_names.html', dropname=data) 

然後在你的HTML叫它,你做

{{dropname [n]的}}

其中n是列的索引

0

fetchall()返回的行的列表,其中每行是列的值的列表。

你要提取每一行的第一列的值:

dropname = [row[0] for row in dropname] 
相關問題