JSON from Python mysqldb

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jpuopolo

    JSON from Python mysqldb

    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
  • Larry Bates

    #2
    Re: JSON from Python mysqldb

    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

    Comment

    • jpuopolo

      #3
      Re: JSON from Python mysqldb

      On Aug 26, 3:15 pm, Larry Bates <larry.ba...@vi talEsafe.comwro te:
      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

      Comment

      • TYR

        #4
        Re: JSON from Python mysqldb

        There's always the naive option.

        query = ('species', 'lifestyle', 'coolness', 'tentacles=>8')
        db.execute('SEL ECT %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('' '
        }''')




        Comment

        • TYR

          #5
          Re: JSON from Python mysqldb

          On Aug 27, 2:37 pm, TYR <a.harrow...@gm ail.comwrote:
          There's always the naive option.
          >
          query = ('species', 'lifestyle', 'coolness', 'tentacles=>8')
          db.execute('SEL ECT %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' )

          Comment

          Working...