Skew

Static constructors

Jun 7th, 2016
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Field(object):
  2.  
  3.     def __init__(self, fid, sid, name, unit):
  4.         self.fid = fid
  5.         self.sid = sid
  6.         self.name = name
  7.         self.unit = unit
  8.  
  9.     @staticmethod
  10.     def get_by_fid(fid):
  11.         with sqlite3.connect("db.sqlite") as conn:
  12.             results = conn.execute("SELECT * FROM fields WHERE fid=?;",[fid])
  13.             result = results[0]
  14.             return Field(result[0], result[1], result[2], result[3])
  15.  
  16.     @staticmethod
  17.     def get_by_name(sid, name):
  18.         with sqlite3.connect("db.sqlite") as conn:
  19.             results = conn.execute("SELECT * FROM fields WHERE sid=? AND name=?;",[sid,name])
  20.             result = results[0]
  21.             return Field(result[0], result[1], result[2], result[3])
  22.  
  23. my_field = Field.get_by_fid(123)
  24. print("Field name is: {}".format(my_field.name))
Advertisement
Add Comment
Please, Sign In to add comment