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. Why name='%s' is not deleting my query but if i set to name='something' it deletes.

Why name='%s' is not deleting my query but if i set to name='something' it deletes.

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 5 Posters 8.0k Views 3 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.
  • B Bui Trung

    Does this function work in your case ?
    http://doc.qt.io/qt-5/qstring.html#arg
    Then you can convert from QString to c++ str

    ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #9

    @Bui-Trung i am using python

    mrjjM 1 Reply Last reply
    0
    • ? A Former User

      @Bui-Trung i am using python

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #10

      @Gelo
      Hi
      Fast google tells me

      query_str = 'SELECT %s FROM track %s'%(select_string, where_string)
      self.query.prepare(query_str)

      That you might need to call prepare.
      Do you ?

      Also this looks like fine tut
      http://www.pythonstudio.us/pyqt-programming/executing-sql-queries.html

      1 Reply Last reply
      1
      • B Offline
        B Offline
        Bui Trung
        wrote on last edited by
        #11

        Sorry about it, i have not been ever using pyQt before, so can you find the same class of QString like the one i linked to?

        1 Reply Last reply
        1
        • VRoninV VRonin

          Not an expert here since it's not C++ but try replacing '%s' with ?

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #12

          @VRonin i am using mysql a "?" will not work but thank you !

          VRoninV 1 Reply Last reply
          0
          • ? A Former User

            @VRonin i am using mysql a "?" will not work but thank you !

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #13

            @Gelo Ok, second try: %s instead of '%s' (I think %s automatically adds the 's)

            Did you try @mrjj 's solution?

            "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

            the_T 1 Reply Last reply
            1
            • VRoninV VRonin

              @Gelo Ok, second try: %s instead of '%s' (I think %s automatically adds the 's)

              Did you try @mrjj 's solution?

              the_T Offline
              the_T Offline
              the_
              wrote on last edited by
              #14

              @VRonin
              if you do a select * from table where something=%s , 'something' it will be replaced to select * from table where something=something, so if you have a string or char, you need to write it as

              • select * from table where something='%s' , 'something'
                or
              • select * from table where something=%s , '"something"'
                or the mysql connector will throw an exception
              cur.execute("select * from process where finished=%s",'K')
              Traceback (most recent call last):
                File "<stdin>", line 1, in <module>
                File "/usr/lib/python2.7/site-packages/mysql/connector/cursor.py", line 507, in execute
                  self._handle_result(self._connection.cmd_query(stmt))
                File "/usr/lib/python2.7/site-packages/mysql/connector/connection.py", line 722, in cmd_query
                  result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
                File "/usr/lib/python2.7/site-packages/mysql/connector/connection.py", line 640, in _handle_result
                  raise errors.get_exception(packet)
              mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s' at line 1
              

              -- No support in PM --

              ? 1 Reply Last reply
              2
              • ? A Former User

                def button_click(self):
                #QString object
                QUERY_DELETE = self.Asset_tag.text()

                    #Conditional statement starts here
                    
                    #Ends here
                    
                    con = mysql.connector.connect(user="root",password="admingelo",host="localhost",database="test")
                    manager = con.cursor()
                    
                    delete_this_data = ("DELETE FROM test_table WHERE name='%s'")
                
                    data = (str(QUERY_DELETE))
                
                    manager.execute(delete_this_data,data)
                
                    con.commit()
                    print "Delete Successful!"
                
                the_T Offline
                the_T Offline
                the_
                wrote on last edited by
                #15

                @Gelo
                https://dev.mysql.com/doc/connector-python/en/connector-python-examples.html There you find some examples how to use mysql connector in python

                -- No support in PM --

                1 Reply Last reply
                3
                • the_T the_

                  @VRonin
                  if you do a select * from table where something=%s , 'something' it will be replaced to select * from table where something=something, so if you have a string or char, you need to write it as

                  • select * from table where something='%s' , 'something'
                    or
                  • select * from table where something=%s , '"something"'
                    or the mysql connector will throw an exception
                  cur.execute("select * from process where finished=%s",'K')
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                    File "/usr/lib/python2.7/site-packages/mysql/connector/cursor.py", line 507, in execute
                      self._handle_result(self._connection.cmd_query(stmt))
                    File "/usr/lib/python2.7/site-packages/mysql/connector/connection.py", line 722, in cmd_query
                      result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
                    File "/usr/lib/python2.7/site-packages/mysql/connector/connection.py", line 640, in _handle_result
                      raise errors.get_exception(packet)
                  mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s' at line 1
                  
                  ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #16

                  @the_ def button_click(self):
                  #QString object
                  QUERY_DELETE = self.Asset_tag.text()
                  data = QUERY_DELETE
                  #Conditional statement starts here

                      #Ends here
                      
                      con = mysql.connector.connect(user="root",password="admingelo",host="localhost",database="test")
                      manager = con.cursor()
                      
                      delete_this_data = ("""DELETE FROM test_table WHERE name = %s""",str(data))
                      manager.execute(delete_this_data)
                      print data
                      con.commit()
                      print "Delete Successful!"
                  

                  Traceback (most recent call last):
                  File "C:\Users\angelo\Desktop\IMS Delta EarthMoving Incorporated\QtDeleteQuery.py", line 39, in button_click
                  manager.execute(delete_this_data)
                  File "C:\Python27\lib\site-packages\mysql\connector\cursor.py", line 529, in execute
                  stmt = operation.encode(self._connection.python_charset)
                  AttributeError: 'tuple' object has no attribute 'encode'

                  the_T 1 Reply Last reply
                  0
                  • ? A Former User

                    @the_ def button_click(self):
                    #QString object
                    QUERY_DELETE = self.Asset_tag.text()
                    data = QUERY_DELETE
                    #Conditional statement starts here

                        #Ends here
                        
                        con = mysql.connector.connect(user="root",password="admingelo",host="localhost",database="test")
                        manager = con.cursor()
                        
                        delete_this_data = ("""DELETE FROM test_table WHERE name = %s""",str(data))
                        manager.execute(delete_this_data)
                        print data
                        con.commit()
                        print "Delete Successful!"
                    

                    Traceback (most recent call last):
                    File "C:\Users\angelo\Desktop\IMS Delta EarthMoving Incorporated\QtDeleteQuery.py", line 39, in button_click
                    manager.execute(delete_this_data)
                    File "C:\Python27\lib\site-packages\mysql\connector\cursor.py", line 529, in execute
                    stmt = operation.encode(self._connection.python_charset)
                    AttributeError: 'tuple' object has no attribute 'encode'

                    the_T Offline
                    the_T Offline
                    the_
                    wrote on last edited by
                    #17

                    @Gelo

                    The error says exactly what the problem is.
                    You are passing a tuple (something like (object, object) as a parameter to the cursor.execute()

                    • What exactly is data and where do you get it from?
                    • You dont need to use """ for a single line string
                    • Even if you replace , with % in delete_this_data = ("""DELETE FROM test_table WHERE name = %s""",str(data)) it will not work as you have to put a string in ' ' inside a SQL statement

                    (i have set data to "test" for this example)

                    >>> data = "text"
                    >>> delete_this_data = ("""DELETE FROM test_table WHERE name = %s""",str(data))
                    >>> print delete_this_data
                    ('DELETE FROM test_table WHERE name = %s', 'text')
                    >>> print type(delete_this_data)
                    <type 'tuple'>
                    >>> delete_this_data = ("""DELETE FROM test_table WHERE name = %s"""%str(data))
                    >>> print delete_this_data
                    DELETE FROM test_table WHERE name = text
                    >>> delete_this_data = ("""DELETE FROM test_table WHERE name = '%s'"""%str(data))
                    >>> print delete_this_data
                    DELETE FROM test_table WHERE name = 'text'
                    
                    

                    -- No support in PM --

                    ? the_T 3 Replies Last reply
                    2
                    • the_T the_

                      @Gelo

                      The error says exactly what the problem is.
                      You are passing a tuple (something like (object, object) as a parameter to the cursor.execute()

                      • What exactly is data and where do you get it from?
                      • You dont need to use """ for a single line string
                      • Even if you replace , with % in delete_this_data = ("""DELETE FROM test_table WHERE name = %s""",str(data)) it will not work as you have to put a string in ' ' inside a SQL statement

                      (i have set data to "test" for this example)

                      >>> data = "text"
                      >>> delete_this_data = ("""DELETE FROM test_table WHERE name = %s""",str(data))
                      >>> print delete_this_data
                      ('DELETE FROM test_table WHERE name = %s', 'text')
                      >>> print type(delete_this_data)
                      <type 'tuple'>
                      >>> delete_this_data = ("""DELETE FROM test_table WHERE name = %s"""%str(data))
                      >>> print delete_this_data
                      DELETE FROM test_table WHERE name = text
                      >>> delete_this_data = ("""DELETE FROM test_table WHERE name = '%s'"""%str(data))
                      >>> print delete_this_data
                      DELETE FROM test_table WHERE name = 'text'
                      
                      
                      ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #18

                      @the_ said in Why name='%s' is not deleting my query but if i set to name='something' it deletes.:

                      delete_this_data = ("""DELETE FROM test_table WHERE name = %s"""%str(data))

                      i got an error invalid syntax with %str(data)

                      1 Reply Last reply
                      0
                      • the_T the_

                        @Gelo

                        The error says exactly what the problem is.
                        You are passing a tuple (something like (object, object) as a parameter to the cursor.execute()

                        • What exactly is data and where do you get it from?
                        • You dont need to use """ for a single line string
                        • Even if you replace , with % in delete_this_data = ("""DELETE FROM test_table WHERE name = %s""",str(data)) it will not work as you have to put a string in ' ' inside a SQL statement

                        (i have set data to "test" for this example)

                        >>> data = "text"
                        >>> delete_this_data = ("""DELETE FROM test_table WHERE name = %s""",str(data))
                        >>> print delete_this_data
                        ('DELETE FROM test_table WHERE name = %s', 'text')
                        >>> print type(delete_this_data)
                        <type 'tuple'>
                        >>> delete_this_data = ("""DELETE FROM test_table WHERE name = %s"""%str(data))
                        >>> print delete_this_data
                        DELETE FROM test_table WHERE name = text
                        >>> delete_this_data = ("""DELETE FROM test_table WHERE name = '%s'"""%str(data))
                        >>> print delete_this_data
                        DELETE FROM test_table WHERE name = 'text'
                        
                        
                        the_T Offline
                        the_T Offline
                        the_
                        wrote on last edited by the_
                        #19

                        @the_ said in Why name='%s' is not deleting my query but if i set to name='something' it deletes.:

                        • What exactly is data and where do you get it from?

                        And please remove the unneccessary " from a single line string

                        -- No support in PM --

                        ? 1 Reply Last reply
                        1
                        • the_T the_

                          @the_ said in Why name='%s' is not deleting my query but if i set to name='something' it deletes.:

                          • What exactly is data and where do you get it from?

                          And please remove the unneccessary " from a single line string

                          ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #20

                          @the_ raise errors.get_exception(packet)
                          mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column 'meratots' in 'where clause'

                          1 Reply Last reply
                          0
                          • the_T the_

                            @Gelo

                            The error says exactly what the problem is.
                            You are passing a tuple (something like (object, object) as a parameter to the cursor.execute()

                            • What exactly is data and where do you get it from?
                            • You dont need to use """ for a single line string
                            • Even if you replace , with % in delete_this_data = ("""DELETE FROM test_table WHERE name = %s""",str(data)) it will not work as you have to put a string in ' ' inside a SQL statement

                            (i have set data to "test" for this example)

                            >>> data = "text"
                            >>> delete_this_data = ("""DELETE FROM test_table WHERE name = %s""",str(data))
                            >>> print delete_this_data
                            ('DELETE FROM test_table WHERE name = %s', 'text')
                            >>> print type(delete_this_data)
                            <type 'tuple'>
                            >>> delete_this_data = ("""DELETE FROM test_table WHERE name = %s"""%str(data))
                            >>> print delete_this_data
                            DELETE FROM test_table WHERE name = text
                            >>> delete_this_data = ("""DELETE FROM test_table WHERE name = '%s'"""%str(data))
                            >>> print delete_this_data
                            DELETE FROM test_table WHERE name = 'text'
                            
                            
                            ? Offline
                            ? Offline
                            A Former User
                            wrote on last edited by
                            #21

                            @the_ Thank you for this it helps me a lot! what does this do , i remove that and it works fine! Thank you men! your so awesome! :D

                            1 Reply Last reply
                            1

                            • Login

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