从Pyqt textbrowser中的sqlite行设置文本

时间:2018-08-04 12:57:02

标签: python-3.x sqlite pyqt5

我需要一种将sqlite.db中的某些行设置为pyqt textbrowser的方法,但是问题是它只放在最后一行。

cur = conn.cursor()
conn.text_factory = str
cur.execute(" SELECT text FROM Translation WHERE priority = ?", (m,))
for row in cur:
    print('{0}'.format(row[0]))
    self.SearchResults.setPlainText('{0}'.format(m))

我用过熊猫,但是

query = "SELECT text FROM Translation WHERE priority=2;"

df = pd.read_sql_query(query,conn)
 self.SearchResults.setPlainText('{0}'.format(df['text'])) 

这不是我想要的。而且:

cur = conn.cursor()
conn.text_factory = str
cur.execute(" SELECT text FROM Translation WHERE priority = ?", (m,))
    all_rows=cur.fetchall()

    self.SearchResults.setPlainText('{0}'.format(all_rows))

1 个答案:

答案 0 :(得分:1)

在第一个代码中,您每次迭代都将替换文本,解决方案是使用append()

cur = conn.cursor()
conn.text_factory = str
cur.execute(" SELECT text FROM Translation WHERE priority = ?", (m,))

self.SearchResults.clear() # clear previous text

for row in cur:
    self.SearchResults.append('{0}'.format(str(row[0])))