Home:ALL Converter>Insert values into SQL column with mysql-python

Insert values into SQL column with mysql-python

Ask Time:2014-11-25T00:42:59         Author:edesz

Json Formatter

I am trying to insert values into a column of a SQL table, using MySQLdb in Python 2.7. I am having problems with the command to insert a list into 1 column.

I have a simple table called 'name' as shown below:

+--------+-----------+----------+--------+
| nameid | firstname | lastname | TopAdd |
+--------+-----------+----------+--------+
| 1      | Cookie    | Monster  |        |
| 2      | Guy       | Smiley   |        |
| 3      | Big       | Bird     |        |
| 4      | Oscar     | Grouch   |        |
| 5      | Alastair  | Cookie   |        |
+--------+-----------+----------+--------+

Here is how I created the table:

CREATE TABLE `name` (
  `nameid` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(45) DEFAULT NULL,
  `lastname` varchar(45) DEFAULT NULL,
  `TopAdd` varchar(40) NOT NULL,
  PRIMARY KEY (`nameid`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8

Here is how I populated the table:

INSERT INTO `test`.`name`
(`firstname`,`lastname`)
VALUES
("Cookie","Monster"),
("Guy","Smiley"),
("Big","Bird"),
("Oscar","Grouch"),
("Alastair","Cookie");

DISCLAIMER: The original source for the above MySQL example is here.

Here is how I created the a new column named TopAdd:

ALTER TABLE name ADD TopAdd VARCHAR(40) NOT NULL

I now have a list of 5 values that I would like to insert into the column TopAdd as the values of that column. Here is the list.

vals_list = ['aa','bb','cc','dd','ee']

Here is what I have tried (UPDATE statement inside loop):

vals = tuple(vals_list)
for self.ijk in range (0,len(self.vals)):
            self.cursor.execute ("UPDATE name SET TopAdd = %s WHERE 'nameid' = %s" % (self.vals[self.ijk],self.ijk+1))

I get the following error message:

Traceback (most recent call last):
  File "C:\Python27\mySQLdbClass.py", line 70, in <module>
    [Finished in 0.2s with exit code 1]main()
  File "C:\Python27\mySQLdbClass.py", line 66, in main
    db.mysqlconnect()
  File "C:\Python27\mySQLdbClass.py", line 22, in mysqlconnect
    self.cursor.execute ("UPDATE name SET TopAdd = %s WHERE 'nameid' = %s" % (self.vals[self.ijk],self.ijk+1))
  File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1054, "Unknown column 'aa' in 'field list'")

Is there a way to insert these values into the column with a loop or directly as a list?

Author:edesz,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/27109753/insert-values-into-sql-column-with-mysql-python
Amy Roy :

Try This:\n\nvals_list = ['aa','bb','cc','dd','ee']\n\n\nfor i, j in enumerate(vals_list):\n\n self.cursor.execute((\"UPDATE test.name SET TopAdd = '%s' WHERE nameid = %s\" % (str(j),int(i+1))\n",
2014-11-24T17:17:54
yy