+ Reply to Thread
Results 1 to 3 of 3

Thread: Database access using Python

  1. #1
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Database access using Python

    Lately I've been writing Python scripts in an effort to better
    understand the language. After completing this last one, I
    decided to make a little tutorial out of it.

    This script uses the dbapi2 of the pysqlite2 package, which is
    basically just a wrapper around a sqlite database which simplifies
    Python access.
    A sqlite database is created and a fixed set of data is inserted into it,
    then a query is executed with the result being printed to standard out.

    Here is the complete script:
    Code:
    #!/bin/env python
    
    from pysqlite2 import dbapi2
    
    debtboy_connection = dbapi2.connect('linux.db')
    debtboy_cursor = debtboy_connection.cursor()
    
    debtboy_cursor.execute('''CREATE TABLE
                              release(distro VARCHAR(15),
                              version VARCHAR(5),
                              type VARCHAR(10))''')
    
    debtboy_data = [
                     ('Gentoo', '2009', 'rolling'),
                     ('Arch', '2009', 'rolling'),
                     ('Slackware', '13.1', 'fixed'),
                     ('Fedora', '11.0', 'fixed'),
                     ('Debian', '5.0', 'fixed'),
                     ('Ubuntu', '9.04', 'fixed'),
                     ('openSUSE', '11.1', 'fixed'),
                     ('Mandriva', '2009', 'fixed'),
                     ('Mint', '7.0', 'fixed'),
                     ('PCLinuxOS', '2009', 'rolling'),
                   ]
     
    debtboy_cursor.executemany('''INSERT INTO 
                                  release(distro, 
                                  version,type) 
                                  VALUES(?, ?, ?)''', debtboy_data)
    
    debtboy_cursor.execute('''SELECT distro, 
                              version, 
                              type 
                              FROM release
                              ORDER BY distro''')
    
    print 
    
    column_width = 15
    
    for column_description in debtboy_cursor.description:
       print column_description[0].ljust(column_width) ,
    print
    
    print '-' * 45
    
    debtboy_index = range(len(debtboy_cursor.description))
    
    for row in debtboy_cursor:
       for index in debtboy_index:
          debtboy_value = str(row[index])
          print debtboy_value.ljust(column_width) ,
       print
    
    debtboy_connection.commit()
    debtboy_connection.close()
    print

    This is the first section, it starts off with a she-bang #!
    and following that is the Python interpreter.
    Then the dbapi2 is imported from pysqlite2 so we can establish
    a database connection (if the database were trying to connect
    doesn't exist, then it will be created).
    A db cursor is also created and used to make a table.
    Code:
    #!/bin/env python
    
    from pysqlite2 import dbapi2
    
    debtboy_connection = dbapi2.connect('linux.db')
    debtboy_cursor = debtboy_connection.cursor()
    
    debtboy_cursor.execute('''CREATE TABLE
                              release(distro VARCHAR(15),
                              version VARCHAR(5),
                              type VARCHAR(10))''')

    Here is the fixed set of data (Linux distro names, versions and types).
    3 string literals are each saved in a tuple and all the tuples are saved
    in a list. This data is inserted into the database using the executemany
    function which saves me from looping and inserting each row.
    Code:
    debtboy_data = [
                     ('Gentoo', '2009', 'rolling'),
                     ('Arch', '2009', 'rolling'),
                     ('Slackware', '13.1', 'fixed'),
                     ('Fedora', '11.0', 'fixed'),
                     ('Debian', '5.0', 'fixed'),
                     ('Ubuntu', '9.04', 'fixed'),
                     ('openSUSE', '11.1', 'fixed'),
                     ('Mandriva', '2009', 'fixed'),
                     ('Mint', '7.0', 'fixed'),
                     ('PCLinuxOS', '2009', 'rolling'),
                   ]
     
    debtboy_cursor.executemany('''INSERT INTO 
                                  release(distro, 
                                  version,type) 
                                  VALUES(?, ?, ?)''', debtboy_data)


    This last section executes a query selecting the data, which is
    then looped through and displayed on standard out. You probably
    noticed a number of stand alone "print" commands which were used
    to format the data (adding a newline).
    Code:
    debtboy_cursor.execute('''SELECT distro, 
                              version, 
                              type 
                              FROM release
                              ORDER BY distro''')
    
    print 
    
    column_width = 15
    
    for column_description in debtboy_cursor.description:
       print column_description[0].ljust(column_width) ,
    print
    
    print '-' * 45
    
    debtboy_index = range(len(debtboy_cursor.description))
    
    for row in debtboy_cursor:
       for index in debtboy_index:
          debtboy_value = str(row[index])
          print debtboy_value.ljust(column_width) ,
       print
    
    debtboy_connection.commit()
    debtboy_connection.close()
    print
    I chose to make this an executable script from the shell, so
    here I am using chmod to make it executable.
    I could have left it as a regular file and ran it from the Python
    interpreter in which case the first line wouldn't be needed.



    Here is the standard out result of the above query.


    A simple script which demonstrates database access using Python.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Database access using Python

    Very well done! +rep

  4. #3
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Database access using Python

    Nice demo
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Access Database in php?
    By Hamed in forum PHP Development
    Replies: 0
    Last Post: 07-23-2011, 06:12 AM
  2. Access toi Database
    By djscottyis in forum PHP Development
    Replies: 0
    Last Post: 05-10-2011, 02:01 AM
  3. uploading database records in the access database in vb.net
    By Diana86 in forum Visual Basic Programming
    Replies: 0
    Last Post: 11-20-2008, 10:50 PM
  4. Retrieving Access Database
    By tigger in forum Visual Basic Programming
    Replies: 0
    Last Post: 07-05-2007, 10:27 PM
  5. Database access
    By jimmyfrube in forum Visual Basic Programming
    Replies: 1
    Last Post: 04-03-2007, 01:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts