simple client data base

Oscar Benjamin oscar.j.benjamin at gmail.com
Mon Sep 3 11:32:19 EDT 2012


On 3 September 2012 15:12, Mark R Rivet <markrrivet at aol.com> wrote:

> Hello all, I am learning to program in python. I have a need to make a
> program that can store, retrieve, add, and delete client data such as
> name, address, social, telephone number and similar information. This
> would be a small client database for my wife who has a home accounting
> business.
>

I would use the sqlite3 module for this (if I wasn't using gmail contacts).


> I have been reading about lists, tuples, and dictionary data
> structures in python and I am confused as to which would be more
> appropriate for a simple database.
>

As already said by Chris these are the types that Python uses to represent
data in memory, rather than on disk. There are a number of ways that you
can use these to represent the information from your database. For example,
you could use a dict of dicts:

>>> contact_db = {} # empty dict
>>> contact_db['john'] = {'alias':'john', 'name':'John Doe', 'email': '
john at example.com'}
>>> contact_db['dave'] = {'alias':'dave', 'name':'Dave Doe', 'email': '
dave at example.com'}
>>> contact_db
{'dave': {'alias': 'dave', 'name': 'Dave Doe', 'email': 'dave at example.com'},
'john': {'alias': 'john', 'name': 'John Doe', 'email': 'john at example.com'}}
>>> contact_db['dave']
{'alias': 'dave', 'name': 'Dave Doe', 'email': 'dave at example.com'}
>>> contact_db['dave']['email']
'dave at example.com'

I know that python has real database capabilities but I'm not there
> yet and would like to proceed with as simple a structure as possible.
>

If you don't want to use real database capabilities you could save the data
above into a csv file using the csv module:

>>> import csv
>>> with open('contacts.csv', 'wb') as f:
...   writer = csv.DictWriter(f)
...   writer.writelines(contact_db.values())

You can then reload the data with:

>>> with open('contacts.csv', 'rb') as f:
...   reader = csv.DictReader(f, ['alias', 'name', 'email'])
...   new_contact_db = {}
...   for row in reader:
...     new_contact_db[row['alias']] = row
...
>>> new_contact_db
{'dave': {'alias': 'dave', 'name': 'Dave Doe', 'email': 'dave at example.com'},
'john': {'alias': 'john', 'name': 'John Doe', 'email': 'john at example.com'}}
>>> contact_db == new_contact_db
True


>
> Can anyone give me some idea's or tell me which structure would be
> best to use?
>

The above method for storing the data on disk is simple but not very safe.
If you use it for your wife's business make sure that you are always
keeping backups of the file. Preferably don't overwrite the file directly
but write the data out to a separate file first and then rename the file
(to avoid loss of data if the program has an error while writing).

The obvious way to improve on the above is to use the sqlite3 module to
store the data in an sqlite3 file instead of a csv file. There is one
advantage to using the above over using an sqlite3 database which is that
the data can be edited manually as a text file or using standard
spreadsheet software if necessary.

Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120903/a1459b15/attachment.html>


More information about the Python-list mailing list