That said, if you can make some assumptions about what else is writing to the table then you can get fairly close. One technique I often use is like this:
-- Insert the row if none with the same PK exists.
-- As a single SQL statement, this executes atomically
-- in all but the most braindead DBMSes.
insert into mytable (keycol, datacol)
select 1, 'a'
where not exists (
where keycol = 1
-- OK, now there is definitely a row there with keycol=1.
-- Update the row which exists.
-- This is redundant work if we just inserted it above, but do it anyway.
set datacol = 'a'
where keycol = 1
This is clearly not safe in general. If there are others accessing the table and doing general delete, or test-and-set, it can lose data. However, if you know that code like the above is the only code accessing the table, or if you restrict it to the above plus simple inserts, then you have a safe way to insert-or-update a row that doesn't involve holding locks for a period of time and doesn't require an explicit transaction.
You logging into the voting machine is recorded when you vote. Information about who you voted for is not recorded.
And how can you be certain that when the votes are counted, your vote is being included? Having something recorded on the blockchain (or any other public record) doesn't help at all with that; you still have to trust the people counting the votes to count them fairly. Or if the votes are counted by machine, you have to trust those who built and operate the machine. If somebody queries the totals after the election, they can't use the blockchain records to check that the total is correct; conversely it's possible for a malicious actor to stuff ballots without ever recording it in the blockchain.
There are proposals for cryptographic voting systems that work properly. When you vote, your vote is recorded so that you can check that it was counted (and anyone can check that the count includes all votes cast); but you cannot find out or demonstrate what the vote was. This is an interesting area of research but it is a lot more complicated than just saying 'add something to the blockchain'. Doing that by itself adds nothing.
You can use blockchain to authenticate that a vote was cast without recording what that vote was.
Could you elaborate on how that would work exactly? It's not an easy problem to solve.
If you had bothered, you'd know that you can sign a message using your private key that proves indeed, that you did something on the blockchain, whether its voting or whatever.
Right. But how do you do that while ensuring a secret ballot? In other words so you *cannot* prove, whether you want to or not, who you voted for?
There are suggested protocols for having a verifiable yet secret ballot, but they are not completely straightforward. Unless the secret ballot part is sorted out properly, recording all votes in a public ledger (be it the blockchain or anything else) doesn't give a free and fair election.
A method of solution is perfect if we can forsee from the start, and even prove, that following that method we shall attain our aim. -- Leibnitz