Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,402
    Rep Power
    0

    Question Cannot run a single MySQLdb execute with MySQLdb in Python 3.2.3


    0
    down vote
    favorite
    I'm about to go nuts with python 3.2.3

    Do you see something wrong with the following statement?

    Code:
    cur.execute('''SELECT hits FROM counters WHERE url = ?''', (page,)) 
    data = cur.fetchone()
    because as you can see by visiting my webpage at http://superhost.gr it produces an error and I don't know why.

    I'm using MySQLdb.

    Ι'm using '?' or '%s'; the latter used to work flawlessly with python 2.6, but it does not in python 3.2.3

    Both these commands fail in python 3.2.3

    Code:
    cur.execute('''SELECT hits FROM counters WHERE url = ?''', (page,))
    Code:
    cur.execute('''SELECT hits FROM counters WHERE url = %s''', (page,))
    Any idea why?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    6
    Originally Posted by Nik
    Code:
    cur.execute('''SELECT hits FROM counters WHERE url = %s''', (page,))
    Any idea why?
    Maybe try putting quotes around %s?
  4. #3
  5. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,402
    Rep Power
    0
    But the whole sql command is inside quotes so, why wrapping it with extra quoting?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    6
    Because index.html is a string, and you need to tell mysql that.

    Code:
    # Valid
    select * from table where something = "something_else"
    # Invalid
    select * from table where something = something_else
  8. #5
  9. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,402
    Rep Power
    0
    i think you are right but as i had it used without problem in python 2.6. why not in 3.2.3?

    also you mean like that:

    Code:
    cur.execute('''SELECT hits FROM counters WHERE url = "?"''', (page,) )
    or like that?

    Code:
    cur.execute('''SELECT hits FROM counters WHERE url = "%s"''', (page,) )
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    6
    I'm not familiar with the syntax in your first example, so I would go with the second.
  12. #7
  13. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,402
    Rep Power
    0
    Both worked! i decided to sue this:

    Code:
    cur.execute('''SELECT hits FROM counters WHERE url = "?"''', page )
    Now please look at http://superhost.gr
    Can you understand eneyhting about this error?
  14. #8
  15. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,402
    Rep Power
    0
    With what i shoudl change this line?

    print ( "Query Error: ", dir( sys.exc_info()[1] ) )
  16. #9
  17. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,402
    Rep Power
    0
    Maybe chnage it with this?

    Code:
    	except MySQLdb.ProgrammingError as e:
    		con.rollback()		#something failed, rollback the entire transaction
    		print( repr(e) )
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    6
    Originally Posted by Nik
    Maybe chnage it with this?

    Code:
    	except MySQLdb.ProgrammingError as e:
    		con.rollback()		#something failed, rollback the entire transaction
    		print( repr(e) )
    How can you do rollback on a select query? You need to somehow get the error messages, maybe you should'nt put them there on the site but instead put them in a log file which you can only retrieve yourself.
  20. #11
  21. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,402
    Rep Power
    0
    ok aprt from that what is the python 3 way of displaying the error message?
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    6
    I don't mean to be rude, but why did you change to python3 if you had a working site in python2 code?

    There is help available for converting to python3: http://docs.python.org/2/library/2to3.html
  24. #13
  25. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,402
    Rep Power
    0
    No please feel free to talk as you wish.
    I wanted to make my website running Python 3 which is more new and bette


    i treid to execute metrites.py via my jailed shell, please take a look if i'am not tiring you and maybe you can see if there is nothign wrong because i dont see nayhting.

    The gethostbyaddr at the end its because the script run in cmd instead of in a browser.
    Please help, its alkmost ready to run correctly!
    Code:
    nikos@superhost.gr [~/www/cgi-bin]# /usr/bin/python3 metrites.py 
    <!--: spam
    Content-Type: text/html
    
    <body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> -->
    <body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> --> -->
    </font> </font> </font> </script> </object> </blockquote> </pre>
    </table> </table> </table> </table> </table> </font> </font> </font><body bgcolor="#f0f0f8">
    <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
    <tr bgcolor="#6622aa">
    <td valign=bottom>&nbsp;<br>
    <font color="#ffffff" face="helvetica, arial">&nbsp;<br><big><big><strong>KeyError</strong></big></big></font></td
    ><td align=right valign=bottom
    ><font color="#ffffff" face="helvetica, arial">Python 3.2.3: /usr/bin/python3<br>Thu Mar 28 09:41:53 2013</font></td></tr></table>
        
    <p>A problem occurred in a Python script.  Here is the sequence of
    function calls leading up to the error, in the order they occurred.</p>
    <table width="100%" cellspacing=0 cellpadding=0 border=0>
    <tr><td bgcolor="#d8bbff"><big>&nbsp;</big><a href="file:///home/nikos/public_html/cgi-bin/metrites.py">/home/nikos/public_html/cgi-bin/metrites.py</a> in <strong><module></strong>()</td></tr>
    <tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;&nbsp;26</small>&nbsp;userform&nbsp;=&nbsp;form.getvalue('userform')<br>
    </tt></font></td></tr>
    <tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;&nbsp;27</small>&nbsp;<br>
    </tt></font></td></tr>
    <tr><td bgcolor="#ffccee"><tt>=&gt;<small>&nbsp;&nbsp;&nbsp;28</small>&nbsp;host&nbsp;=&nbsp;socket.gethostbyaddr(&nbsp;os.environ['REMOTE_ADDR']&nbsp;)[0]<br>
    </tt></td></tr>
    <tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;&nbsp;29</small>&nbsp;date&nbsp;=&nbsp;(&nbsp;datetime.utcnow()&nbsp;+&nbsp;timedelta(hours=2)&nbsp;).strftime(&nbsp;'%y-%m-%d&nbsp;%H:%M:%S'&nbsp;)<br>
    </tt></font></td></tr>
    <tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;&nbsp;30</small>&nbsp;userinfo&nbsp;=&nbsp;os.environ['HTTP_USER_AGENT']<br>
    </tt></font></td></tr>
    <tr><td><small><font color="#909090">host <em>undefined</em>, <strong>socket</strong>&nbsp;= &lt;module 'socket' from '/opt/python3/lib/python3.2/socket.py'&gt;, socket.<strong>gethostbyaddr</strong>&nbsp;= &lt;built-in function gethostbyaddr&gt;, <strong>os</strong>&nbsp;= &lt;module 'os' from '/opt/python3/lib/python3.2/os.py'&gt;, os.<strong>environ</strong>&nbsp;= environ({'PROMPT_COMMAND': 'history -a', 'PERL_M...xa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:'})</font></small></td></tr></table>
    <table width="100%" cellspacing=0 cellpadding=0 border=0>
    <tr><td bgcolor="#d8bbff"><big>&nbsp;</big><a href="file:///opt/python3/lib/python3.2/os.py">/opt/python3/lib/python3.2/os.py</a> in <strong>__getitem__</strong>(self=environ({'PROMPT_COMMAND': 'history -a', 'PERL_M...xa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:'}), key='REMOTE_ADDR')</td></tr>
    <tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;448</small>&nbsp;<br>
    </tt></font></td></tr>
    <tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;449</small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__getitem__(self,&nbsp;key):<br>
    </tt></font></td></tr>
    <tr><td bgcolor="#ffccee"><tt>=&gt;<small>&nbsp;&nbsp;450</small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value&nbsp;=&nbsp;self._data[self.encodekey(key)]<br>
    </tt></td></tr>
    <tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;451</small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self.decodevalue(value)<br>
    </tt></font></td></tr>
    <tr><td><font color="#909090"><tt>&nbsp;&nbsp;<small>&nbsp;&nbsp;452</small>&nbsp;<br>
    </tt></font></td></tr>
    <tr><td><small><font color="#909090">value <em>undefined</em>, <strong>self</strong>&nbsp;= environ({'PROMPT_COMMAND': 'history -a', 'PERL_M...xa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:'}), self.<strong>_data</strong>&nbsp;= {b'CLASSPATH': b'.:/usr/local/jdk/lib/classes.zip', b'CVS_RSH': b'ssh', b'EDITOR': b'pico', b'GREP_COLOR': b'1;32', b'GREP_OPTIONS': b'--color', b'G_BROKEN_FILENAMES': b'1', b'HISTSIZE': b'5000', b'HOME': b'/home/nikos', b'HOSTNAME': b'menara.websitewelcome.com', b'INPUTRC': b'/etc/inputrc', ...}, self.<strong>encodekey</strong>&nbsp;= &lt;function encode&gt;, <strong>key</strong>&nbsp;= 'REMOTE_ADDR'</font></small></td></tr></table><p><strong>KeyError</strong>: b'REMOTE_ADDR'
    <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>args&nbsp;=
    (b'REMOTE_ADDR',)
    <br><tt><small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</small>&nbsp;</tt>with_traceback&nbsp;=
    &lt;built-in method with_traceback of KeyError object&gt;
    
    
    <!-- The above is a description of an error in a Python program, formatted
         for a Web browser because the 'cgitb' module was enabled.  In case you
         are not reading this in a Web browser, here is the original traceback:
    
    Traceback (most recent call last):
      File "metrites.py", line 28, in &lt;module&gt;
        host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]
      File "/opt/python3/lib/python3.2/os.py", line 450, in __getitem__
        value = self._data[self.encodekey(key)]
    KeyError: b'REMOTE_ADDR'
    
    -->
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    6
    You don't have an environment variable in your shell called REMOTE_ADDR. I think you're meaning to use some other environment variable.

    Code:
    26 userform = form.getvalue('userform')
         27 
    =>   28 host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]
         29 date = ( datetime.utcnow() + timedelta(hours=2) ).strftime( '%y-%m-%d %H:%M:%S' )
         30 userinfo = os.environ['HTTP_USER_AGENT']
  28. #15
  29. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,402
    Rep Power
    0
    this is correct when it runs from a browser thats not the issue here othwerise it would prodice an error.

    the question is why the blank screen...

    somehting with param style perhaps?
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo