Menu

New bug in 1.0 or am I doing something wrong?

Code Rage
2009-04-02
2013-04-29
  • Code Rage

    Code Rage - 2009-04-02

    A few years ago, I wrote a small word game for some friends and I to play.  But, after updating to pymssql 1.0.1 form 0.8 and python 2.6.1 from 2.5.3, it quit working, and I have no idea why...   I modified pieces of code that changed, but for some reason I don't understand why this code is failing.. 

    Here's a piece of code I got to fail every time from the game:

    con = pymssql.connect(host='sqldatabase.intranet.com',user='user',password='something unseen',database='games')
    cur = con.cursor()
    # Select a random available word.
    newgamequerry = "SELECT TOP 1 Word FROM WordGame where choosable = 1 ORDER BY NEWID(); "
    cur.execute(newgamequerry )
    listing = cur.fetchall()
    if cur.rowcount == 0:
      # Never executes.
      Methods.respond(" Game db empty\n",135);
    elif cur.rowcount > 15:
      # Never executes.
      Methods.respond(" Too much to say.. Something went wrong \n",135);
    else:
      for x in listing:
        Methods.respond( " The word is chosen \n",135);
        # Set game state vars (Omitted here)
        # Debug - ask if the word we just selected is in the database.
        querry = "Select word from WordGame where Word = '" + str(x[0]) + "'";
        cur.execute(querry)
        if cur.rowcount == 0:
          # ALWAYS executes... it shouldn't.
          Methods.debugoutput("\nOkay, what the @@@@? [" + querry + "] returned not found.\n");

    What am I doing wrong? :( 

    The WordGame database has 2 columns - Word and IsChoosable.

    Thank you =)

     
    • A

      A - 2009-04-02

      Could you please post error you receive?

       
      • Code Rage

        Code Rage - 2009-04-02

        There is no error message given, except by the code I posted...
        The formatting got all goofed up by the post... But here:

        newgamequerry = "SELECT TOP 1 Word FROM WordGame where choosable = 1 ORDER BY NEWID(); "
        cur.execute(newgamequerry )
        listing = cur.fetchall()

        listing  contains 1 row, the randomly requested word.

        Then, that word is used in a new querry:

        querry = "Select word from WordGame where Word = '" + str(x[0]) + "'";
        cur.execute(querry)

        Which always contains zero rows... There is no error message given by pymssql. :(

         
    • A

      A - 2009-04-02

      I'm surprised it doesn't work, please insert a `print' instruction after fetchall() to examine what's in x[0] and see if it's what you expect.

       
    • Code Rage

      Code Rage - 2009-04-02

      I figured it out, but I don't know if it's intended behavior...

      The first query works fine:

      cur.execute(newgamequerry )
      listing = cur.fetchall()
      if cur.rowcount == 0:

      but the second does not:

      cur.execute(querry)
      if cur.rowcount == 0:

      The difference, is the second doesn't care about what is actually IN the result set, so it never called fetchall().  It only cared if rows were returned... apparently, cur.rowcount is zero until fetchall is called...

      Changing it to:
      cur.execute(querry)
      cur.fetchall()
      if cur.rowcount == 0:

      makes it work.

       

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.