Using the framework for testing we created in earlier articles,
let’s try to modify some data. We are writing a small program
that increments a counter. Our table looks like this, and
contains 10 counters:
CREATE TABLE `demo` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`counter` int NOT NULL DEFAULT '0',
UNIQUE KEY `id` (`id`)
)
INSERT INTO `demo` VALUES (1,0);
INSERT INTO `demo` VALUES (2,0);
...
INSERT INTO `demo` VALUES (10,0);
We are using some very simple programming to increment a counter:
@sql.command()
@click.option("--name", default="demo", help="Table name to count in")
@click.option("--id", default=0, help="Counter to use")
@click.option("--count", default=1000, help="Number of increments")
def count(name, id, count):
""" Increment counter --id by --count many steps in table --name """
for i in range(0, count):
cmd = f"update {name} set counter=counter+1 where id = {id}"
c = …
[Read more]