0

Apologies if my title is unclear; I'm trying to retrieve some data from an SQLLite database (which is working) and show it to the user on a window in PyQt5. So far, I've managed to retrieve the result and print in Python, but when trying to add/view the results in the window Python simply crashes. I've inserted the code which isn't working below. (The indentation has gone slightly funny when copying it, but it is perfectly fine in my file).

class Profile(QWidget):
def __init__(self, Type):
    super().__init__()
    self.window = QWidget()
    self.window.setGeometry(100,100, 350, 400)
    self.window.setWindowTitle("Welcome")
    self.window.show()
    self.Choices()

def Choices(self):
    self.layout = QGridLayout()
    c.execute('''SELECT username, teamname FROM Users WHERE username = ?''',
    (user,))
    result = c.fetchone()
    print(result)
    print(user)

    self.TeamInfo = QLabel(result)
    self.layout.addWidget(self.TeamInfo)
    self.window.setLayout(self.layout)
    self.window.show()

user is a global variable in a previous window (the log in page) to avoid the user having to reenter their username. This section is not included, as that is not the problem. All the other buttons in the class are working - it is just this particular section. Any help as to how to solve this is greatly appreciated, I figure the problem is the line self.TeamInfo = QLabel(result) but I don't have the PyQT5 knowledge on how to solve this.

Edit: I have included a screenshot of the error message I'm getting.

Error

3
  • what is the output of print(result, type(result))? and show the complete error message. Commented Apr 25, 2019 at 18:52
  • @eyllanesc the output is: ('john1', 'johns team') <class 'tuple'>, and the error message is a different window saying Python has stopped working Commented Apr 26, 2019 at 12:29
  • 1
    use self.TeamInfo = QLabel(" ".join(results)) Commented Apr 26, 2019 at 12:34

2 Answers 2

1

If I understand right you are trying to add a text to QLabel, right? According to the documentation adding text to it is done by: QLabel().setText(result).

Edit: Could you please try this and tell me what the compiler is complaining about?

label = QLabel()
label.setText(result)
self.layout.addWidget(label)
Sign up to request clarification or add additional context in comments.

3 Comments

Could you please paste what is the compiler saying when running the code?
The compiler brings up a window complaining that Python has stopped working, tries to fix it then brings up the window that Python has stopped working entirely, it's printing user so it's able to get past that point.
I tried your code and it seems resolved by eyllanesc's reply. It was the problem that you were trying to put a tuple inside the Label, which seems like it only accepts string. I get this error in my IDE when I try to put tuple inside the label: QLabel(parent: QWidget = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags()): argument 1 has unexpected type 'tuple'
0

Try it:

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

import sqlite3

class Profile(QWidget):
    def __init__(self, Type=None):
        super().__init__()
        self.window = QWidget()
        self.window.setGeometry(100,100, 350, 400)
        self.window.setWindowTitle("Welcome")
        self.Choices()

    def Choices(self):
        self.layout = QGridLayout()

#        c.execute('''SELECT username, teamname FROM Users WHERE username = ?''',
#                  (user,))
#        result = c.fetchone()
#

        user = "Nick" 
        try:
            self.conn = sqlite3.connect("database.db")
            self.c = self.conn.cursor()
#            result = self.c.execute("SELECT * from students WHERE name='{}'".format(user) )   
            result = self.c.execute("SELECT * from students WHERE name=?", (user, ))

            row = result.fetchone()                                               # <---
            print("\nrow->", type(row), row)
            serachresult = "Rollno : "+str(row[0])+'\n'+"Name : "+str(row[1])+'\n'+"Branch : "+str(row[2])+'\n'+"Sem : "+str(row[3])+'\n'+"Address : "+str(row[4])
            QMessageBox.information(QMessageBox(), 'Successful', serachresult)
            self.conn.commit()
            self.c.close()
            self.conn.close()
        except Exception:
            QMessageBox.warning(QMessageBox(), 'Error', 'Could not Find student from the database.')

        print(result)
        print(user)

        self.TeamInfo = QLabel(", ".join([ str(i) for i in row ]))                # <---
        self.layout.addWidget(self.TeamInfo)
        self.window.setLayout(self.layout)
        self.window.show()

if __name__ == '__main__':
     app = QApplication(sys.argv)
     main = Profile()
     main.show()
     sys.exit(app.exec_()) 

enter image description here

3 Comments

self.c.execute("SELECT * from students WHERE name='{}'".format(user)) == SQL Injection :-( .... read docs.python.org/3/library/sqlite3.html: # Never do this -- insecure! symbol = 'RHAT' c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol). Better to use a placeholder: self.c.execute("SELECT * from students WHERE name=?", (user, ))
@eyllanesc Thanks
Do not provide code just to respond to the OP, here the most important thing is that the question is that it is of quality so that it serves the whole community, and always use: Try it does not help, why do not you explain the code failure of the OP and your solution?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.