JSON from Python mysqldb

J

jpuopolo

All:

I am using Python to read some records from the MySQL database. I am
using the mysqldb library, and things are working well.

Now, I would like to pass back the results of the query to a Web-based
front end, and I would like to use JSON. Is there a library/example of
creating a JSON array from a mysqldb row or row set?

Thanks,
jpuopolo
 
L

Larry Bates

jpuopolo said:
All:

I am using Python to read some records from the MySQL database. I am
using the mysqldb library, and things are working well.

Now, I would like to pass back the results of the query to a Web-based
front end, and I would like to use JSON. Is there a library/example of
creating a JSON array from a mysqldb row or row set?

Thanks,
jpuopolo

Google turns up several high quality links, just look for 'python json'.

-Larry
 
J

jpuopolo

Google turns up several high quality links, just look for 'python json'.

-Larry

Larry:

Thank you for the reply. Unfortunately, some of the libs the Google
search returns bail when
converting a database record into a JSON object. I may be using it/
them incorrectly, so I'll post
results to this newsgroup accordingly.

Best,
John
 
T

TYR

There's always the naive option.

query = ('species', 'lifestyle', 'coolness', 'tentacles=>8')
db.execute('SELECT %s, %s, %s % FROM creatures WHERE %s;' % query)
record = dict(zip((query), (db.fetchall()))
array = '''{
'''
for k,v in record:
array.append('"(k)":"(v)"')
array.append('''
}''')
 
T

TYR

There's always the naive option.

query = ('species', 'lifestyle', 'coolness', 'tentacles=>8')
db.execute('SELECT %s, %s, %s % FROM creatures WHERE %s;' % query)
record = dict(zip((query), (db.fetchall()))
array = '''{
             '''
for k,v in record:
  array.append('"(k)":"(v)"')
array.append('''
                }''')

Actually, don't.

query = ('species', 'lifestyle', 'coolness')
limit = ('tentacles=>8')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top