I have a dataset stored locally in an sqlite3 database. I extracted a column, performed some operations and now want to replace ALL of the values in the database column. How can I do this?
The length of the column and the list are guaranteed to be the same length. I simply want to update the table with the new values. Is there an easy way to do this all at once?
Using python 2.7
Edited to add:
myList is a pandas series backed by a numpy array of dtype 'object'. The table column, myCol is text formatted.
In [1]: curr.execute('UPDATE test SET myCol= ?', myList)
---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)
f:\python\<ipython-input-52-ea41c426502a> in <module>()
----> 1 curr.execute('UPDATE test SET myCol = ?', myList)
ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 401125 supplied.
Use the
.executemany()
method instead:However,
myList
must be a sequence of tuples here. If it is not, a generator expression will do to create these tuples:Demonstration (with insertions):
Note that for an
UPDATE
statement, you have to have aWHERE
statement to identify what row to update. AnUPDATE
without aWHERE
filter will update all rows.Make sure your data has a row identifier to go with it; in this case it may be easier to use named parameters instead: