Menu

Problem with insert

Help
Mahesh
2006-03-01
2012-09-19
  • Mahesh

    Mahesh - 2006-03-01

    Hi,

    I am using MySQLdb and I have a problem with executing insert statements. I use the following code:

    db = MySQLdb.connect(host=url, user=username, passwd=password, db='temp')

    c = db.cursor()

    c.execute('INSERT INTO filelist VALUES (%s %s %s)', (fileName, annotation, username,))

    where fileName, annotation and username are variables containing strings.

    When I run the program, I get a MySQL error that says that the column count does not match value count. However, I am able to use the insert statement when I use the MySQL query browser. I have checked for any spelling and syntax errors. Can anyone help me spot the problem in my code?

    Thanks.

    M.

     
    • Mahesh

      Mahesh - 2006-03-01

      I knew it would be something small. :)

      Thanks.

      M.

       
    • Brett Powley

      Brett Powley - 2006-03-01

      INSERT INTO filelist VALUES (%s %s %s)', (fileName, annotation, username,)

      Two things:

      (1) There's an extra comma after 'username' which might be causing issues

      (2) Try

      c.execute('INSERT INTO filelist(filename, annotation, username) VALUES(%s, %s, %s)', (fileName, annotation, username))

      assuming 'filename', 'annotation' 'username' are the names of the columns in your table.

       
    • Mahesh

      Mahesh - 2006-03-01

      Hi,

      I tried both of your suggestions, it makes no difference.

      M.

       
      • Brett Powley

        Brett Powley - 2006-03-01

        The problem is you're missing commas:

        c.execute('INSERT INTO filelist VALUES (%s %s %s)', (fileName, annotation, username,))

        that needs to be
        VALUES(%s, %s, %s)

         

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.