How to update entire column with values in list us

2019-02-15 11:41发布

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.

1条回答
Rolldiameter
2楼-- · 2019-02-15 12:14

Use the .executemany() method instead:

curr.executemany('UPDATE test SET myCol= ?', myList)

However, myList must be a sequence of tuples here. If it is not, a generator expression will do to create these tuples:

curr.executemany('UPDATE test SET myCol= ?', ((val,) for val in myList))

Demonstration (with insertions):

>>> import sqlite3
>>> conn=sqlite3.connect(':memory:')
>>> conn.execute('CREATE TABLE test (myCol)')
<sqlite3.Cursor object at 0x10542f1f0>
>>> conn.commit()
>>> myList = ('foo', 'bar', 'spam')
>>> conn.executemany('INSERT into test values (?)', ((val,) for val in myList))
<sqlite3.Cursor object at 0x10542f180>
>>> list(conn.execute('select * from test'))
[(u'foo',), (u'bar',), (u'spam',)]

Note that for an UPDATE statement, you have to have a WHERE statement to identify what row to update. An UPDATE without a WHERE 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:

my_data = ({id=1, value='foo'}, {id=2, value='bar'})
cursor.executemany('UPDATE test SET myCol=:value WHERE rowId=:id', my_data)
查看更多
登录 后发表回答