【发布时间】:2015-03-02 11:18:46
【问题描述】:
我在 views.py 中使用它来获取应用程序
from django.db import connection
def test(request):
cursor = connection.cursor()
sql = """
SELECT x , n
from table1 limit 10
"""
cursor.execute(sql)
rows = cursor.fetchall()
# df1 = pd.read_sql_query(sql,cursor) <==> not working )
# df1.columns = cursor.keys() <==> not working )
return render(request, 'app/test.html',{ "row" : rows,})
我能够在 test.html
中打印行并在下面得到类似这样的列表row((x1,yvalue1),(x2,yvalue2) , .... ))
但我想要做的是获取所有数据及其列名,我提取并放入数据框,希望使用下面这样的东西:
【问题讨论】:
-
我对 django 一无所知,但我相信您可以与 read_sql_query 一起使用的唯一“本机”连接是用于 sql lite。您需要对所有其他人使用 sql alchemy。