Adding column in SQLite3, then filling it
I'm using SQLite 3.6, and connecting to it using Python 2.7 on Fedora 14.
I am attempting to add a column to a table using ALTER TABLE, then immediately afterwards UPDATE the table with data for the newly created column. Through python, I get nothing but NULLs in the database's new column. If I run the queries through sqlite3 in the terminal, it works.
Here is the--sanitized--python
def upgrade(cursor)
cursor.execute("ALTER TABLE Test ADD COLUMN 'Guid' TEXT")
cursor.execute("SELECT DISTINCT Name FROM Test WHERE ForeignKey开发者_运维问答=-1")
# Loop using a row factory that puts all the Names into a list called NameList
for Name in NameList:
Guid = uuid.uuid4()
cursor.execute("UPDATE Test SET Guid=? WHERE Name=?", (str(Guid),Name))
The SQLite3 connection is managed by the python's main function, and the connection is commited when the python script ends. The python executes without errors and debug statements show that all of the proper rows were found in the select call. However, when I look at the database using Sqliteman or sqlite3, I see only NULLs in the new column.
Here are my sqlite3 calls.
ALTER TABLE Test ADD COLUMN 'Guid' TEXT;
UPDATE Test SET GUID="foo" WHERE Name="Test3";
select Name, Guid from Test where Name='Test3';
This works for some reason. I see the--fake--guid where I expect.
I'm at my wits end for what to do.
The issue was in the main function, which exited before the connection's commit() call could be made due to an error in the main function.
精彩评论