Exemplo n.º 1
0
        self.price = 34
        self.order_id = self.id
        Order.id = Order.id + 1

register_adapter(Album, ObjectMapper)
register_adapter(Order, ObjectMapper)
    
# Describe what is needed to save on each object
# This is actually just configuration, you can use xml with a parser if you
# like to have plenty of wasted CPU cycles ;P.

persistent_fields = {'Album': ['album_id', 'creation_time', 'binary_data'],
                              'Order':  ['order_id', 'items', 'price']
                            }
 
print adapt(Album()).generateInsert()
print adapt(Album()).generateInsert()
print adapt(Album()).generateInsert()
print adapt(Order()).generateInsert()
print adapt(Order()).generateInsert()
print adapt(Order()).generateInsert()

"""
- Discussion

Psycopg 2 has a great new feature: adaptation. The big thing about 
adaptation is that it enable the programmer to glue most of the 
code out there without many difficulties.

This recipe tries to focus the attention on a way to generate SQL queries to 
insert  completely new objects inside a database. As you can see objects do 
Exemplo n.º 2
0
Arquivo: dt.py Projeto: fabioz/coev
    )
conn.commit()

# build and insert some data using mx.DateTime
mx1 = (
    1,
    mx.DateTime.Date(2004, 10, 19),
    mx.DateTime.Time(0, 11, 17.015),
    mx.DateTime.Timestamp(2004, 10, 19, 0, 11, 17.5),
    mx.DateTime.DateTimeDelta(13, 15, 17, 59.9),
)

from psycoev.extensions import adapt
import psycoev.extras

print adapt(mx1)

print "Inserting mx.DateTime values..."
curs.execute("INSERT INTO test_dt VALUES (%s, %s, %s, %s, %s)", mx1)

# build and insert some values using the datetime adapters
dt1 = (
    2,
    datetime.date(2004, 10, 19),
    datetime.time(0, 11, 17, 15000),
    datetime.datetime(2004, 10, 19, 0, 11, 17, 500000),
    datetime.timedelta(13, 15 * 3600 + 17 * 60 + 59, 900000),
)

print "Inserting Python datetime values..."
curs.execute("INSERT INTO test_dt VALUES (%s, %s, %s, %s, %s)", dt1)