【问题标题】:Python [ mod_wsgi ] how to gather up all of the mysql results into an array?Python [ mod_wsgi ] 如何将所有 mysql 结果收集到一个数组中?
【发布时间】:2013-05-10 23:34:54
【问题描述】:

通过 cli:

[root@localhost 0]# python test13.wsgi
(1, 'aaaaaa')
(2, 'sdsdfsdfsd')
(3, 'dsfsdfasdfsdf')
(4, 'sdgsdgsdfsa')
[root@localhost 0]# 

通过阿帕奇:

(4, 'sdgsdgsdfsa')

脚本代码:

import MySQLdb
conn = MySQLdb.connect (host = "localhost",
                        user = "root",
                        passwd = "",
                        db = "aaa")
cursor = conn.cursor ()
cursor.execute ("select * from bbb limit 10")
numrows = int(cursor.rowcount)
for i in range(numrows):
    row =  cursor.fetchone()
    print row
cursor.close ()
conn.close ()

def application(environ, start_response):
    start_response('200 OK', [('content-type', 'text/html')])
    return [repr(row)]

我想简单地将所有这些行放入一个数组中,如 php 然后与 php 相比,在 python 中做一个 print_r() 等价。

所以在 apache 中打印的是一切,而不仅仅是 最后一个。

【问题讨论】:

    标签: python python-2.7 mod-wsgi wsgi mysql-python


    【解决方案1】:

    这段代码:

    for i in range(numrows):
        row =  cursor.fetchone()
    

    row 设置为cursor.fetchone() numrows 次的结果。它没有列出清单。

    你可能只想写rows = cursor.fetchall()

    另外,如果您尝试使用 Python 编写一个简单(或复杂)的 web 应用程序,我会考虑查看 Flask

    【讨论】:

    • 这行得通,但我不需要它在一个数组中,以便我可以选择要打印的数组吗?这就是我得到的.. ((1, 'aaaaaa'), (2, 'sdsdfsdfsd'), (3, 'dsfsdfasdfsdf'), (4, 'sdgsdgsdfsa')) 我如何选择性地打印其中一个?
    • @PythonNewbie:在进入 WSGI 之前阅读 Python 教程。
    • 我会选择远离烧瓶、django 等,因为我想一旦我想办法从 mysql 获取一些东西然后做一个 php-apc 等效内存缓存,我不会不再需要设计一个网页。
    • 听起来不错,我想下一个问题将是如何打印特定的数组。
    猜你喜欢
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    • 2023-01-31
    • 2022-12-15
    • 2012-12-13
    • 2012-04-07
    • 1970-01-01
    • 2013-01-23
    相关资源
    最近更新 更多