Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlQuery return "no such table" (but SQL code seems good)
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery return "no such table" (but SQL code seems good)

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 2.7k Views 1 Watching
  • 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.
  • H Offline
    H Offline
    Hoccau
    wrote on 1 Apr 2017, 17:18 last edited by
    #1

    Hello,

    I am using QSqlQuery with Sqlite3 db. This code works perfectly when I execute it in sqlitebrowser. :

    INSERT INTO repas(date, type_id, comment) VALUES('2017-04-01', 1, '26ieme');
    INSERT INTO outputs(quantity, repas_id, product_id) VALUES(2.0, 26, 8)
    

    But, with QT (python bindings), the following code fail :

    from PyQt5.QtSql import QSqlDatabase, QSqlQuery
    
    db = QSqlDatabase.addDatabase('QSQLITE')
    db.setDatabaseName('aa.db')
    opened = db.open()
    
    query = QSqlQuery()
    query.exec_("PRAGMA foreign_keys = ON")
    query.exec_(
            "INSERT INTO repas(date, type_id, comment) VALUES('2017-04-01', 1, '24ieme')")
    req = query.exec_("INSERT INTO outputs(quantity, repas_id, product_id) VALUES(2.0, 24, 8)")
    print(req, ':', query.lastQuery())
    print(query.lastError().text())
    

    it's return the following error :

    False : INSERT INTO outputs(quantity, repas_id, product_id) VALUES(2.0, 24, 8)
    no such table: main.product Unable to execute statement
    

    I don't understand, there is no table named 'main' in the database structure and it is not my query... What's wrong ?
    Thanks!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 1 Apr 2017, 20:16 last edited by
      #2

      Hi,

      Where is aa.db located exactly ?

      It's likely something along the lines of opening a database file which path is relative to the executable and the real file is somewhere else.

      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
      0
      • H Offline
        H Offline
        Hoccau
        wrote on 1 Apr 2017, 21:26 last edited by
        #3

        hi,
        aa.db is located in the same path as the script. I have tried with relative or absolute path but i still have the same error.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 1 Apr 2017, 21:31 last edited by
          #4

          Did you check that the opening succeeded ?

          Do you have a product table ?

          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
          0
          • H Offline
            H Offline
            Hoccau
            wrote on 1 Apr 2017, 21:39 last edited by Hoccau 4 Jan 2017, 21:56
            #5

            yes, db.open return True. I have no "product" table... The INSERT requests on other tables works. The problem occurs with this table only.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 1 Apr 2017, 22:02 last edited by
              #6

              Then were doesn't product_id come from ?

              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
              0
              • H Offline
                H Offline
                Hoccau
                wrote on 1 Apr 2017, 22:13 last edited by
                #7

                Ok, i have found the problem, it was a bad foreign key on an nonexistent table "product". Perhaps Sqlitebrowser doesn't have foreign key support activated by default, so it was working in it. Sorry, in fact it was a sql issue. Thank for your help!

                1 Reply Last reply
                0

                1/7

                1 Apr 2017, 17:18

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved