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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
I knew it would be something small. :)
Thanks.
M.
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.
Hi,
I tried both of your suggestions, it makes no difference.
M.
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)