I suggest that you look at the
existing plugins and find one or two that do something similar to what you need. You can then download, unzip, and examine the python code to see how they work.
Also check out documentation like the
Plugin API and
Database API.