8000 Add con.select_db() method. (fixes #80) · gcmcom/PyMySQL@c591645 · GitHub
[go: up one dir, main page]

Skip to content

Commit c591645

Browse files
committed
Add con.select_db() method. (fixes PyMySQL#80)
1 parent e03a23b commit c591645

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

pymysql/connections.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,11 @@ def rollback(self):
706706
self._execute_command(COM_QUERY, "ROLLBACK")
707707
self._read_ok_packet()
708708

709+
def select_db(self, db):
710+
'''Set current db'''
711+
self._execute_command(COM_INIT_DB, db)
712+
self._read_ok_packet()
713+
709714
def escape(self, obj):
710715
''' Escape whatever value you pass to it '''
711716
if isinstance(obj, str_type):

pymysql/tests/test_connection.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ def test_autocommit(self):
4141
cur.execute("SELECT @@AUTOCOMMIT")
4242
self.assertEqual(cur.fetchone()[0], 0)
4343

44+
def test_select_db(self):
45+
con = self.connections[0]
46+
current_db = self.databases[0]['db']
47+
other_db = self.databases[1]['db']
48+
49+
cur = con.cursor()
50+
cur.execute('SELECT database()')
51+
self.assertEqual(cur.fetchone()[0], current_db)
52+
53+
con.select_db(other_db)
54+
cur.execute('SELECT database()')
55+
self.assertEqual(cur.fetchone()[0], other_db)
56+
4457

4558
if __name__ == "__main__":
4659
try:

0 commit comments

Comments
 (0)
0