【问题标题】:Python with SQLite. SQLite data into variablePython 与 SQLite。 SQLite 数据转换成变量
【发布时间】:2013-06-05 23:55:51
【问题描述】:

python 中的 SQLite,在屏幕上打印,python 3.2.4

我有这个代码

cur.execute("SELECT * FROM Player")
while True:

    row = cur.fetchone()
    print(row)
    print("why  {0}".format(cur.fetchone()))

    if row == None:
        break

我收到这个输出: 从数据库加载

    (1, 'Damian', 1, 15, 18, 100, 100)
    why  None
    None
    why  None

(((((1, 'Damian', 1, 15, 18, 100, 100)))

【问题讨论】:

  • row 在第二个循环中是 None,因此它没有第一个元素。

标签: python sqlite printing screen output


【解决方案1】:

您的问题是您在循环内调用了两次fetchone()。 对应于行的while 循环并没有什么神奇之处;相反,它是调用fetchone() 的副作用,会前进到下一个结果行。

第一个print输出第一行;第二个(带有"why")将打印第二行(如果存在)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多