Merge pull request #170 from methane/select-db

Add con.select_db() method. (fixes #80)
This commit is contained in:
INADA Naoki
2013-09-30 09:57:40 -07:00
2 changed files with 18 additions and 0 deletions

View File

@@ -706,6 +706,11 @@ class Connection(object):
self._execute_command(COM_QUERY, "ROLLBACK")
self._read_ok_packet()
def select_db(self, db):
'''Set current db'''
self._execute_command(COM_INIT_DB, db)
self._read_ok_packet()
def escape(self, obj):
''' Escape whatever value you pass to it '''
if isinstance(obj, str_type):

View File

@@ -41,6 +41,19 @@ class TestConnection(base.PyMySQLTestCase):
cur.execute("SELECT @@AUTOCOMMIT")
self.assertEqual(cur.fetchone()[0], 0)
def test_select_db(self):
con = self.connections[0]
current_db = self.databases[0]['db']
other_db = self.databases[1]['db']
cur = con.cursor()
cur.execute('SELECT database()')
self.assertEqual(cur.fetchone()[0], current_db)
con.select_db(other_db)
cur.execute('SELECT database()')
self.assertEqual(cur.fetchone()[0], other_db)
if __name__ == "__main__":
try: