Let's go back to our single-table database of historical people.
>>> import sqlite3
>>> conn = sqlite3.connect("C:\\sqlite\\db\\history1.db")
>>> curs = conn.cursor()
>>> curs.execute("SELECT * FROM historicalfigures;")
<sqlite3.Cursor object at 0x039DD360>
>>> dumpa = curs.fetchall()
>>> for a in dumpa:
print(a)
('Leonardo da Vinci', 1452, 1519, 'Artist', 'Italy')
('Niccolo Machiavelli', 1469, 1527, 'Politician & Author', 'Italy (Florence)')
('Cardinal Thomas Wolsey', 1473, 1530, 'Churchman & Politician', 'England')
('William Caxton', 1422, 1491, 'Publisher', 'England')
('William Shakespeare', 1564, 1616, 'Author & Playwright', 'England')
('Francis Drake', 1540, 1596, 'Explorer', 'England')
('Henry VIII', 1491, 1547, 'Monarch', 'England')
('Elizabeth I', 1533, 1603, 'Monarch', 'England')
('Christopher Columbus', 1451, 1509, 'Explorer', 'Spain & Italy')
('Ferdinand Magellan', 1480, 1521, 'Explorer', 'Portugal')
>>>
So far so good. But I want to modify a datum - I realise that Francis Drake was a naval commander as well as an explorer (he led numerous naval attacks against the Spanish and helped defeat the Spanish Armarda). The general format for UPDATE is
UPDATE tablename SET update_list WHERE predicate;
That is just SQLite. To get Python to pass it to SQLite we use the cursor object like this:
>>> curs.execute("UPDATE historicalfigures SET Role = 'Naval Commander & Explorer' WHERE Name = 'Francis Drake';")
As before, I have used single quotes ('') within the SQLite command, and double quotes ("")for the Python command, with the results:
<sqlite3.Cursor object at 0x039DD360>
>>> curs.execute("SELECT * FROM historicalfigures WHERE Name = 'Francis Drake';")<sqlite3.Cursor object at 0x039DD360>
>>> curs.fetchall()
[('Francis Drake', 1540, 1596, 'Naval Commander & Explorer', 'England')]
>>>
I have found to my annoyance that it is very important to include the WHERE part of the statement to specify which entry (or entries) should be changed. I have made the stupid mistake of leaving that part out , then finding I have changed every record in the table - if you don't specify which records in the table should be changed by a command, SQLite assumes you mean all of them.
This is of vital importance when dealing with the next SQLite command - DELETE. This follows the format:
DELETE FROM tablename WHERE predicate;
Let's say we want to remove William Caxton from the table - we don't think he's historically significant enough (though he brought the first printing press to England). Again it is vital that we are careful about which records we delete.
>>> curs.execute("DELETE FROM historicalfigures WHERE Name = 'William Caxton';")
<sqlite3.Cursor object at 0x039DD360>
>>> curs.execute("SELECT * FROM historicalfigures;")
<sqlite3.Cursor object at 0x039DD360>
>>> dumpb = curs.fetchall()
>>> for b in dumpb:
print(b)
('Leonardo da Vinci', 1452, 1519, 'Artist', 'Italy')
('Niccolo Machiavelli', 1469, 1527, 'Politician & Author', 'Italy (Florence)')
('Cardinal Thomas Wolsey', 1473, 1530, 'Churchman & Politician', 'England')
('William Shakespeare', 1564, 1616, 'Author & Playwright', 'England')
('Francis Drake', 1540, 1596, 'Naval Commander & Explorer', 'England')
('Henry VIII', 1491, 1547, 'Monarch', 'England')
('Elizabeth I', 1533, 1603, 'Monarch', 'England')
('Christopher Columbus', 1451, 1509, 'Explorer', 'Spain & Italy')
('Ferdinand Magellan', 1480, 1521, 'Explorer', 'Portugal')
>>>
And we can see that William Caxton has been removed (and only that record...).
No comments:
Post a Comment