Thread: MySQLdbd error

    #1
  1. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13

    Question MySQLdbd error


    Code:
    Hello today i deiced to go form python 2.6 to python 3.2.3 
    
    although in 2.6 my script runs in the ht enewest python i ge this error: 
    
    
    Traceback (most recent call last): 
      File "/opt/python3/lib/python3.2/site-packages/MySQL_python-1.2.3-py3.2-linux-x86_64.egg/MySQLdb/cursors.py", line 171, in execute 
        r = self._query(query) 
      File "/opt/python3/lib/python3.2/site-packages/MySQL_python-1.2.3-py3.2-linux-x86_64.egg/MySQLdb/cursors.py", line 330, in _query 
        rowcount = self._do_query(q) 
      File "/opt/python3/lib/python3.2/site-packages/MySQL_python-1.2.3-py3.2-linux-x86_64.egg/MySQLdb/cursors.py", line 294, in _do_query 
        db.query(q) 
    _mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1") 
    
    During handling of the above exception, another exception occurred: 
    
    Traceback (most recent call last): 
      File "metrites.py", line 193, in <module> 
        cur.execute( '''SELECT hits FROM counters WHERE url = %s''', (page,) ) 
      File "/opt/python3/lib/python3.2/site-packages/MySQL_python-1.2.3-py3.2-linux-x86_64.egg/MySQLdb/cursors.py", line 184, in execute 
        self.errorhandler(self, exc, value) 
      File "/opt/python3/lib/python3.2/site-packages/MySQL_python-1.2.3-py3.2-linux-x86_64.egg/MySQLdb/connections.py", line 36, in defaulterrorhandler 
        raise errorclass(errorvalue) 
    _mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1") 
    
    
    
    you can also see it at http://superhost.gr 
    
    Any ideas what might be the problem? 
    
    My webhost (hostgator.com) says that The MySQLdb module is already installed for python 3 on this server. 
    
    Can you please tell me what's the nature of this error? 
    
    Thank you.
    I tried the following:

    print( page )
    sys.exit(0)
    cur.execute( '''SELECT hits FROM counters WHERE url = %s''', (page,) )


    and the result is printed in the webpage as 'index.html'

    so page seem s to be a string but the error is still persistant.
    Anything else i need to try?

    And also i must show you that 'page' values are calculated by:

    # detect how 'index.html' is called and validate variables 'htmlpage' & 'page'
    if page and os.path.isfile( '/home/nikos/www/cgi-bin/' + page ):
    page = page
    elif form.getvalue('show') and os.path.isfile( htmlpage ):
    page = htmlpage.replace( '/home/nikos/public_html/', '' )
    else:
    page = 'index.html'


    so in all cases theya re string.

    i just dont see whats worng....
    What is now proved was once only imagined!
  2. #2
  3. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    please someone give me a hand here1
    What is now proved was once only imagined!
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Try this statement. I added a semi-colon and, not knowing if cur.execute formats the string for you I formatted the string before passing it to cur.execute

    cur.execute( '''SELECT hits FROM counters WHERE url = %s ;'''%(page,) )



    Looking at superhost.gr
    Code:
    =>  187                 print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] )

    excepinfo is probably mis-spelled---I have no idea what you intend.l


    Python3 raised exceptions in an except: clause giving the double exception message that I don't think you'd have seen in python2. Maybe the purpose is to help novice python programmers who haven't learned to make try: except: clause trap a specific statement looking for specific Errors.

    But let's get to what I think was the original problem:

    _mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1")

    Which may have come from this statement:

    cur.execute( '''SELECT hits FROM counters WHERE url = %s''', (page,) )

    But I don't know anything about cur.execute . Maybe this would work?

    cur.execute( '''SELECT hits FROM counters WHERE url = %s ;'''%(page,) )
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    Now my program gives internal server error and i haveno idea why.

    I noticed that in another scipr of mine

    Code:
     187                 print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] )
    produces an error.

    Maybe this line which iuses to work in python 2.6 doesnt in python 3.2.3 ?

    do i have to switch it to soemthing else?
    What is now proved was once only imagined!
  8. #5
  9. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    I had encoding isseus as well!

    Now i tireid your suggestion changing comma with '%' and now the error is more clear.

    Code:
    _mysql_exceptions.OperationalError: (1054, "Unknown column 'index.html' in 'where clause'")

    loook at http://superhost.gr please to see the whoel traceback
    What is now proved was once only imagined!
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Find out what type of object
    sys.exc_info()[1]
    this is. What are it's attributes?

    print ( "Query Error: ", dir(sys.exc_info()[1]))

    superhost.gr tells you

    AttributeError: 'ProgrammingError' object has no attribute 'excepinfo'
    args = ("'ProgrammingError' object has no attribute 'excepinfo'",)
    with_traceback = <built-in method with_traceback of AttributeError object>
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    Code:
    Even better: 
    
    try: 
            sql = '''SELECT hits FROM counters WHERE url = %s''' % page 
            print( sql ) 
            cur.execute( sql ) 
            data = cur.fetchone() 
    except MySQLdb.ProgrammingError as e: 
            print ( "Query Error: ", dir( sys.exc_info()[1] ) )
    ========

    sql statement seems okey...
    but then why the problem?
    Last edited by Nik; March 27th, 2013 at 02:24 PM.
    What is now proved was once only imagined!
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Not knowing anything about cursor execute, I would stick a semi-colon in the sql statement.

    sql = '''SELECT hits FROM counters WHERE url = '%s';''' % page


    and also, not knowing any better, I'd quote the string in the query.
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    The following works!

    Code:
    	cur.execute( '''SELECT hits FROM counters WHERE url = %s''') , (page,)
    is there a difefrence between the above and

    Code:
    	cur.execute( '''SELECT hits FROM counters WHERE url = %s''' , (page,) )
    Also now http://superhost.gr seems to stuck in the following line which i try to open an acii file to slect a random line, please take a look.
    What is now proved was once only imagined!
  18. #10
  19. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480

    Different


    cur.execute( '''SELECT hits FROM counters WHERE url = %s''') , (page,)

    is a tuple of two objects. The first is the result of
    cur.execute( '''SELECT hits FROM counters WHERE url = %s''')
    and the second is
    (page,)


    cur.execute( '''SELECT hits FROM counters WHERE url = %s''' , (page,) )
    is the result of the execute method of the cur object called with two arguments.



    >>> int('10010110101', 2)
    1205
    >>> int('10010110101'), 2
    (10010110101, 2)
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  20. #11
  21. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    Than k you very much for the explanation.

    Also now http://superhost.gr seems to stuck in the following line which i try to open an acii file to slect a random line, please take a look.
    What is now proved was once only imagined!
  22. #12
  23. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    Then how come the same .txt file opens normally in Python 2.6

    i also tried to save ti as .ascii istead of utf-8 file and still same error also if i quote it out same things happen in those liens too.

    f = open( "/home/nikos/www/" + page )
    htmldata = f.read()

    f.read produced the same kind of error. why?
    What is now proved was once only imagined!
  24. #13
  25. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    this worked

    quote = random.choice( list( open( "/home/nikos/www/data/private/quotes.txt", 'rb') ) )

    and also this

    f = open( "/home/nikos/www/" + page, 'rb' )

    i dont know why python 3 needs 'rb' though.

    Now ima having problem with this:

    htmldata = htmldata % (quote, music)

    it says soemthign about wrong operand....
    What is now proved was once only imagined!
  26. #14
  27. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Code:
    >>> f = open( '/home/lambertdw/.bashrc', 'rb' )
    >>> htmldata = f.read()
    >>> type(htmldata)
    <class 'bytes'>
    >>> type('string?')
    <class 'str'>
    >>> type(htmldata.decode('utf8'))
    <class 'str'>
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  28. #15
  29. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,280
    Rep Power
    13
    Can you do me a big favour please?

    may i send you my code or perhaps jailed linux access so to understand whats wrong and altyhough i get no erros now i see a blank poage at http://superhost.gr

    Please man iam struggling 2 days to conever a 2,6 script to 3.2.3

    please accept!!!
    What is now proved was once only imagined!

IMN logo majestic logo threadwatch logo seochat tools logo