Menu

Insert not getting saved in Windows

Help
venks100
2005-08-19
2012-09-19
  • venks100

    venks100 - 2005-08-19

    I am using Python 2.41 with mysql 4.1 & MySQL-python.exe-.2.0.win32-py2.4 versions
    I am having the following code & am using Commit also


    import MySQLdb

    connection = MySQLdb.connect(host='localhost', db='CNS' ,user= 'root',passwd='')

    cursor = connection.cursor()

    cursor.execute("INSERT INTO rjhtest2 (name,age) SELECT Name ,age from rjhtest3 ");

    connection.commit
    cursor.close
    connection.close


    Data is not getting saved.This code works fine when i use it with SQLyog(a mySQL GUI)
    I found that even when I use a normal insert statment ( ie with out Select statemnt)it is not getting saved
    Is this a bug or is it that i am not using the correct libraries
    I also want to know what is the correct code if i am using tables from two different databases(since two different cursor will be used )
    Venkatesh

     
    • venks100

      venks100 - 2005-08-20

      Real thanks for pointing it out.I was using all functions like this eg cursor.fetchall etc .Though there wer no compiling errors data was not getting saved
      You saved me a lot of time by pointing it out.Thanks once again
      Venkatesh

       
      • Andy Dustman

        Andy Dustman - 2005-08-20

        Right. Leaving the parenthesis off is not a syntax error; it is merely a reference. For example:

        row = cursor.fetchone

        doesn't actually fetch anything. It simply makes row be a reference to the fetchone method on cursor. You could subsequently do this:

        data = row()

        which is equilvalent to:

        data = cursor.fetchone()

        Simply doing:

        cursor.fetchone

        gets a reference to the bound fetchone method, which immediately goes away, since it doesn't get assigned to anything.

        The Pythonism that applies here (I think) is: Explicit is better than implicit.

         
    • venks100

      venks100 - 2005-08-19

      I found the answer from other threads
      If i do connection.autocommit after
      connection = MySQLdb.connect(host='localhost', db='CNS' ,user= 'root',passwd='')
      The data is getting updated correctly
      Venkatesh

       
    • Andy Dustman

      Andy Dustman - 2005-08-19

      Oh, you've managed to screw it up in a new and interesting way I haven't seen:

      connection.commit

      is only a reference to the commit method; it does not cause it to be called. You need:

      connection.commit()
      cursor.close()
      connection.close()

      This is not perl, after all.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.