Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Replace the table name in query with variabale value
Forum Updated to NodeBB v4.3 + New Features

Replace the table name in query with variabale value

Scheduled Pinned Locked Moved Solved Qt for Python
21 Posts 5 Posters 5.4k Views 2 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.
  • D Offline
    D Offline
    dvlpr.bernard
    wrote on last edited by
    #1

    Is it possible to replace the table name in query with variabale value?
    cb16c52d-5fe9-4e1a-9a4e-c680980a3161-image.png

    jsulmJ JonBJ 2 Replies Last reply
    0
    • D dvlpr.bernard

      Is it possible to replace the table name in query with variabale value?
      cb16c52d-5fe9-4e1a-9a4e-c680980a3161-image.png

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @dvlpr-bernard Sure:

      QString queryStrTemplate = "SELECT * from %1";
      c.execute(queryStrTemplate.arg("TABLE_NAME"));
      

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

      D 1 Reply Last reply
      5
      • D dvlpr.bernard

        Is it possible to replace the table name in query with variabale value?
        cb16c52d-5fe9-4e1a-9a4e-c680980a3161-image.png

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @dvlpr-bernard
        Note that as @jsulm has written, for a table name (which is not shown as a variable in your code) you cannot do it via the SQL ?-binding mechanism, you must do it as he has shown by literal in-line substitution as you build the statement.

        1 Reply Last reply
        3
        • jsulmJ jsulm

          @dvlpr-bernard Sure:

          QString queryStrTemplate = "SELECT * from %1";
          c.execute(queryStrTemplate.arg("TABLE_NAME"));
          
          D Offline
          D Offline
          dvlpr.bernard
          wrote on last edited by
          #4

          @jsulm
          Sir I was using PYQT and it displays an error.

          c4a2f88e-dc81-4f7a-b94a-32cebce86aca-image.png
          94e06239-d5bc-435a-83e3-b0a741e0c9f1-image.png

          jsulmJ JonBJ 2 Replies Last reply
          0
          • D dvlpr.bernard

            @jsulm
            Sir I was using PYQT and it displays an error.

            c4a2f88e-dc81-4f7a-b94a-32cebce86aca-image.png
            94e06239-d5bc-435a-83e3-b0a741e0c9f1-image.png

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @dvlpr-bernard You're using a Python string. So, simply use what Python provides you:

            queryStrTemplate = 'SELECT * from {}'
            c.execute(queryStrTemplate.format(table_name))
            

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

            1 Reply Last reply
            3
            • D dvlpr.bernard

              @jsulm
              Sir I was using PYQT and it displays an error.

              c4a2f88e-dc81-4f7a-b94a-32cebce86aca-image.png
              94e06239-d5bc-435a-83e3-b0a741e0c9f1-image.png

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by JonB
              #6

              @dvlpr-bernard
              In addition to what @jsulm has corrected, once you get past that line you will need to correct your Python code when it gets to your subsequent line (oh, please try to paste code not screenshots, it makes it impossible for respondents to copy/paste!)

              c.execute('... {} \'.format(...) where SCHED_ID = ?', (1,)))
              

              It might help if you read up on Python strings.

              I also observe that since you are choosing to do all your database access through Python library calls, there is not a single bit of Qt or PyQt in the code you have now pasted. That's fine, but then for generic Python questions you might find a Python forum more suitable than a Qt one.

              1 Reply Last reply
              2
              • D Offline
                D Offline
                dvlpr.bernard
                wrote on last edited by
                #7

                Thank you for your response :)
                But, I'm still getting an error. I apologize, I'm not familiar with the syntax of python.

                table_name = '1.0'
                queryStrTemplate = 'SELECT NO_EXERCISE from {}'
                c.execute(queryStrTemplate.format(table_name))
                
                c.execute('select NO_EXERCISE from {} \'.format(table_name) where SCHED_ID = ?', (1,))
                row = c.fetchone()
                print(row[0])
                

                36612b8b-8c2d-4a1e-bc69-fbafc0f11068-image.png

                jsulmJ JonBJ D 3 Replies Last reply
                0
                • D dvlpr.bernard

                  Thank you for your response :)
                  But, I'm still getting an error. I apologize, I'm not familiar with the syntax of python.

                  table_name = '1.0'
                  queryStrTemplate = 'SELECT NO_EXERCISE from {}'
                  c.execute(queryStrTemplate.format(table_name))
                  
                  c.execute('select NO_EXERCISE from {} \'.format(table_name) where SCHED_ID = ?', (1,))
                  row = c.fetchone()
                  print(row[0])
                  

                  36612b8b-8c2d-4a1e-bc69-fbafc0f11068-image.png

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by jsulm
                  #8

                  @dvlpr-bernard said in Replace the table name in query with variabale value:

                  I'm not familiar with the syntax of python

                  Then you should learn it.
                  And please take a closer look at your execute() line it is really broken...

                  c.execute('select NO_EXERCISE from {} where SCHED_ID = {}'.format(table_name, 1))
                  

                  See https://www.geeksforgeeks.org/python-format-function/

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

                  1 Reply Last reply
                  2
                  • D dvlpr.bernard

                    Thank you for your response :)
                    But, I'm still getting an error. I apologize, I'm not familiar with the syntax of python.

                    table_name = '1.0'
                    queryStrTemplate = 'SELECT NO_EXERCISE from {}'
                    c.execute(queryStrTemplate.format(table_name))
                    
                    c.execute('select NO_EXERCISE from {} \'.format(table_name) where SCHED_ID = ?', (1,))
                    row = c.fetchone()
                    print(row[0])
                    

                    36612b8b-8c2d-4a1e-bc69-fbafc0f11068-image.png

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by JonB
                    #9

                    @dvlpr-bernard said in Replace the table name in query with variabale value:

                    But, I'm still getting an error. I apologize, I'm not familiar with the syntax of python.

                    We (I) do not want to be rude or mean to you. But if you are not familiar with Python or its syntax, why are you programming in it, how do you expect to get anywhere? You are going to get this kind of issue on every line you try to write. Which is also why I said you might want to use a general Python programming forum, since this sort of thing simply has nothing to do with Qt, but as you please.

                    1 Reply Last reply
                    1
                    • D Offline
                      D Offline
                      dvlpr.bernard
                      wrote on last edited by
                      #10
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        dvlpr.bernard
                        wrote on last edited by dvlpr.bernard
                        #11

                        I didn't meant to offend you guys. I was working on a PyQt app it is just a small school project. Python is the required language, what do I mean is I don't have time to dig deeper on learning other python stuff. I learned the fundamentals and I also interested on the things that I needed. But in this case, I was hyped because I was about to finish my project. The only thing left is replacing the table name in query with variable value and then my project will get finished. That's maybe the reason why I was hyped to get the correct syntax for that.

                        I apologize if I said something mean, English is not my first language. I hope you understand. Thank you for both of you :)

                        1 Reply Last reply
                        0
                        • D dvlpr.bernard

                          Thank you for your response :)
                          But, I'm still getting an error. I apologize, I'm not familiar with the syntax of python.

                          table_name = '1.0'
                          queryStrTemplate = 'SELECT NO_EXERCISE from {}'
                          c.execute(queryStrTemplate.format(table_name))
                          
                          c.execute('select NO_EXERCISE from {} \'.format(table_name) where SCHED_ID = ?', (1,))
                          row = c.fetchone()
                          print(row[0])
                          

                          36612b8b-8c2d-4a1e-bc69-fbafc0f11068-image.png

                          D Offline
                          D Offline
                          dvlpr.bernard
                          wrote on last edited by
                          #12

                          @dvlpr-bernard
                          I finally noticed the problem. It must be

                          table_name = "\'1.0\'"
                          

                          not

                          table_name = '1.0'
                          

                          Thank you guys for your help :)

                          JonBJ 1 Reply Last reply
                          1
                          • D dvlpr.bernard

                            @dvlpr-bernard
                            I finally noticed the problem. It must be

                            table_name = "\'1.0\'"
                            

                            not

                            table_name = '1.0'
                            

                            Thank you guys for your help :)

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by
                            #13

                            @dvlpr-bernard
                            One thing I meant to ask: what editor do you use when writing your Python code? If it's an IDE like PyCharm or VSCode it will helpfully show you when code is syntactically wrong, offer you methods/parameters to choose from etc.; if you are not using an editor with Python syntax you are missing out.

                            D 1 Reply Last reply
                            1
                            • D Offline
                              D Offline
                              dvlpr.bernard
                              wrote on last edited by
                              #14

                              @Denni-0 Thank you I will take note of that

                              1 Reply Last reply
                              0
                              • JonBJ JonB

                                @dvlpr-bernard
                                One thing I meant to ask: what editor do you use when writing your Python code? If it's an IDE like PyCharm or VSCode it will helpfully show you when code is syntactically wrong, offer you methods/parameters to choose from etc.; if you are not using an editor with Python syntax you are missing out.

                                D Offline
                                D Offline
                                dvlpr.bernard
                                wrote on last edited by
                                #15

                                @JonB I actually using IDLE hahaha. I do have PyCharm but I got used to the IDLE environment. Prob would gonna try them.

                                JonBJ 1 Reply Last reply
                                0
                                • D dvlpr.bernard

                                  @JonB I actually using IDLE hahaha. I do have PyCharm but I got used to the IDLE environment. Prob would gonna try them.

                                  JonBJ Online
                                  JonBJ Online
                                  JonB
                                  wrote on last edited by
                                  #16

                                  @dvlpr-bernard
                                  I imagine IDLE, being a Python editor, shows you errors, no? The point was, in your earlier erroneous code, I would have expected your editor to be showing you those lines were incorrect.

                                  D 1 Reply Last reply
                                  1
                                  • JonBJ Online
                                    JonBJ Online
                                    JonB
                                    wrote on last edited by JonB
                                    #17

                                    @Denni-0
                                    You would actively choose a generic editor without Python syntax/library knowledge over one which corrects, completes and so on? And you would recommend that to a self-proclaimed beginner?

                                    1 Reply Last reply
                                    0
                                    • JonBJ JonB

                                      @dvlpr-bernard
                                      I imagine IDLE, being a Python editor, shows you errors, no? The point was, in your earlier erroneous code, I would have expected your editor to be showing you those lines were incorrect.

                                      D Offline
                                      D Offline
                                      dvlpr.bernard
                                      wrote on last edited by
                                      #18

                                      @JonB HAHAHAHAHAH it works fine to me, IDLE also shows where the error is, but not as powerful as other IDE's can do. It would have been a little bit easier if I use other IDE's like PyCharm. But just like I what I have said I had so much fun and excitement using IDLE. Next time I would prob use PyCharm to put myself at ease.

                                      1 Reply Last reply
                                      0
                                      • JonBJ Online
                                        JonBJ Online
                                        JonB
                                        wrote on last edited by
                                        #19

                                        @Denni-0
                                        No problem. I am surprised you choose a non-Python-environment editor, but that is your choice. :) I am also glad you are happy for the OP or a beginner to use the help he can get from one.

                                        1 Reply Last reply
                                        0
                                        • D Offline
                                          D Offline
                                          dvlpr.bernard
                                          wrote on last edited by
                                          #20

                                          @Denni-0 I'm sorry sir. I felt bad about the replies(they were indirectly telling me that I don't belong or I shouldn't be here), so I decided to delete it. Forget about it, I appreciate your help sir :).

                                          JKSHJ 1 Reply Last reply
                                          0

                                          • Login

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