im使用Postgres和python(psycopg2)。我正试图在QLabel中插入数据。它显示了数据,但数据附带了粘人。我该如何摆脱粘人?
我的代码:
def getstunden():
conn = None
try:
conn = psycopg2.connect("dbname=test user=postgres password=test")
cur = conn.cursor()
cur.execute("SELECT stunden FROM ueberstunden WHERE name = 'test'")
row = cur.fetchone()
while row is not None:
self.s_test.setText(str(row))
row = cur.fetchone()
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
这就是我从中得到的:
我希望它只显示12
根据此处光标:
注意
游标对象是可迭代的,因此,可以使用对象本身,而不是在循环中显式调用fetchone():
当前执行("SELECT*FROM test")以当前货币记录:打印记录
(1100,"abc'def")
(2,无,'dada')
(3,42,'bar')
所以要简化:
def getstunden():
conn = None
try:
conn = psycopg2.connect("dbname=test user=postgres password=test")
cur = conn.cursor()
cur.execute("SELECT stunden FROM ueberstunden WHERE name = 'test'")
for row in cur:
self.s_test.setText(str(row[0]))
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
它就像这个
print(row[0])
在您的代码中
def getstunden():
conn = None
try:
conn = psycopg2.connect("dbname=test user=postgres password=test")
cur = conn.cursor()
cur.execute("SELECT stunden FROM ueberstunden WHERE name = 'test'")
row = cur.fetchone()
while row is not None:
#row without the brackets
self.s_test.setText(str(row[0]))
row = cur.fetchone()
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()