The Artima Developer Community
Sponsored Link

Python Buzz Forum
Sqlite usage

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Baiju M

Posts: 225
Nickname: baijum81
Registered: Aug, 2004

Baiju M is a new user
Sqlite usage Posted: Sep 14, 2006 10:21 PM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Baiju M.
Original Post: Sqlite usage
Feed Title: Programming life without Python(?)
Feed URL: http://baijum81.livejournal.com/data/rss
Feed Description: Programming life without Python(?)
Latest Python Buzz Posts
Latest Python Buzz Posts by Baiju M
Latest Posts From Programming life without Python(?)

Advertisement
This is a simple tutorial(?) to use sqlite. I used pysqlite 1.0.1 in Python 2.3,
which is the default in Debian 3.1 (Sarge).

The latest pysqlite version is 2.3.2, Visit this site for more info:
http://initd.org/tracker/pysqlite

"""sqlite_usage.py: sqlite module usage with examples

First import sqlite::

  >>> import sqlite
  
Create a temporary directory::

  >>> import tempfile
  >>> tempdir = tempfile.mkdtemp()
  
Now create a db file::

  >>> import os
  >>> dbfile = os.path.join(tempdir, 'testdb.sdb')
  
Now connect to db and create a cursor::

  >>> conn = sqlite.connect(dbfile)
  >>> cr = conn.cursor()

Create a table::

  >>> cr.execute("CREATE TABLE first_table (a INT)")

Insert a value and get::

  >>> cr.execute("INSERT INTO first_table (a) values (1)")
  >>> cr.execute("SELECT a FROM first_table")
  >>> cr.fetchall()
  [(1,)]

Test rollback::

  >>> conn.commit()
  >>> cr.execute("DELETE FROM first_table")
  >>> cr.execute("SELECT a FROM first_table")
  >>> cr.fetchall()
  []
  >>> #now rollback and query again
  >>> conn.rollback()
  >>> cr.execute("SELECT a FROM first_table")
  >>> cr.fetchall()
  [(1,)]
  
Insert more values as parameters::

  >>> cr.execute("CREATE TABLE second_table (a INT, b VARCHAR(20))")
  >>> cr.execute("INSERT INTO second_table (a, b) VALUES (%s, %s)",
  ... (1, 'hi1'))
  >>> cr.execute("SELECT a, b FROM second_table")
  >>> cr.fetchone()
  (1, 'hi1')

Insert more values using executemany::

  >>> cr.executemany("INSERT INTO second_table (a, b) VALUES (%s, %s)",
  ... [(2, 'hi2'), (3, 'hi3')])
  >>> cr.execute("SELECT a, b FROM second_table")
  >>> rst = cr.fetchall()
  >>> (2, 'hi2') in rst
  True
  >>> (3, 'hi3') in rst
  True

Hmm.. there is no rewind::

  >>> cr.rewind
  Traceback (most recent call last):
  ...
  AttributeError: rewind

Views are supported::

  >>> cr.execute("CREATE VIEW first_second_view \
          AS SELECT a.a AS a1, b.a AS a2, b.b \
          FROM first_table AS a LEFT JOIN second_table AS b \
          ON a.a = b.a")
  >>> cr.execute("SELECT a1, a2, b FROM first_second_view")
  >>> cr.fetchone()
  (1, 1, 'hi1')

Cleanup tempdir::

  >>> import shutil
  >>> shutil.rmtree(tempdir)

"""

def _test():
    import doctest
    return doctest.testmod()

if __name__ == '__main__':
    _test()
    pass

Read: Sqlite usage

Topic: Plone 2.5.1 RC1 Installer for Windows Previous Topic   Next Topic Topic: Heads up, small issue with Plone 2.5.1-rc1

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use