# ORDERS # Create the orders table if it doesn't exist yet and add data. if not "orders" in db: schema = ( pk(), field("product_id", INTEGER), field("customer_id", INTEGER), field("date", DATE, default=NOW) # By default, current date/time. ) 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")
# ORDERS # Create the orders table if it doesn't exist yet and add data. if not "orders" in db: schema = ( pk(), field("product_id", INTEGER), field("customer_id", INTEGER), field("date", DATE, default=NOW) # By default, current date/time. ) 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