The tool which seems most suitable to me is SQLite - it essentially offers all (most) of the features of a server-based database, but just works on a file like you have - doing all the locking and concurrency you need, on Unix/Linux or Windows.
The overhead for you will be:
And the SQL isn't hard, and you can play at the sqlite prompt:
There is a little trickery in the update command. Rather than read the info in one SQL SELECT and then store it as a seperate UPDATE, I did the read+update in one statement.$ sqlite3 foo.db SQLite version 3.2.8 Enter ".help" for instructions sqlite> create table players (id integer, name varchar(256), score int +eger); sqlite> insert into players (id, name, score) values (1, "bob", 100); sqlite> insert into players (id, name, score) values (2, "sally", 200) +; sqlite> insert into players (id, name, score) values (3, "frank", 10); sqlite> select * from players; 1|bob|100 2|sally|200 3|frank|10 sqlite> select score from players where name='sally'; 200 sqlite> update players set score=score+10 where name='sally'; sqlite> select score from players where name='sally'; 210 sqlite> delete from players where name='bob'; sqlite> select * from players; 2|sally|210 3|frank|10 sqlite>
This is because another process could have got in and altered the score between my read and my update. There is no chance of that happening if done with one statement.
If you want to get deeper into SQL you can get around this with transactions and/or locks, but the above is probably be all you need.
You can get very complicated with SQL if you need/want to, and in fact I've stuck in a (redundant?) id number in there out of habit - which I could have told the system was an indexed, unique key. But all of that stuff is really for bigger systems where you're giving hints to help performance and similar. If you're currently using a flat file you're probably not near worrying about that yet.
In reply to Re: Best practices for modifying a file in place: q's about opening files, file locking, and using the rename function
by jbert
in thread Best practices for modifying a file in place: q's about opening files, file locking, and using the rename function
by davebaker
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |