mirror of
https://github.com/rasbt/python_reference.git
synced 2025-04-23 13:17:35 +00:00
Update update_db.py
This commit is contained in:
parent
3139a22f38
commit
35fce5c502
@ -7,11 +7,23 @@ import sqlite3
|
|||||||
conn = sqlite3.connect('zinc_db1.db')
|
conn = sqlite3.connect('zinc_db1.db')
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
|
|
||||||
# update field
|
# update field (no insert if id doesn't exist)
|
||||||
t = ('NO', 'ZINC00895033', )
|
t = ('NO', 'ZINC00895033', )
|
||||||
c.execute("UPDATE zinc_db1 SET purchasable=? WHERE zinc_id=?", t)
|
c.execute("UPDATE zinc_db1 SET purchasable=? WHERE zinc_id=?", t)
|
||||||
print "Total number of rows changed:", conn.total_changes
|
print "Total number of rows changed:", conn.total_changes
|
||||||
|
|
||||||
|
|
||||||
|
# update, or insert when id does not exist
|
||||||
|
# here: updates rotatable bonds if record with primary key zinc_id exists,<br>
|
||||||
|
# else inserts new record an sets purchasable to 0
|
||||||
|
c.execute("""INSERT OR REPLACE INTO zinc_db1 (zinc_id, rotatable_bonds, purchasable)
|
||||||
|
VALUES ( 'ZINC123456798',
|
||||||
|
3,
|
||||||
|
COALESCE((SELECT purchasable from zinc_db1 WHERE zinc_id = 'ZINC123456798'), 0)
|
||||||
|
)"""
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# delete rows
|
# delete rows
|
||||||
t = ('NO', )
|
t = ('NO', )
|
||||||
c.execute("DELETE FROM zinc_db1 WHERE purchasable=?", t)
|
c.execute("DELETE FROM zinc_db1 WHERE purchasable=?", t)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user