Last active
August 29, 2015 14:04
-
-
Save xiexiao/5f5778a2f479fc7729c5 to your computer and use it in GitHub Desktop.
a python dbhook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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