# Karrigell/Managing The Database
## Database engine

Karrigell can use any database engine (MySQL, SQLite, etc) using the appropriate module. For this example we will use a very simple module that stores the data we need : for each CD in the collection, the artist name and the title of the album
Copy this in a module called simpledb.py :
When you import the module in a script you simply use its functions read and save. The information is stored in a list of tuples
For our CD collection application, the information will consist in only 2 values : the artist name and the CD title. If we call the database file mycds.db the code to print all the CDs in an HTML table will simply be :
Notice the way you import the module simpledb.py : you can't use the usual import statement, because in a shared environment like a web server, there are issues with the resolution of module names to files in the file system. Karrigell uses a built-in function Import(module_url) for user-defined modules ; for the modules in the standard Python distribution, you can safely use import

## Home page with the CD list

We can now modify the index function to print the list of CDs:
Notice that simpledb is imported at the module level, not inside the function index : the names defined at module level are available in the functions, exactly like in ordinary Python scripts

## Adding new CDs

For logged-in users there is a link to enter new CDs in the database. The href attribute of this link is new_cd, so we must write a function new_cd
This function will print a form to enter the CD artist and title, and submit the data to another function which will actually write the information in the database, then return to the home page
At this stage you shouldn't have problems to understand the code below :
Add these functions to index.ks and enter a couple of CDs in the database. Each time you are back to the home page you should see the collection growing

## Editing records

Logged-in users should be able to edit the information about a CD : on the list they should see a link "Edit" sending them to a page where the information will be available for editing
This requires that the information about the CD is carried from the home page to the edition page. For this we could append the information about the CD (artist + title) to the link, for instance : href = edit?artist=Beatles&title=Revolver ; but database engines usually provide record identifiers, that is, integers which identify the record
In our simple database we can use the index of the item in the list as identifier, so that we can rewrite the code to print the CD collection this way :
The function edit will receive one argument called num. All arguments passed to functions in Karrigell Services are bytestrings, so before using this argument num as the index in a list don't forget to convert it to an integer

## Removing records

The final step is to enable deletion of CDs from the base : once again, edit the function index like this
and add a new function, remove:

## Summary

We now have a complete application to manage our CD collection. The structure of the application in the script index.ks is as clear as in any Python module :
Each function matches a page of the application. For the pages that receive user input, the fields are the arguments to the function (always as strings)
Throughout this short presentation you see that all you need to know is Python and HTML. The package doesn't require any configuration, and for programmers Karrigell only provides a small set of additional built-in names