Menu

2 questions

Help
Anonymous
2001-08-05
2012-09-19
  • Anonymous

    Anonymous - 2001-08-05

    Hi,
    I have 2 questions that I was hoping you could help me with, sorry if they have already been asked, I tried reading the rest of the forum, but I dont think they have been asked.

    The first question kind of relates to the person asking about getting the field names out of the result. I am new to python (3 days or so) so forgive me if there is something obvious that I am not doing. In PHP you can get what I guess would be a dictionary back from the database, so you could go resultset[username] and get the username. With MySQLdb it seems that I get back a plain old tuple with no way to get the handy dictionary like access. Hopefully I have just missed something and someone will be able to help me.

    The second question I have is in regards to the formatting. This is what I have:
    >>> def upq3(number):
    ...     query3 = "select * from tbl_assignmentSpecification where subjectId=%d"
    ...     cur.execute(query3, number)
    ...     b = cur.fetchall()
    ...     print b
    ...

    And here is where I am having the trouble:
    >>> upq3(1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "<stdin>", line 3, in upq3
      File "/usr/lib/python2.0/site-packages/MySQLdb/cursors.py", line 67, in execute
        r = self._query(query % escape(args, qc))
    TypeError: an integer is required

    The column in the DB is an integer and the value I am passing in is an integer, but it doesnt appear to like it. Is it because it needs to be a long integer? If it does, how would I make it a long integer?

    Thanks for your patience and time, and hopefully someone will be able to help me.

    - michael

     
    • Andy Dustman

      Andy Dustman - 2001-08-05

      1) Use a DictCursor, i.e. cur=db.cursor(MySQLdb.cursors.DictCursor)

      2) Rewrite one of two ways:

      a)

      >>> def upq3(number):
      ...     query3 = "select * from tbl_assignmentSpecification where subjectId=%d" % number
      ...     cur.execute(query3)
      ...     b = cur.fetchall()
      ...     print b
      ...

      b)

      >>> def upq3(number):
      ...     query3 = "select * from tbl_assignmentSpecification where subjectId=%s"
      ...     cur.execute(query3, (number,))
      ...     b = cur.fetchall()
      ...     print b
      ...

      The latter is the "correct" method, particularly when you have multiple parameters. This is also possible:

      c)

      >>> def upq3(dict):
      ...     query3 = "select * from tbl_assignmentSpecification where subjectId=%(number)s"
      ...     cur.execute(query3, dict)
      ...     b = cur.fetchall()
      ...     print b
      ...

       
    • Anonymous

      Anonymous - 2001-08-07

      Thanks for your help. I just have one more question, arent the place holders supposed to be the C style printf format characters. So for an integer wouldnt I use %d?

       
      • Andy Dustman

        Andy Dustman - 2001-08-07

        Always use %s. MySQLdb converts whatever value it finds into a correct SQL literal value, which is a string. If you pass it the integer 69, it will be converted to the string '69', and substituted into the query.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.