示例#1
0
print "There are", len(db.products), "products available:"
for row in assoc(db.products):
    print row

# Note how the orders table only contains integer id's.
# This is much more efficient than storing entire strings (e.g., customer address).
# To get the related data, we can create a query with relations between the tables.
q = db.orders.search(
    fields = (
       "products.description", 
       "products.price", 
       "customers.name", 
       "date"
    ),
    relations = (
        rel("product_id", "products.id", "products"),
        rel("customer_id", "customers.id", "customers")
    ))
print
print "Invoices:"
for row in assoc(q):
    print row # (product description, product price, customer name, date created)
print
print "Invoice query SQL syntax:"
print q
print
print "Invoice query XML:"
print q.xml

# The XML can be passed to Database.create() to create a new table (with data).
# This is explained in the online documentation.
示例#2
0
        ))
    db.orders.append(product_id=1, customer_id=2)  # Hofstadter orders pizza.

# Show all the products in the database:
print "There are", db.products.count(), "products available:"
for row in db.products:
    print row

# Note how the orders table only contains integer id's.
# This is much more efficient than storing entire strings (e.g., customer address).
# To get the related data, we can create a query with relations between the tables.
q = db.orders.search(fields=[
    "products.description", "products.price", "customers.name", "date"
],
                     relations=[
                         rel("product_id", "products.id", "products"),
                         rel("customer_id", "customers.id", "customers")
                     ])
print
print "Invoices:"
for row in q:
    print row  # (product description, product price, customer name, date created)
print
print "Invoice query SQL syntax:"
print q
print
print "Invoice query XML:"
print q.xml

# The XML can be passed to Database.create() to create a new table (with data).
# This is explained in the online documentation.
示例#3
0
    )
    db.create("orders", schema)
    db.orders.append(product_id=1, customer_id=2)  # Hofstadter orders pizza.

# Show all the products in the database.
# The assoc() iterator yields each row as a dictionary.
print "There are", len(db.products), "products available:"
for row in assoc(db.products):
    print row

# Note how the orders table only contains integer id's.
# This is much more efficient than storing entire strings (e.g., customer address).
# To get the related data, we can create a query with relations between the tables.
q = db.orders.search(fields=("products.description", "products.price",
                             "customers.name", "date"),
                     relations=(rel("product_id", "products.id", "products"),
                                rel("customer_id", "customers.id",
                                    "customers")))
print
print "Invoices:"
for row in assoc(q):
    print row  # (product description, product price, customer name, date created)
print
print "Invoice query SQL syntax:"
print q
print
print "Invoice query XML:"
print q.xml

# The XML can be passed to Database.create() to create a new table (with data).
# This is explained in the online documentation.