demo2b.py :  » Database » PyPgSQL » pyPgSQL-2.5.1 » examples » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » PyPgSQL 
PyPgSQL » pyPgSQL 2.5.1 » examples » demo2b.py
#!/usr/local/bin/python
#ident "$Id: demo2b.py,v 1.3 2001/10/18 03:17:08 ballie01 Exp $"
#-----------------------------------------------------------------------+
# demo2b.py - Print out the results of a select (similar to pgsql).  |
#-----------------------------------------------------------------------+
import time
from pyPgSQL import PgSQL

c = PgSQL.connect()

c.autocommit = 1 # Auto commit must be on to drop the tables!

c1 = c.cursor()

# Make sure that the tables and rules don't exist.
try:
    c1.execute("DROP RULE demo2r1")
except StandardError:
    pass

try:
    c1.execute("DROP TABLE demo2a")
except StandardError:
    pass

try:
    c1.execute("DROP TABLE demo2b")
except StandardError:
    pass

# Create the tables and rule.
c1.execute("""
CREATE TABLE demo2a (
  updated timestamp default now(),
  name varchar(30)
        )""")
c1.execute("""
CREATE TABLE demo2b (
  updated timestamp default now(),
  name varchar(30)
        )""")
c1.execute("""
CREATE RULE demo2r1 AS ON INSERT TO demo2b DO
  (INSERT INTO demo2a (name) values (new.name); NOTIFY demo2a)""")

c1.close()
del c1

c.autocommit = 0

c1 = c.cursor()

# Update the table (which kicks off the rule which sends the notify
# Note: The notification does not occur until a commit() happens.  Multiple
#  updates to demo2b with a single commit() will only send one notice.

c1.execute("INSERT INTO demo2b (name) VALUES ('This is a test(1)')")
c1.execute("INSERT INTO demo2b (name) VALUES ('This is another test(2)')")
c.commit()  # Note: 2 updates, 1 notice.
time.sleep(5)
c1.execute("INSERT INTO demo2b (name) VALUES ('And another test(3)')")
c.commit()
time.sleep(10)
c1.execute("INSERT INTO demo2b (name) VALUES ('The final insert(4)')")
c.commit()

print "\nThere should of been 3 notices sent and recieved.\n"

del c1, c
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.