Till KTH:s startsida Till KTH:s startsida

Advanced assignment

Interfacing to SQLite from Python

Completing this assignment will raise your grade one step.

The purpose of this assignment is to practice using an SQL database from Python. By mastering this, which is not very hard, you have learned a great way of easily saving and accessing data. Moreover, you have immediate access to very advanced data manipulation.

Your task is to solve the exercises in "Your own database" using a Python program instead of using pure SQL interactively.

Use the standard Python module sqlite3 and write a program that, when called from the command line, solves the tasks. The output from the program should be clearly printed and tabulated to stdout. Simply "echoing" the result from a select is not sufficient; you must show that you can extract columns of data from a SELECT command to be ready to process it in some way.

To present:

  1. Your Python program, code and test runs
  2. How do you extract the data from the results of SELECT commands?
  3. What happens when you insert Sus scrofa a second time?

Lars Arvestad skapade sidan 27 oktober 2016

En användare har tagit bort sin kommentar
kommenterade 13 december 2016

I am unsure of how to load the data into my database form python. In other words, I don' t know how to write this line: sqlite> .read /afs/nada.kth.se/info/appbio10/data/protdb.sqlite3 using Python.

Lärare kommenterade 13 december 2016

The 'connect' function in Python's sqlite module will work if you have created the database with something like:

sqlite my_db <  /afs/nada.kth.se/info/appbio10/data/protdb.sqlite3

Hope this helps!
kommenterade 13 december 2016

Thanks