Advertisement
caglartoklu

Standalone Usage of web2py's DAL, gluon

Jun 24th, 2011
3,397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. """
  2. An example to use only gluon, which is the name of
  3. web2py's data abstraction layer.
  4. http://caglartoklu.blogspot.com/2011/06/standalone-usage-of-web2pys-dal-gluon.html
  5. """
  6.  
  7. import os
  8. import sys
  9.  
  10. # web2py is distributed and deployed with zero configuration.
  11. # Let us follow the same approach.
  12. # To do this, I have opened a directory called "alone" inside
  13. # the web2py directory, next to applications.
  14.  
  15. # I want to distribute the whole web2py directory.
  16. # So, I want to make sure that the gluon is automatically found.
  17.  
  18. sys.path.append("../")
  19. # print sys.path
  20.  
  21. from gluon.sql import DAL, Field
  22. from gluon.validators import *
  23.  
  24. # For simplicity, we will be using SQLite.
  25. # First, determine the path for the database file.
  26. # We are headed for portability, so let us open the database
  27. # file in the same directory with this module.
  28.  
  29. module_path = os.path.abspath(os.path.dirname(__file__))
  30. # print module_path # => /home/alex/Projects/web2py/alone
  31.  
  32. # This is only the file name:
  33. db_name = "our_db.sqlite"
  34.  
  35. # The protocol and full path to our database file.
  36. db = DAL('sqlite://' + db_name, folder=module_path)
  37.  
  38. # Define the table "genre", note that the field "id" is automatic.
  39. db.define_table("genre",
  40.         Field("name"))
  41. db.genre.name.requires = IS_NOT_EMPTY()
  42.  
  43. # Define the table "band", note that the field "id" is automatic.
  44. # band.genre_id references to genre.id field.
  45. db.define_table("band",
  46.         Field("name"),
  47.         Field("origin"),
  48.         Field("genre_id", db.genre))
  49. db.band.name.requires = IS_NOT_EMPTY()
  50. db.band.genre_id.requires = IS_IN_DB(db, db.genre.id, '%(name)s')
  51. db.band.genre_id.writable = False
  52. db.band.genre_id.readable = False
  53.  
  54. # Let us insert some data for table "genre".
  55. # Each insert statement returns the unique_id.
  56. id_rock = db.genre.insert(name="rock")
  57. id_metal = db.genre.insert(name="metal")
  58. id_grunge = db.genre.insert(name="grunge")  # we will use the ID.
  59. id_jazz = db.genre.insert(name="jazz")
  60.  
  61. # The data will be stored only after a commit.
  62. db.commit()
  63.  
  64. # We can also do a rollback, before a commit.
  65. print db.genre.insert(name="banana")
  66. # It will print 5 to the screen.
  67. db.rollback()
  68. # But it will not be stored in the database.
  69.  
  70. # Let us insert some data for table "band".
  71. db.band.insert(name="Nirvana", origin="Aberdeen", genre_id=id_grunge)
  72. # Do not forget to commit.
  73. db.commit()
  74.  
  75. # A simple select.
  76. for row in db(db.band.name == 'Nirvana').select():
  77.     print row.name, row.origin
  78.     #prints Nirvana Aberdeen
  79.  
  80. # We can also take the last query.
  81. print db._lastsql
  82. # It will print this:
  83. # SELECT  band.id, band.name, band.origin, band.genre_id FROM band
  84. # WHERE (band.name = 'Nirvana');
  85.  
  86. # We can use free, raw SQL queries too.
  87. print db.executesql('SELECT * FROM genre;')
  88. # [(1, u'rock'), (2, u'metal'), (3, u'grunge'), (4, u'jazz')]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement