0

我创建了一个小型数据库,我想从中提取每个字段中的每个项目!简而言之,我希望能够在 UI (Ot.Gui) 中显示项目,我能想到的唯一方法是将项目按 1 分配给变量 1。第一行,第一个值到最后一行最后一个值.

目前,我正在使用

import sqlite3 as lite
con = lite.connect('Bestellung.db')    
with con:
    con.row_factory = lite.Row
    cur = con.cursor() 
    cur.execute("SELECT * FROM Bestellung")
    rows = cur.fetchall()

    for row in rows:
        print "%s %s %s" % (row["Id"], row["ArtikelNr"], row["Beschreibung"])

它从数据库中提取并打印所有信息:(只有 6 个值 atm)

1 a1 b1
2 a2 b2

但现在我想创建一个函数或模块,它会自动为每个项目创建一个变量。这样:

Row 1, Value 1 = Row1Val1
Row 1, Value 2 - Row1Val1
...

然后我可以使用它来设置 TextLabels。

除非你有更简单的方法来解决我的问题。

预先感谢

4

2 回答 2

0

看看namedtuple,它们非常适合这种任务。

于 2013-07-05T20:46:34.097 回答
0

我不确定我是否很好地理解了你的问题。您是否要在不明确指定列名的情况下动态迭代列(这就是我所理解的)。如果是,那么这应该可以帮助您:

with lite.connect('bestellung.db') as con:
    con.row_factory = lite.Row
    cur = con.cursor() 
    cur.execute("SELECT * FROM Bestellung")
    rows = cur.fetchall()

    rowcount = 1
    for row in rows:
        colcount = 1
        for key in row.keys():
            value = row[key]
            print "Row %d, Value %d = %s" % (rowcount, colcount, value)
            colcount += 1
        rowcount += 1

如果没有,那没关系,我会删除这个答案。

于 2013-07-05T21:31:16.773 回答