Performing a MySQL query

This is the place for queries that don't fit in any of the other categories.

Performing a MySQL query

Postby fbartolom » Sat Feb 08, 2014 3:10 pm

Hullo,
I am desperately trying to perform a query from a python3 script to a MySQL DB,both of the on a centOS system. I tried both directly accessing the DB and calling a php script doing it. I could not go the former way as no SQL library seems to work for Python3, the latter instead needed the urllib2 library that I could not get how to install on the system. Finally I could not manage to install apt-get by which a certain python-tk could be downloaded which allegedly would have supported urllib2.
Have you got any suggestion on the way I threaded or on the general issue?

Thanks, Fabrizio
fbartolom
 
Posts: 15
Joined: Tue Jan 28, 2014 9:30 am

Re: Performing a MySQL query

Postby stranac » Sat Feb 08, 2014 3:17 pm

fbartolom wrote:I could not go the former way as no SQL library seems to work for Python3

That's not even close to being true...
Some I know for a fact work with python 3 would be SQLAlchemy(supports many sql databases) and oursql(specifically for MyQSL).

fbartolom wrote:the latter instead needed the urllib2 library that I could not get how to install on the system

urllib2 is a built-in module in python 2.
In python 3, it's a part of urllib.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm

Re: Performing a MySQL query

Postby fbartolom » Sat Feb 08, 2014 5:15 pm

stranac wrote:
fbartolom wrote:I could not go the former way as no SQL library seems to work for Python3

That's not even close to being true...
Some I know for a fact work with python 3 would be SQLAlchemy(supports many sql databases) and oursql(specifically for MyQSL).

Have you got some code examples and installation instructions for CentOS? I keep bouncing against solutions with apt-get that I did not manage to have working on CentOS.
fbartolom wrote:the latter instead needed the urllib2 library that I could not get how to install on the system

urllib2 is a built-in module in python 2.
In python 3, it's a part of urllib.

It may be so, but if I only include urllib, I receive error:
Traceback (most recent call last):
File "direction.py", line 118, in <module>
print(indexForPalina(80626))
File "direction.py", line 16, in indexForPalina
mydata=urllib.urlencode(mydata)
AttributeError: 'module' object has no attribute 'urlencode'
when I try to apply urlencode to urlllib instead of urllib2.
I also tried:
from urllib import urllib2
but I got:
/opt/python3/bin/python3 direction.py "via ugolino vivaldi" "via conca d'oro"
Traceback (most recent call last):
File "direction.py", line 13, in <module>
from urllib import urllib2
ImportError: cannot import name urllib2

This is the function I would like to use:
Code: Select all
import urllib
def indexForPalina(palina):
# if you want output
   mydata=[('palina',palina)]    #The first is the var name the second is the value
   mydata=urllib2.urlencode(mydata)
   path='http://localhost/iPhone/inarrivo/php/indexForPalina'    #the url you want to POST to
   req=urllib2.Request(path, mydata)
   req.add_header("Content-type", "application/x-www-form-urlencoded")
   page=urllib2.urlopen(req).read()
   print(page)
   return

in which urllib2 seems to be needed for 3 methods.
What is the way out?
fbartolom
 
Posts: 15
Joined: Tue Jan 28, 2014 9:30 am

Re: Performing a MySQL query

Postby stranac » Sat Feb 08, 2014 5:25 pm

fbartolom wrote:ave you got some code examples and installation instructions for CentOS?

Sorry, I know nothing about CentOS.
But you can probably just run setup.py install on stuff.
Or if you have it, use pip.

As for getting urllib to work, the way out is actually reading the docs(recommended).
Or just give it to 2to3 and hope it fixes it.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm

Re: Performing a MySQL query

Postby fbartolom » Sat Feb 08, 2014 5:37 pm

stranac wrote:
fbartolom wrote:ave you got some code examples and installation instructions for CentOS?

Sorry, I know nothing about CentOS.
But you can probably just run setup.py install on stuff.
Or if you have it, use pip.

As for getting urllib to work, the way out is actually reading the docs(recommended).
Or just give it to 2to3 and hope it fixes it.

As a matter of fact I changed my code to:
Code: Select all
import urllib
def indexForPalina(palina):
   mydata=[('palina',palina)]    #The first is the var name the second is the value
   mydata=urllib.parse.urlencode(mydata)
   path='http://localhost/iPhone/inarrivo/php/indexForPalina'    #the url you want to POST to
   page=urllib.request.urlopen(path, mydata).read()
   #print(page)
   return

following your hint and the instructions at:
http://docs.python.org/3/library/urllib.request.html#module-urllib.request
Yet I strangely receive error:
File "direction.py", line 18, in indexForPalina
page=urllib.request.urlopen(path, mydata).read()
AttributeError: 'module' object has no attribute 'request'
fbartolom
 
Posts: 15
Joined: Tue Jan 28, 2014 9:30 am

Re: Performing a MySQL query

Postby stranac » Sat Feb 08, 2014 5:43 pm

Yes, importing urllib doesn't automatically import all the modules inside it. You want:
Code: Select all
import urllib.request, urllib.parse
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm

Re: Performing a MySQL query

Postby fbartolom » Sat Feb 08, 2014 5:45 pm

I forgot:
Code: Select all
from urllib import request

Now it loads it and error strangely moved down:
File "/opt/python3/lib/python3.2/urllib/request.py", line 138, in urlopen
return opener.open(url, data, timeout)
File "/opt/python3/lib/python3.2/urllib/request.py", line 364, in open
req = meth(req)
File "/opt/python3/lib/python3.2/urllib/request.py", line 1052, in do_request_
raise TypeError("POST data should be bytes"
TypeError: POST data should be bytes or an iterable of bytes. It cannot be str.

The new code is:
Code: Select all
def indexForPalina(palina):
# if you want output
   mydata=[('palina',palina)]    #The first is the var name the second is the value
   mydata=urllib.parse.urlencode(mydata)
   path='http://localhost/iPhone/inarrivo/php/indexForPalina'    #the url you want to POST to
   page=urllib.request.urlopen(path, mydata)
   #print(page)
   return

Have you got some code examples for its usage?
fbartolom
 
Posts: 15
Joined: Tue Jan 28, 2014 9:30 am

Re: Performing a MySQL query

Postby fbartolom » Sat Feb 08, 2014 5:53 pm

Ok, I managed to submit the request:
Code: Select all
from urllib import request
from urllib import parse
def indexForPalina(palina):
# if you want output
   mydata=[('palina',palina)]    #The first is the var name the second is the value
   mydata=urllib.parse.urlencode(mydata)
   path='http://localhost/iPhone/inarrivo/php/indexForPalina.php'
   page=urllib.request.urlopen(path, mydata.encode('utf-8'))
   print(page)
   return

and I get a <http.client.HTTPResponse object at 0x90641ec>
How do I get the data from it?
fbartolom
 
Posts: 15
Joined: Tue Jan 28, 2014 9:30 am

Re: Performing a MySQL query

Postby fbartolom » Sat Feb 08, 2014 5:58 pm

Ok, done:
print(result.read().decode())

yet I receive the correct result followed by None
where does it come from?
fbartolom
 
Posts: 15
Joined: Tue Jan 28, 2014 9:30 am

Re: Performing a MySQL query

Postby stranac » Sat Feb 08, 2014 6:04 pm

Hard to tell without seeing the exact code you're using...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm

Re: Performing a MySQL query

Postby Yoriz » Sat Feb 08, 2014 6:43 pm

It looks like you are probably using print at the end of a function and then either using return with nothing or no return, in this case None will be returned.
so when you do
Code: Select all
print yourfunction()
your function does its own printing and the print before yourfunction prints the returned None.
Either dont print in your function and return the value or just call your function without print.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1020
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: pam and 1 guest