Python Database Scripts

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

    #1

    Python Database Scripts

    Hello,

    Can anyone provide any kind of python database (mysql) code or point me
    to a link that has this? Just simple things as maybe using a driver,
    opening up a db, an insert and select. Any help would be greatly
    appreciated!

    Thanks,
    --Chuck

  • Peter Decker

    #2
    Re: Python Database Scripts

    On 12 Sep 2005 08:28:39 -0700, Chuck
    [color=blue]
    > Can anyone provide any kind of python database (mysql) code or point me
    > to a link that has this? Just simple things as maybe using a driver,
    > opening up a db, an insert and select. Any help would be greatly
    > appreciated![/color]

    It might be more than you're looking for, but there is a Python
    framework called Dabo that makes working with databases, including
    MySQL, very, very easy. Their URL is http://dabodev.com.

    --

    # p.d.

    Comment

    • jegenye2001

      #3
      Re: Python Database Scripts

      import MySQLdb

      # Create a connection object and create a cursor
      conn = MySQLdb.Connect (host="localhos t", port=3306, user="mysql",
      passwd="pwd123" , db="mytest")
      c = conn.cursor()

      # execute some SQL
      c.execute("SELE CT * FROM mystuff")

      # Fetch all results from the cursor into a sequence
      results = c.fetchall()
      for r in results:
      do_something(r)

      # close the connection
      conn.close()



      Cheers,
      Miklos
      --
      Software development: Python,Zope,Plo ne,PDF,XML,Miva Script


      Comment

      • Dennis Lee Bieber

        #4
        Re: Python Database Scripts

        On 12 Sep 2005 08:28:39 -0700, "Chuck" <willardthompso n@gmail.com>
        declaimed the following in comp.lang.pytho n:
        [color=blue]
        > Hello,
        >
        > Can anyone provide any kind of python database (mysql) code or point me
        > to a link that has this? Just simple things as maybe using a driver,
        > opening up a db, an insert and select. Any help would be greatly
        > appreciated!
        >[/color]
        Did you read the DB-API documentation?


        import db_module
        aConn = db_module.conne ct(db specific login parameters)
        aCurs = aConn.cursor()
        stat = aCurs("SQL Statement", (varying parameters))
        for row in aCurs.fetch():
        #do stuff with the data
        aCurs.close()
        aConn.close()

        with sometimes an aCurs.commit() or aCurs.rollback( )
        --[color=blue]
        > =============== =============== =============== =============== == <
        > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
        > wulfraed@dm.net | Bestiaria Support Staff <
        > =============== =============== =============== =============== == <
        > Home Page: <http://www.dm.net/~wulfraed/> <
        > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

        Comment

        • ncf

          #5
          Re: Python Database Scripts

          Hmm...sorry to go a little off topic here, but I, also, have been
          striving to learn Python/MySQL for a while using MySQL's official
          thing. Can you please explain to me why one must use a cursor and can't
          just do an execute on the connction? :confused about the subject:

          Comment

          • jegenye2001

            #6
            Re: Python Database Scripts

            Well, for a single connection object you could use several cursor
            objects and juggle with all of them in your program. This can come in
            handy if it's not about a simple script like I put in here. You can
            reuse the results from the cursors, etc. without issuing more,
            potentially resource-hungry, SQL statements.
            DB connections can also be shared across threads (I think.. or at least
            for some kind of DB adapters.)


            Cheers,
            Miklos
            --
            The ring of the friendly serpent in business suit: Python, Zope, Plone


            Comment

            • Chuck

              #7
              Re: Python Database Scripts

              Hi, thanks for (all) of your help.

              BTW, where is the DB-API docs for python?

              Thanks,
              --Chuck

              Comment

              • Simon Brunning

                #8
                Re: Python Database Scripts

                On 13 Sep 2005 11:32:05 -0700, Chuck <willardthompso n@gmail.com> wrote:[color=blue]
                > BTW, where is the DB-API docs for python?[/color]

                Google is your friend - <http://www.google.co.u k/search?q=DB-API>, 1st hit.

                --
                Cheers,
                Simon B,
                simon@brunningo nline.net,

                Comment

                Working...