Skip to content

Commit fecc4a6

Browse files
committed
added insert_id
1 parent b1f538e commit fecc4a6

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

pymysql/connections.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ def _config(key, default):
508508
self.encoders = encoders
509509
self.decoders = conv
510510

511+
self._result = None
511512
self._affected_rows = 0
512513
self.host_info = "Not connected"
513514

@@ -685,6 +686,12 @@ def _read_query_result(self):
685686
self._result = result
686687
return result.affected_rows
687688

689+
def insert_id(self):
690+
if self._result:
691+
return self._result.insert_id
692+
else:
693+
return 0
694+
688695
def _send_command(self, command, sql):
689696
#send_data = struct.pack('<i', len(sql) + 1) + command + sql
690697
# could probably be more efficient, at least it's correct

pymysql/cursors.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ def _do_get_result(self):
240240
self.lastrowid = conn._result.insert_id
241241
self._rows = conn._result.rows
242242
self._has_next = conn._result.has_next
243-
conn._result = None
244243

245244
def __iter__(self):
246245
return iter(self.fetchone, None)

pymysql/tests/test_issues.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,17 @@ def disabled_test_issue_54(self):
249249
c.execute("drop table issue54")
250250

251251
class TestGitHubIssues(base.PyMySQLTestCase):
252-
pass
252+
def test_issue_66(self):
253+
conn = self.connections[0]
254+
c = conn.cursor()
255+
self.assertEquals(0, conn.insert_id())
256+
try:
257+
c.execute("create table issue66 (id integer primary key auto_increment, x integer)")
258+
c.execute("insert into issue66 (x) values (1)")
259+
c.execute("insert into issue66 (x) values (1)")
260+
self.assertEquals(2, conn.insert_id())
261+
finally:
262+
c.execute("drop table issue66")
253263

254264
__all__ = ["TestOldIssues", "TestNewIssues", "TestGitHubIssues"]
255265

0 commit comments

Comments
 (0)