Skip to content

Instantly share code, notes, and snippets.

@xiexiao
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiexiao/5f5778a2f479fc7729c5 to your computer and use it in GitHub Desktop.
Save xiexiao/5f5778a2f479fc7729c5 to your computer and use it in GitHub Desktop.
a python dbhook
def dbhook():
'''数据库的hook'''
def _(func):
'''_'''
def wrapper(*a, **kw):
'''wrapper'''
_handler = a[0]
db = "DB/ershou.db"
_handler.conn = sqlite3.connect(db,check_same_thread = False)
_handler.conn.execute('pragma foreign_keys = on')
_handler.conn.commit()
_handler.cur = _handler.conn.cursor()
try:
result = func(*a, **kw)
finally:
_handler.conn.close()
return result
return wrapper
return _
#调用
@dbhook()
def GET(self):
DBshuC = self.cur.execute("SELECT post,link,name,date from caiji")
self.conn.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment