Skip to content

Commit 35fce5c

Browse files
author
Sebastian Raschka
committed
Update update_db.py
1 parent 3139a22 commit 35fce5c

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

sqlite3/update_db.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,23 @@
77
conn = sqlite3.connect('zinc_db1.db')
88
c = conn.cursor()
99

10-
# update field
10+
# update field (no insert if id doesn't exist)
1111
t = ('NO', 'ZINC00895033', )
1212
c.execute("UPDATE zinc_db1 SET purchasable=? WHERE zinc_id=?", t)
1313
print "Total number of rows changed:", conn.total_changes
1414

15+
16+
# update, or insert when id does not exist
17+
# here: updates rotatable bonds if record with primary key zinc_id exists,<br>
18+
# else inserts new record an sets purchasable to 0
19+
c.execute("""INSERT OR REPLACE INTO zinc_db1 (zinc_id, rotatable_bonds, purchasable)
20+
VALUES ( 'ZINC123456798',
21+
3,
22+
COALESCE((SELECT purchasable from zinc_db1 WHERE zinc_id = 'ZINC123456798'), 0)
23+
)"""
24+
25+
26+
1527
# delete rows
1628
t = ('NO', )
1729
c.execute("DELETE FROM zinc_db1 WHERE purchasable=?", t)

0 commit comments

Comments
 (0)