Python 3.x database framework Sqlalchemy for SQlite (continued)

Source: Internet
Author: User

Next, we will explain the Sqlalchemy framework. Here is another example.

Related reading: database framework of Python 3.x Sqlalchemy for SQlite operations

As follows:

This example is based on others' Code.

  1. _ Author _ ='Minggxu9'
  2. ImportSqlalchemy
  3. FromSqlalchemyImportCreate_engine
  4. Eng = create_engine ('Sqlite: //: memory :', Echo =True)
  5. Eng.exe cute ("Select 1"). Scalar ()
  6. FromSqlalchemy. ext. declarativeImportDeclarative_base
  7. Base = declarative_base ()
  8. ######################################## #################
  9. FromSqlalchemyImportJoin, Table, MetaData ,\
  10. Select, func ,\
  11. And _,\
  12. Column, ForeignKey ,\
  13. Integer, String, Text, Binary
  14. FromSqlalchemy. ormImportDeferred, mapper ,\
  15. Relationship, column_property ,\
  16. Object_session ,\
  17. Validates
  18. ######################################## #################
  19. Metadata = MetaData ()
  20. ######################################## #################
  21. ClassBook (Base ):
  22. _ Tablename __='Book'
  23. Book_id = Column (Integer, primary_key =True)
  24. Title = Column (String (200), Nullable =False)
  25. Summary = Column (String (2000))
  26. Excerpt = deferred (Column (Text ))
  27. Photo1 = deferred (Column (Binary), group ='Photos')
  28. Photo2 = deferred (Column (Binary), group ='Photos')
  29. Photo3 = deferred (Column (Binary), group ='Photos')
  30. ClassAddress (Base ):
  31. _ Tablename __='Address'
  32. Id = Column (Integer, primary_key =True)
  33. User_id = Column (Integer, ForeignKey ('User. id'))
  34. ClassUser (Base ):
  35. _ Tablename __='User'
  36. Id = Column (Integer, primary_key =True)
  37. Firstname = Column (String (50))
  38. Lastname = Column (String (50))
  39. # Fullname = column_property (firstname + ''+ lastname)
  40. # Address_count = column_property (
  41. # Select ([func. count (Address. id)]). \
  42. # Where (Address. user_id = id)
  43. #)
  44. Addresses = relationship ("Address") 
  45.  
  46. @ Validates('Address')
  47. DefValidate_address (Self, Key, address ):
  48. Assert '@' InAddress. email
  49. ReturnAddress 
  50.  
  51. @ Property
  52. DefFullname (Self):
  53. Return Self. Firstname +''+Self. Lastname 
  54.  
  55. @ Property
  56. DefAddress_count (Self):
  57. ReturnObject_session (Self).\
  58. Scalar (select ([func. count (Address. id)]). \
  59. Where (Address. user_id =Self. Id ))
  60. ######################################## #################
  61. Base. metadata. create_all (eng)
  62. # Ed1_user = User ('ed1', 'ed1 Jones ', 'ed1spassword ')
  63. # Ed2_user = User ('ed2', 'ed2 Jones ', 'ed2spassword ')
  64. # Print ("-----------------------")
  65. # From sqlalchemy. orm import Session
  66. # Ses = Session (bind = eng)
  67. # Ses. add (ed1_user)
  68. # Ses. add (ed2_user)
  69. # Ses. commit ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.