Results 1 to 3 of 3

Thread: Issue with query.exec_ in PyQt5

  1. #1
    Join Date
    Jan 2018

    Default Issue with query.exec_ in PyQt5

    I folks!
    I'm new in the Qt and PyQt world, more precisely in the latter! At the moment I'm using PyQt 5.6 with Python 3.5 and i'mtrying to connect my simple app to a local MySQL server. I managed to open the server, but unfortunately the program crash whenever I commit a query (Which i'm sure it's correct since it works fine on SQLyog).
    My code is the following:

    from PyQt5 import QtWidgets, QtSql
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import QApplication

    import sys

    app = QApplication(sys.argv)
    db = QtSql.QSqlDatabase.addDatabase("QMYSQL")
    print("Db open!")
    query = QtSql.QSqlQuery(db)

    query.exec_("SELECT * FROM userscredentials")


    print("Db not open")



    and i got the following output

    C:\Python35\python.exe C:/Users/Egon/PycharmProjects/PyQt/DemoSQL/
    Db open!

    Process finished with exit code -1073741819 (0xC0000005)

    Can you please help me to understand the problem ?

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 864 Times in 851 Posts
    Qt products

    Default Re: Issue with query.exec_ in PyQt5

    In the Microsoft world, this error code means the program crashed with an access violation. That means something in the program tried to use an invalid (probably NULL) pointer to read or write something.

    The QSqlQuery::exec() method returns a bool code that tells you whether the query succeeded or not. You don't bother to check so you don't actually know if the query succeeded.. But even if it did succeed, the record cursor isn't pointing to a valid record until you call one of the record navigation functions (QSqlQuery::first(), QSqlQuery::next(), etc.). You haven't done that either, meaning query.value(0) isn't accessing a valid record and is probably returning a NULL pointer. When you send that NULL pointer to the print method, boom!

    Note that the record navigation functions also return a bool indicating whether or not they succeeded, and you will be wise to check that value before you try to access the record in any way.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Apr 2008
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Unix/X11 Windows

    Default Re: Issue with query.exec_ in PyQt5

    Quote Originally Posted by Evethir View Post
    query = QtSql.QSqlQuery(db)

    query.exec_("SELECT * FROM userscredentials")

    As above, your query is not pointing at a valid record. You don't call (or first as d_stranz said). If your expecting a single record you can do;

    Qt Code:
    1. if
    2. print(query.value(0))
    To copy to clipboard, switch view to plain text mode 

    or for more results use a while loop:

    Qt Code:
    1. while
    2. print(query.value(0))
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. issue resizing QScrollArea pyqt5
    By Bybuu in forum Qt Programming
    Replies: 0
    Last Post: 13th October 2017, 10:22
  2. PyQt5 Questions
    By Miton in forum Newbie
    Replies: 6
    Last Post: 30th January 2017, 13:10
  3. pyqt New Dialog No Attribute exec_
    By supertom44 in forum Newbie
    Replies: 2
    Last Post: 10th November 2016, 19:58
  4. Replies: 7
    Last Post: 16th April 2015, 18:11
  5. PyQt5 QPixmap
    By ChrisOfBristol in forum Newbie
    Replies: 4
    Last Post: 4th April 2015, 22:48

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.