Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- An example to use only gluon, which is the name of
- web2py's data abstraction layer.
- http://caglartoklu.blogspot.com/2011/06/standalone-usage-of-web2pys-dal-gluon.html
- """
- import os
- import sys
- # web2py is distributed and deployed with zero configuration.
- # Let us follow the same approach.
- # To do this, I have opened a directory called "alone" inside
- # the web2py directory, next to applications.
- # I want to distribute the whole web2py directory.
- # So, I want to make sure that the gluon is automatically found.
- sys.path.append("../")
- # print sys.path
- from gluon.sql import DAL, Field
- from gluon.validators import *
- # For simplicity, we will be using SQLite.
- # First, determine the path for the database file.
- # We are headed for portability, so let us open the database
- # file in the same directory with this module.
- module_path = os.path.abspath(os.path.dirname(__file__))
- # print module_path # => /home/alex/Projects/web2py/alone
- # This is only the file name:
- db_name = "our_db.sqlite"
- # The protocol and full path to our database file.
- db = DAL('sqlite://' + db_name, folder=module_path)
- # Define the table "genre", note that the field "id" is automatic.
- db.define_table("genre",
- Field("name"))
- db.genre.name.requires = IS_NOT_EMPTY()
- # Define the table "band", note that the field "id" is automatic.
- # band.genre_id references to genre.id field.
- db.define_table("band",
- Field("name"),
- Field("origin"),
- Field("genre_id", db.genre))
- db.band.name.requires = IS_NOT_EMPTY()
- db.band.genre_id.requires = IS_IN_DB(db, db.genre.id, '%(name)s')
- db.band.genre_id.writable = False
- db.band.genre_id.readable = False
- # Let us insert some data for table "genre".
- # Each insert statement returns the unique_id.
- id_rock = db.genre.insert(name="rock")
- id_metal = db.genre.insert(name="metal")
- id_grunge = db.genre.insert(name="grunge") # we will use the ID.
- id_jazz = db.genre.insert(name="jazz")
- # The data will be stored only after a commit.
- db.commit()
- # We can also do a rollback, before a commit.
- print db.genre.insert(name="banana")
- # It will print 5 to the screen.
- db.rollback()
- # But it will not be stored in the database.
- # Let us insert some data for table "band".
- db.band.insert(name="Nirvana", origin="Aberdeen", genre_id=id_grunge)
- # Do not forget to commit.
- db.commit()
- # A simple select.
- for row in db(db.band.name == 'Nirvana').select():
- print row.name, row.origin
- #prints Nirvana Aberdeen
- # We can also take the last query.
- print db._lastsql
- # It will print this:
- # SELECT band.id, band.name, band.origin, band.genre_id FROM band
- # WHERE (band.name = 'Nirvana');
- # We can use free, raw SQL queries too.
- print db.executesql('SELECT * FROM genre;')
- # [(1, u'rock'), (2, u'metal'), (3, u'grunge'), (4, u'jazz')]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement