JSON from Python mysqldb

Discussion in 'Python' started by jpuopolo, Aug 26, 2008.

  1. jpuopolo

    jpuopolo Guest

    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
     
    jpuopolo, Aug 26, 2008
    #1
    1. Advertising

  2. jpuopolo

    Larry Bates Guest

    jpuopolo wrote:
    > 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
     
    Larry Bates, Aug 26, 2008
    #2
    1. Advertising

  3. jpuopolo

    jpuopolo Guest

    On Aug 26, 3:15 pm, Larry Bates <> wrote:
    > jpuopolo wrote:
    > > 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


    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
     
    jpuopolo, Aug 27, 2008
    #3
  4. jpuopolo

    TYR Guest

    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('''
    }''')
     
    TYR, Aug 27, 2008
    #4
  5. jpuopolo

    TYR Guest

    On Aug 27, 2:37 pm, TYR <> wrote:
    > 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')
     
    TYR, Aug 27, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Frank
    Replies:
    0
    Views:
    269
    Florian Frank
    Jun 30, 2009
  2. sajuptpm
    Replies:
    2
    Views:
    411
    sajuptpm
    Dec 28, 2012
  3. Acácio Centeno
    Replies:
    1
    Views:
    314
    dieter
    Feb 15, 2013
  4. Bryan Britten
    Replies:
    9
    Views:
    302
    Bryan Britten
    May 28, 2013
  5. David Karr
    Replies:
    1
    Views:
    198
    David Karr
    Jun 17, 2013
Loading...

Share This Page