• No results found

Python. Data bases. Marcin Młotkowski. 8th May, 2013

N/A
N/A
Protected

Academic year: 2021

Share "Python. Data bases. Marcin Młotkowski. 8th May, 2013"

Copied!
31
0
0

Loading.... (view fulltext now)

Full text

(1)

Data bases

Marcin Młotkowski

(2)

DBM databases Object persistence SQL data bases and data engines

1 DBM databases

2 Object persistence

3 SQL data bases and data engines

(3)

Types of data storage

Bekreley DB (n)dbm Gnu dbm Trwałe słowniki Oracle DB/2 MySQL PostgeSQL Firebird Sqlite Klient-serwer SQL

(4)

DBM databases

Object persistence SQL data bases and data engines

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!!!

(5)

DBM databases

Object persistence SQL data bases and data engines

How to use DBM

db =<modul>.open(’storage.dbm’, ’c’) db[’jeden’] = ’one’ db[’dwa’] = ’two’ if ’trzy’in db: del db[’trzy’] db.close()

(6)

DBM databases

Object persistence SQL data bases and data engines

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()

(7)

Available modules

Python 2.* Python 3.* Description

dumbdb dbm.dumb slow, but pure Python implementation

bsddb (dbhash) — implementation of Berkeley DB

dbm dbm.ndbm API to U*X (n)dbm

(8)

DBM databases

Object persistence SQL data bases and data engines

How to process entire collection

forkey in db.keys():

print db[key]

Warning

All file must be kept in memory.

(9)

How to process entire collection

forkey in db.keys():

print db[key] Warning

(10)

DBM databases

Object persistence SQL data bases and data engines

Collection processing, dumbdbm

forkey in db:

print ’db[’, key, ’] =’, db[key] db.close()

(11)

Collection processing

dbhash printdb.first() whileTrue: try: el = db.next() print el except KeyError: break gdbm k = db.firstkey() whilek != None: print k k = db.nextkey(k)

(12)

DBM databases

Object persistence SQL data bases and data engines

Available modules

for–in firstkey(), nextkey() first(), next()

dumbdbm X — —

(n)dbm — — —

gdbm — X —

dbhash — — X

(13)

DBM databases

Object persistence SQL data bases and data engines

how to deal with this mess

Use generic packageanydbm

>>>importanydbm

(14)

DBM databases

Object persistence SQL data bases and data engines

how to deal with this mess

Use generic packageanydbm

Use packagewhichdbm:

>>>importanydbm

>>>whichdb.whichdb(’plik.db’)

(15)

Berkeley DB

Who use Berkeley DB?

OpenLDAP, Subversion, Spamassasin, KDevelop, ... Features:

transactions; replication; record locking;

(16)

DBM databases

Object persistence

SQL data bases and data engines

How to store object?

shelvepackage

Shelve file is a dictionary, i.e. list of pairs(string, object)

(17)

shelve package

Example importshelve db = shelve.open(’dane.db’) db[’a_list’] = [2,3,5,7,11] db.sync() deldb[’an_object’] db.close()

(18)

DBM databases Object persistence

SQL data bases and data engines

Database engines

Oracle DB/2 MySQL PostgreSQL MSSQL ...

(19)

DB API

Python Database API Specification

A unified API (methods and fields) to different database engines. Current version: 2.0.

(20)

DBM databases Object persistence

SQL data bases and data engines

Opening connection to database

connect(’parameters’) # returns an object Connection

MySQL importMySQLdb db = MySQLdb.connect(host=’localhost’, db=’testing’, user=’user’, passwd=’123456’)

(21)

Opening connection to database

connect(’parameters’) # returns an object Connection

MySQL importMySQLdb db = MySQLdb.connect(host=’localhost’, db=’testing’, user=’user’, passwd=’123456’)

(22)

DBM databases Object persistence

SQL data bases and data engines

Closing connection

db.close()

(23)

DBM databases Object persistence

SQL data bases and data engines

Communication with database

send a query

wynik = db.cursor()

wynik.execute(’SELECT * FROM Students’)

row = wynik.fetchone() whilerow: print row row = wynik.fetchone() Optional wynik.close()

(24)

DBM databases Object persistence

SQL data bases and data engines

Communication with database

send a query

wynik = db.cursor()

wynik.execute(’SELECT * FROM Students’) Fetching a result row = wynik.fetchone() whilerow: print row row = wynik.fetchone() Optional wynik.close()

(25)

A result

Result attributes (an object of the class Cursor): description: describes columns

rowcount: number of returned or processed (e.g. INSERT or UPDATE)rows

(26)

DBM databases Object persistence

SQL data bases and data engines

DB API - additional information

Standard exceptions:

Warning, DatabaseError, NotSupportedError, ...

(27)

SQLite

’File’ database, no external server, no contact with admin; module: sqlite3

(28)

DBM databases Object persistence

SQL data bases and data engines

Sqlite usage

Table creation

importsqlite3

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()

(29)

Sqlite usage

Insert row into table

kursor.execute(” ’insert into Library values (’Shakespeare’, ’Hamlet’, 2003, 25.5)” ’)

(30)

DBM databases Object persistence

SQL data bases and data engines

Sqlite usage

Fetching data (extension of DB API 2.0) kursor.execute(’SELECT * FROM Library’)

forrow in kursor:

print row

(31)

Sqlite

Curiosity

Figure

Table creation import sqlite3

References

Related documents

GlaxoSmithKline broke the law by promoting Zofran off-label and settled the criminal accusations with the United States (albeit without an express statement of

But Candidate 0* is just an elaboration of Candidate 2, which states that in each ordinary retrieval case, the relevant process that forms the subject’s justified belief

In the framework of the multifractal energy cascades, the wind velocity and power output time series possess scaling fluctuations since both spectra follow a -5/3 power law for

This device is designed for use where high linearity is required and features high OIP3 and P1 with low consumption current (135mA) and requires a few external match- ing

The data is based on a measurement bandwidth of 10 Hz (filter type: normal), step attenuation of 0 dB, nominal source power of 0 dBm and system error calibration by means of

Sensitivity for initial SC detection high speed/high dynamic range –119 dBm/–127 dBm Sensitivity after successful SC detection high speed/high dynamic range

Oracle DB SQLiteAccess (CORAL Plugin) SQLite C API MySQLAccess (CORAL Plugin) MySQL C API MySQL DB SQLite DB (file) OCI OCI FrontierAccess (CORAL Plugin) Frontier API CoralAccess

The Mental Health Association in New Jersey is dedicated to promoting good mental health for all New Jerseyans, reducing the barriers that prevent to many people from seeking