Python och Perl - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Python och Perl

Description:

(DBMS Database management system, e.g. MySQL, Oracle, Access) We have extracted a huge data amounts from flat files (databanks) That takes a lot of time. ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 8
Provided by: andersb6
Category:
Tags: limbo | och | perl | python

less

Transcript and Presenter's Notes

Title: Python och Perl


1
Python och Perl
  • Lecture 3
  • API and DBMS-connections.

2
Why use a DBMS?(DBMS Database management
system, e.g. MySQL, Oracle, Access)
  • We have extracted a huge data amounts from flat
    files (databanks)
  • That takes a lot of time.
  • Same information and repeated queries.
  • Solution
  • Store data in a DBMS
  • only needs to extract data once.
  • only needs to read in the data once.
  • We can put an index on any values we want
  • (compare Hash)

3
Requirements for Python/Perl and DBMS hook up
  • A database server (e.g. MySQL-server)
  • A client for interaction with the database
    server (e.g. mysql-client)
  • An API (Application Programming Interface)
  • Perl uses DBI (database independent interface)
  • Python uses the module MySQLdb
  • Driver
  • Perl uses mysql
  • Python MySQLdb module has a built in driver.

4
Overview of system
Python script, using API. import MySQLdb
MySQL-server, with databases
API
Python-MySQLdb is a module for MySQL which uses
the API-syntax standard.
Perl-DBI is a generic API and requires a
complementing driver for every type of database
server.
5
How to apply
  • Connect , connect()
  • Prepare generisc query, prepare()
  • Execute query with specific values, execute()
  • Retrieve result, fetch()
  • It is possible to iterate over both 4. and 3.

6
Example - Perl
  • use DBI
  • ( user, password) ( "student, "student)
  • database "biolabdb"
  • driver "mysql"
  • data_source "DBIdriverdatabase"
  • data_source ska ha 3 värden, moduldrivrutindat
    abas
  • step 1.
  • dbh DBI-gtconnect( data_source, user,
    password )
  • step 2.
  • statement "SELECT ID FROM keywords WHERE
    keyword?
  • sth dbh-gtprepare( statement )
  • step 3.
  • sth-gtexecute( Membrane )
  • step 4.
  • _at_result sth-gtfetchall_array()

7
Example - Python
  • import MySQLdb
  • step 1.
  • connection MySQLdb.connect(user"student", \
  • passwd"student",db"biolabdb")
  • step 2.
  • cursor connection.cursor()
  • statement "SELECT ID " \
  • "FROM keywords " \
  • "WHERE keywords
  • step 3.
  • cursor.execute( statement, Membrane) step 3.
  • step 4.
  • result cursor.fetchall() step 4.
  • print result
Write a Comment
User Comments (0)
About PowerShow.com