@@ -46,53 +46,80 @@ from GitHub and installed with Setuptools::
4646Example
4747-------
4848
49- The following examples assume that you have the quickstart database running (test@localhost).
50- If you don't, you can start it by running /opt/nuodb/run-quickstart.
51-
52- Simple example for connecting and reading from an existing table:
49+ Here is an example using the `PEP 249 `_ API that creates some tables, inserts
50+ some data, runs a query, and cleans up after itself:
5351
5452.. code :: python
5553
5654 import pynuodb
5755
58- connection = pynuodb.connect(" test" , " localhost" , " dba" , " goalie" , options = {' schema' :' hockey' })
56+ options = {" schema" : " test" }
57+ connect_kw_args = {' database' : " test" , ' host' : " localhost" , ' user' : " dba" , ' password' : " dba" , ' options' : options}
58+
59+ connection = pynuodb.connect(** connect_kw_args)
5960 cursor = connection.cursor()
60- cursor.arraysize = 3
61- cursor.execute(" select * from hockey" )
62- print cursor.fetchone()
6361
64- Data can be inserted into a table either explicitly within the execute method:
62+ stmt_drop = " DROP TABLE IF EXISTS names"
63+ cursor.execute(stmt_drop)
6564
66- .. code :: python
65+ stmt_create = """
66+ CREATE TABLE names (
67+ id BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
68+ name VARCHAR(30) DEFAULT '' NOT NULL,
69+ age INTEGER DEFAULT 0
70+ )"""
71+ cursor.execute(stmt_create)
6772
68- import pynuodb
73+ names = ((' Greg' , 17 ,), (' Marsha' , 16 ,), (' Jan' , 14 ,))
74+ stmt_insert = " INSERT INTO names (name, age) VALUES (?, ?)"
75+ cursor.executemany(stmt_insert, names)
6976
70- connection = pynuodb.connect(" test" , " localhost" , " dba" , " goalie" , options = {' schema' :' hockey' })
71- cursor = connection.cursor()
77+ connection.commit()
7278
73- cursor.execute(" create table typetest (bool_col boolean, date_col date, " +
74- " string_col string, integer_col integer)" )
79+ age_limit = 15
80+ stmt_select = " SELECT id, name FROM names where age > ? ORDER BY id"
81+ cursor.execute(stmt_select, (age_limit,))
82+ print (" Results:" )
83+ for row in cursor.fetchall():
84+ print (" %d | %s " % (row[0 ], row[1 ]))
7585
76- cursor.execute(" insert into typetest values ('False', '2012-10-03', 'hello world', 42) " )
77- cursor.execute( " select * from typetest " )
78- print cursor.fetchone ()
86+ cursor.execute(stmt_drop )
87+ cursor.close( )
88+ connection.close ()
7989
80- or using variables:
90+ All sorts of management and monitoring operations may be performed through the
91+ NuoDB Python API, a few below include listening to database state, and shutting
92+ down a database:
8193
8294.. code :: python
8395
84- import pynuodb
85-
86- connection = pynuodb.connect(" test" , " localhost" , " dba" , " goalie" , options = {' schema' :' hockey' })
87- cursor = connection.cursor()
88-
89- cursor.execute(" create table variabletest (bool_col boolean, date_col date, " +
90- " string_col string, integer_col integer)" )
91-
92- test_vals = (False , pynuodb.Date(2012 ,10 ,3 ), " hello world" , 42 )
93- cursor.execute(" insert into variabletest values (?, ?, ?, ?)" , test_vals)
94- cursor.execute(" select * from variabletest" )
95- print cursor.fetchone()
96+ import time
97+ from pynuodb import entity
98+
99+ class DatabaseListener (object ):
100+ def __init__ (self ):
101+ self .db_left = False
102+
103+ def process_left (self , process ):
104+ print (" process left: %s " % process)
105+
106+ def database_left (self , database ):
107+ print (" database shutdown: %s " % database)
108+ self .db_left = True
109+
110+ listener = DatabaseListener()
111+ domain = entity.Domain(" localhost" , " domain" , " bird" , listener)
112+ try :
113+ database = domain.get_database(" test" )
114+ if database is not None :
115+ database.shutdown(graceful = True )
116+ for i in range (1 , 20 ):
117+ time.sleep(0.25 )
118+ if listener.db_left:
119+ time.sleep(1 )
120+ break
121+ finally :
122+ domain.disconnect()
96123
97124 For further information on getting started with NuoDB, please refer to the Documentation _.
98125
0 commit comments