im using Postgres together with python(psycopg2). Im trying to insert data in a QLabel. It shows the data, but the data comes with clinges. How do I get rid of the clinges?
My code:
JavaScript
x
18
18
1
def getstunden():
2
conn = None
3
try:
4
conn = psycopg2.connect("dbname=test user=postgres password=test")
5
cur = conn.cursor()
6
cur.execute("SELECT stunden FROM ueberstunden WHERE name = 'test'")
7
row = cur.fetchone()
8
9
while row is not None:
10
self.s_test.setText(str(row))
11
row = cur.fetchone()
12
cur.close()
13
except (Exception, psycopg2.DatabaseError) as error:
14
print(error)
15
finally:
16
if conn is not None:
17
conn.close()
18
This is what I get out of it:
Advertisement
Answer
Per here Cursor:
Note
cursor objects are iterable, so, instead of calling explicitly fetchone() in a loop, the object itself can be used:
cur.execute(“SELECT * FROM test;”) for record in cur: print record
(1, 100, “abc’def”)
(2, None, ‘dada’)
(3, 42, ‘bar’)
So to simplify:
JavaScript
1
16
16
1
def getstunden():
2
conn = None
3
try:
4
conn = psycopg2.connect("dbname=test user=postgres password=test")
5
cur = conn.cursor()
6
cur.execute("SELECT stunden FROM ueberstunden WHERE name = 'test'")
7
for row in cur:
8
self.s_test.setText(str(row[0]))
9
cur.close()
10
except (Exception, psycopg2.DatabaseError) as error:
11
print(error)
12
finally:
13
if conn is not None:
14
conn.close()
15
16