Data bases 8th May, 2013
1 DBM databases 2 3
Types of data storage Trwałe słowniki Klient-serwer SQL Bekreley DB Gnu dbm (n)dbm Firebird Sqlite Oracle MySQL PostgeSQL DB/2
DBM databases Database manager Data is stored in file being a hashtable Values are accessed by keys Usually, value can be only of type string Implementation: standard hash tables and b-trees No server, all in one file Fast!!!
How to use DBM db[ jeden ] = one db[ dwa ] = two if trzy in db: del db[ trzy ]
How to use DBM import <modul> db = <modul>.open( storage.dbm, c ) db[ jeden ] = one db[ dwa ] = two if trzy in db: del db[ trzy ] db.close()
Available modules 2.* 3.* Description dumbdb dbm.dumb slow, but pure implementation bsddb (dbhash) implementation of Berkeley DB dbm dbm.ndbm API to U*X (n)dbm gdbm dbm.gnu extension of dbm
How to process entire collection for key in db.keys(): print db[key]
How to process entire collection for key in db.keys(): print db[key] Warning All file must be kept in memory.
Collection processing, dumbdbm for key in db: print db[, key, ] =, db[key] db.close()
Collection processing dbhash print db.first() while True: try: el = db.next() print el except KeyError: break gdbm k = db.firstkey() while k!= None: print k k = db.nextkey(k)
Available modules for in firstkey(), nextkey() first(), next() dumbdbm X (n)dbm gdbm X dbhash X
how to deal with this mess Use generic package anydbm
how to deal with this mess Use generic package anydbm Use package whichdbm: >>> import anydbm >>> whichdb.whichdb( plik.db )
Berkeley DB Who use Berkeley DB? OpenLDAP, Subversion, Spamassasin, KDevelop,... Features: transactions; replication; record locking; keys: positive integers.
How to store object? shelve package Shelve file is a dictionary, i.e. list of pairs (string, object)
shelve package Example import shelve db = shelve.open( dane.db ) db[ a_list ] = [2,3,5,7,11] db.sync() del db[ an_object ] db.close()
Database engines Oracle DB/2 MySQL PostgreSQL MSSQL...
DB API DBM databases Database API Specification A unified API (methods and fields) to different database engines. Current version: 2.0.
Opening connection to database connect( parameters ) # returns an object Connection
Opening connection to database connect( parameters ) MySQL # returns an object Connection import MySQLdb db = MySQLdb.connect(host= localhost, db= testing, user= user, passwd= 123456 )
Closing connection db.close()
Communication with database send a query wynik = db.cursor() wynik.execute( SELECT * FROM Students )
Communication with database send a query wynik = db.cursor() wynik.execute( SELECT * FROM Students ) Fetching a result row = wynik.fetchone() while row: print row row = wynik.fetchone() Optional wynik.close()
A result DBM databases Result attributes (an object of the class Cursor): description: describes columns rowcount: number of returned or processed (e.g. INSERT or UPDATE)rows...
DB API - additional information Standard exceptions: Warning, DatabaseError, NotSupportedError,...
SQLite DBM databases File database, no external server, no contact with admin; module: sqlite3 Implements DB API 2.0 with extensions
Sqlite usage Table creation import sqlite3 db = sqlite.connect( /tmp/temp.db ) kursor = db.cursor() kursor.execute( create table Library (Author text, Title text, publish_year int, price real) ) kursor.commit()
Sqlite usage Insert row into table kursor.execute( insert into Library values ( Shakespeare, Hamlet, 2003, 25.5) )
Sqlite usage Fetching data (extension of DB API 2.0) kursor.execute( SELECT * FROM Library ) for row in kursor: print row
Sqlite DBM databases Curiosity db = sqlite.connect( :memory: )