Skip to content

Commit

Permalink
jkklee#13 feat: return lastrowid when insert the record to DB
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver.su committed Dec 19, 2022
1 parent b86bf5a commit fa97f20
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pymysqlpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def ping(self, reconnect=True):
else:
raise

def execute_query(self, query, args=(), dictcursor=False, return_one=False, exec_many=False):
def execute_query(self, query, args=(), dictcursor=False, return_one=False, exec_many=False, return_lastrowid=False):
"""
A wrapped method of pymysql's execute() or executemany().
dictcursor: whether want use the dict cursor(cursor's default type is tuple)
Expand All @@ -107,8 +107,12 @@ def execute_query(self, query, args=(), dictcursor=False, return_one=False, exec
cur.execute(query, args)
except Exception:
raise
# if no record match the query, return () if return_one==False, else return None
return cur.fetchone() if return_one else cur.fetchall()
if return_lastrowid:
return cur.fetchone() if return_one else cur.fetchall(
), cur.lastrowid
else:
# if no record match the query, return () if return_one==False, else return None
return cur.fetchone() if return_one else cur.fetchall()


class ConnectionPool:
Expand Down

0 comments on commit fa97f20

Please sign in to comment.