MySQLdb trouble

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nicolay A. Vasiliev

    #1

    MySQLdb trouble

    Hello there!

    I got some trouble trying to insert data into the database with MySQLdb
    module.

    I have such code:

    from MySQLdb import *

    def loc_connect_db( ):
    """
    The DB connection subroutine
    """
    db = MySQLdb.connect (host = "localhost" ,
    user = "root",
    passwd="mysql",
    db="some_db")
    return db

    db = loc_connect_db( )
    cursor = db.cursor()

    query = """insert ignore into categories (cat_id, cat_name, parent)
    values (%d, "%s", %d)""" % (int(CatId), CatName.capital ize(), int(parent))
    print query
    cursor.execute( query)

    Queries are formed in the loop, they look fine. No errors, but no
    effect, records are not inserted into the table. There is my possible
    mistake?

    Any suggestions are very appreciated - Thanks in advance.

    Best regards,
    Nicolay
  • John Salerno

    #2
    Re: MySQLdb trouble

    Nicolay A. Vasiliev wrote:
    [color=blue]
    > def loc_connect_db( ):
    > """
    > The DB connection subroutine
    > """
    > db = MySQLdb.connect (host = "localhost" ,
    > user = "root",
    > passwd="mysql",
    > db="some_db")
    > return db[/color]

    Hi. Sorry I can't help, but I'm interested in learning about mysqldb and
    I was wondering why you chose to wrap the 'connect' function inside your
    own function. Does this accomplish something that I'm not seeing?
    Couldn't you just use the body of the loc_connect_db( ) function as your
    actual code in order to get 'db'?

    Thanks.

    Comment

    • Thomas Bartkus

      #3
      Re: MySQLdb trouble

      "John Salerno" <johnjsal@NOSPA Mgmail.com> wrote in message
      news:Xnm8g.2076 $No6.45677@news .tufts.edu...[color=blue]
      > Nicolay A. Vasiliev wrote:
      >[color=green]
      > > def loc_connect_db( ):
      > > """
      > > The DB connection subroutine
      > > """
      > > db = MySQLdb.connect (host = "localhost" ,
      > > user = "root",
      > > passwd="mysql",
      > > db="some_db")
      > > return db[/color]
      >
      > Hi. Sorry I can't help, but I'm interested in learning about mysqldb and
      > I was wondering why you chose to wrap the 'connect' function inside your
      > own function.[/color]
      [color=blue]
      > Does this accomplish something that I'm not seeing?[/color]

      Probably!

      1) His code body will be less likely to cause migrane headaches when he
      tries to read and interpret what he did a year from now. If you are trying
      to figure out what is going on with the logic, user names and passwords can
      be so much chaff your brain needs to wade through in order to get to the
      central idea.

      2) The coder won't have to repeat himself if he needs to re-open the
      databases.
      He can just call his less comples loc_connect_db( ) function.

      And know that #1 is a sufficient reason even if #2 doesn't apply!
      [color=blue]
      > Couldn't you just use the body of the loc_connect_db( ) function as your
      > actual code in order to get 'db'?[/color]

      Yes you could!
      It's really a matter of style and preference. Some programmers (myself
      included!) prefer many, very short and simple functions over fewer function
      with longer blocks of more complex code.

      It's hard to make a mistake by having too many short and simple functions.

      And much too easy to make them when you have too few ;-)
      Thomas Bartkus

      ..


      Comment

      • John Salerno

        #4
        Re: MySQLdb trouble

        Thomas Bartkus wrote:
        [color=blue]
        > 1) His code body will be less likely to cause migrane headaches when he
        > tries to read and interpret what he did a year from now. If you are trying
        > to figure out what is going on with the logic, user names and passwords can
        > be so much chaff your brain needs to wade through in order to get to the
        > central idea.
        >
        > 2) The coder won't have to repeat himself if he needs to re-open the
        > databases.[/color]

        Ah, that makes sense! It seemed like an unnecessary step, but in some
        ways it might end up being more efficient.

        Comment

        • Thomas Bartkus

          #5
          Re: MySQLdb trouble


          "John Salerno" <johnjsal@NOSPA Mgmail.com> wrote in message
          news:U_n8g.2079 $No6.45875@news .tufts.edu...[color=blue]
          > Thomas Bartkus wrote:
          >[color=green]
          > > 1) His code body will be less likely to cause migrane headaches when he
          > > tries to read and interpret what he did a year from now. If you are[/color][/color]
          trying[color=blue][color=green]
          > > to figure out what is going on with the logic, user names and passwords[/color][/color]
          can[color=blue][color=green]
          > > be so much chaff your brain needs to wade through in order to get to the
          > > central idea.
          > >
          > > 2) The coder won't have to repeat himself if he needs to re-open the
          > > databases.[/color]
          >
          > Ah, that makes sense! It seemed like an unnecessary step, but in some
          > ways it might end up being more efficient.[/color]

          For the machine - it *is* an unnecessary step.
          For the human being who must write and maintain code, it is quite a useful
          step.

          And a very excellent coding habit to get into ;-)
          Thomas Bartkus


          Comment

          • Nicolay A. Vasiliev

            #6
            Re: MySQLdb trouble

            Hi!
            [color=blue]
            > Number two suggestion: try committing the transaction (autocommit
            > might not be enabled in your environment).
            >[/color]

            commit() - is what I need :)

            Thank you very much!


            Comment

            Working...