Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QSqlQuery Access violation by out of scope

    General and Desktop
    qt 5.7 sql mysql qsqlquery
    6
    17
    5611
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      masterofeye last edited by masterofeye

      Hello all,

      first my system:
      Win 7 x64
      QT 5.7 64bit (installed from installer)

      Problem:
      I want to create a table in my MySQL DB. So I "login" in my DB and want to execute a statement.

      	QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
      	db.setHostName("bla");
      	db.setPort(bla);
      	db.setDatabaseName("bla");
      	db.setUserName("bla");
      	db.setPassword("bla");
      
      	if (!db.open())
      		return false;
      
      	QSqlQuery query(db);
      	const QString cmd = "CREATE TABLE IF NOT EXISTS bla (idbla INTEGER UNIQUE PRIMARY KEY, bla VARCHAR(255))";
      	bool res = query.prepare(cmd);
      	if (res)
      	{
      		if (query.exec())
      		{
      			qDebug() << "bla created succesfully";
      		}
      		else
      		{
      			qDebug() << "Error " << "MySQl Error " << query.lastError().text();
      		}
      	}
      	query.finish();
      	} <- Access Violation (ntdll.dll), because of QSqlQuery
      

      The Database connection ist still alive. So why I get this error? Maybe I used the wrong libmysql.dll?
      I copied libmysql.dll(mysql-connector-c-6.1.6-winx64) and qsqlmysqld.dll to my application directory.

      Best regards
      MOE

      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @masterofeye last edited by

        @masterofeye Can you post the exact error message?
        Also there is no need to call finish()

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 1
        • M
          masterofeye last edited by

          Hi,
          there error message say not more then fatal error at 0x000... (ntdll.dll) in UnitTest.exe 0x000... Access Violation at read from pos 0x000....

          here the orignal text in german.
          Ausnahmefehler bei 0x000000007739F23C (ntdll.dll) in UnitTest.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0x000000202B2B0F98

          The finish call was just a try ;)

          jsulm 1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @masterofeye last edited by

            @masterofeye Run through debugger and see what happens on the call stack

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • M
              masterofeye last edited by

              Nothing what i can interpret as a failure:
              0_1481529136057_upload-50113657-6185-4eaa-9911-dcb20aa55a1f

              jsulm 1 Reply Last reply Reply Quote 0
              • jsulm
                jsulm Lifetime Qt Champion @masterofeye last edited by

                @masterofeye Images do not work in the forum. Please upload it to somewhere and post a link. Or, even better, just copy paste here as text.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply Reply Quote 0
                • M
                  masterofeye last edited by

                  Really? Because I see the Image ... fantastic ..

                  ntdll.dll!RtlFreeHeap()	Unknow
                  kernel32.dll!HeapFree()	Unknow
                  msvcr120.dll!000007feee2669d8()	Unknow
                  Qt5Sql.dll!00000000612dae72()	Unknow
                  qsqlmysql.dll!000007fefa8f1e05()	Unknow
                  qsqlmysql.dll!000007fefa8f1d97()	Unknow
                  Qt5Sql.dll!00000000612d15e4()	Unknow
                  UnitTest.exe!SQLConstructor::MySQLInitialization() Zeile 35	C++
                  UnitTest.exe!main(int argc, char * * argv) Zeile 23	C++
                  
                  kshegunov 1 Reply Last reply Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    Hi,

                    Did you check that your MySQL dlls are compiled with the same compiler as Qt ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply Reply Quote 1
                    • M
                      masterofeye last edited by

                      As mentioned on the top, i took the libmysql.dll from the installation here mysql-connector-c-6.1.6-winx64 and the qsqlmysqld.dll from the msvc2013_64 installation. They should be compatible or ?

                      jsulm 1 Reply Last reply Reply Quote 0
                      • jsulm
                        jsulm Lifetime Qt Champion @masterofeye last edited by

                        @masterofeye said in QSqlQuery Access violation by out of scope:

                        They should be compatible or ?

                        Only if libmysql.dll was built with msvc2013_64.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply Reply Quote 2
                        • kshegunov
                          kshegunov Moderators @masterofeye last edited by

                          @masterofeye said in QSqlQuery Access violation by out of scope:

                          UnitTest.exe!SQLConstructor::MySQLInitialization() Zeile 35	C++
                          UnitTest.exe!main(int argc, char * * argv) Zeile 23	C++
                          

                          What do you have around line 23 in your main() and what do you have around line 35 in SQLConstructor::MySQLInitialization()? It's not clear from the code snippet how the code you posted relates to these two locations.

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply Reply Quote 2
                          • M
                            masterofeye last edited by masterofeye

                            So tested some more and put my code in the main function between of the QCoreApplication creation and the exec() function. And it works! I changed the QSqlQuery variable to a pointer and dellete it before the call of the exec().

                            When i put the this code now back in the class and execute the function, I get the error again.
                            The class inherits form QObject and uses the Q_OBJECT macro. Do I need anything else ?

                            sorry here the answer from @kshegunov question
                            main:

                            int main(int argc, char *argv[])
                            {
                            	QCoreApplication a(argc, argv);
                            
                            	SQLConstructor sql();
                            	sql.MySQLInitialization();   //line 13
                            
                            	QLocalSocket socket;
                            	socket.connectToServer("server");
                            	while (!socket.isOpen());
                            
                                   RW::CORE::Unit u(&socket);
                                   u.StartTest(RW::CORE::Util::Functions::PortalInfoShowDialog);
                                   return a.exec();
                            }
                            ```in MySQLInitialization happens my code posted in the first post.
                            1 Reply Last reply Reply Quote 0
                            • M
                              masterofeye last edited by masterofeye

                              I found it !!! .... i linked against the Qt5Sql.lib and not against the Qt5Sqld.lib ..... GRRR
                              Thx for your help !!

                              1 Reply Last reply Reply Quote 0
                              • SGaist
                                SGaist Lifetime Qt Champion last edited by

                                Out of curiosity, how did that happen ?

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                V 1 Reply Last reply Reply Quote 0
                                • V
                                  VRonin @SGaist last edited by

                                  @SGaist Happened to me too when I was using VS Express (which does not support the addins), you have to do the linking manually (and invoke moc and stuff in prebuild) and it's easy to get it wrong

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  1 Reply Last reply Reply Quote 1
                                  • SGaist
                                    SGaist Lifetime Qt Champion last edited by

                                    @VRonin I was thinking about something along this line. Just wanted to check whether it was that or a setup issue, an IDE quirk or something more exotic :)

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    mrjj 1 Reply Last reply Reply Quote 1
                                    • mrjj
                                      mrjj Lifetime Qt Champion @SGaist last edited by

                                      @SGaist
                                      is VS express not excotic enough ? ;)

                                      1 Reply Last reply Reply Quote 1
                                      • First post
                                        Last post