本文整理汇总了Python中_mysql_connector.MySQL.connect方法的典型用法代码示例。如果您正苦于以下问题:Python MySQL.connect方法的具体用法?Python MySQL.connect怎么用?Python MySQL.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类_mysql_connector.MySQL
的用法示例。
在下文中一共展示了MySQL.connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_query
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import connect [as 别名]
def test_query(self):
config = self.connect_kwargs.copy()
cmy = MySQL(buffered=True)
self.assertRaises(MySQLInterfaceError, cmy.query)
cmy.connect(**config)
self.assertRaises(MySQLInterfaceError, cmy.query, "SELECT spam")
self.assertTrue(cmy.query("SET @ham = 4"))
self.assertEqual(None, cmy.num_fields())
self.assertEqual(0, cmy.field_count())
self.assertTrue(cmy.query("SELECT @ham"))
self.assertEqual(4, cmy.fetch_row()[0])
self.assertEqual(None, cmy.fetch_row())
cmy.free_result()
self.assertTrue(cmy.query("SELECT 'ham', 'spam', 5", raw=True))
row = cmy.fetch_row()
self.assertTrue(isinstance(row[0], bytearray))
self.assertEqual(bytearray(b'spam'), row[1])
self.assertEqual(None, cmy.fetch_row())
cmy.free_result()
示例2: test_character_set_name
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import connect [as 别名]
def test_character_set_name(self):
cmy1 = MySQL(buffered=True)
self.assertRaises(MySQLInterfaceError, cmy1.character_set_name)
cmy1.connect(**self.connect_kwargs)
self.assertEqual('latin1', cmy1.character_set_name())
示例3: test_escape_string
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import connect [as 别名]
def test_escape_string(self):
cases = [
('new\nline', b'new\\nline'),
('carriage\rreturn', b'carriage\\rreturn'),
('control\x1aZ', b'control\\ZZ'),
("single'quote", b"single\\'quote"),
('double"quote', b'double\\"quote'),
('back\slash', b'back\\\\slash'),
('nul\0char', b'nul\\0char'),
(u"Kangxi⽃\0⽇", b'Kangxi\xe2\xbd\x83\\0\xe2\xbd\x87'),
(b'bytes\0ob\'j\n"ct\x1a', b'bytes\\0ob\\\'j\\n\\"ct\\Z'),
]
cmy = MySQL()
cmy.connect(**self.connect_kwargs)
unicode_string = u"Kangxi⽃\0⽇"
self.assertRaises(UnicodeEncodeError, cmy.escape_string, unicode_string)
cmy.set_character_set("UTF8")
for value, exp in cases:
self.assertEqual(exp, cmy.escape_string(value))
self.assertRaises(TypeError, cmy.escape_string, 1234);
示例4: test_rollback
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import connect [as 别名]
def test_rollback(self):
cmy1 = MySQL(buffered=True)
cmy1.connect(**self.connect_kwargs)
cmy2 = MySQL(buffered=True)
cmy2.connect(**self.connect_kwargs)
table = "commit_test"
cmy1.query("DROP TABLE IF EXISTS {0}".format(table))
cmy1.query("CREATE TABLE {0} (c1 INT)".format(table))
cmy1.query("START TRANSACTION")
cmy1.query("INSERT INTO {0} (c1) VALUES "
"(1), (2), (3)".format(table))
cmy1.commit()
cmy2.query("SELECT * FROM {0}".format(table))
self.assertEqual([(1,), (2,), (3,)], fetch_rows(cmy2))
cmy1.query("START TRANSACTION")
cmy1.query("INSERT INTO {0} (c1) VALUES "
"(4), (5), (6)".format(table))
cmy1.rollback()
cmy2.query("SELECT * FROM {0}".format(table))
self.assertEqual(3, cmy2.num_rows())
cmy1.query("DROP TABLE IF EXISTS {0}".format(table))
示例5: test_connected
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import connect [as 别名]
def test_connected(self):
config = self.connect_kwargs.copy()
cmy = MySQL()
self.assertFalse(cmy.connected())
cmy.connect(**config)
self.assertTrue(cmy.connected())
cmy.close()
self.assertFalse(cmy.connected())
示例6: test_get_server_info
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import connect [as 别名]
def test_get_server_info(self):
cmy = MySQL()
self.assertRaises(MySQLInterfaceError, cmy.get_server_info)
cmy.connect(**self.connect_kwargs)
version = cmy.get_server_version()
info = cmy.get_server_info()
self.assertIsInstance(info, str)
self.assertTrue(info.startswith('.'.join([str(v) for v in version])))
示例7: test_hex_string
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import connect [as 别名]
def test_hex_string(self):
config = self.connect_kwargs.copy()
cmy = MySQL(buffered=True)
table = "hex_string"
cases = {
'utf8': [
(u'ham', b"X'68616D'"),
],
'big5': [
(u'\u5C62', b"X'B9F0'")
],
'sjis': [
(u'\u005c', b"X'5C'"),
],
'gbk': [
(u'赵孟頫', b"X'D5D4C3CFEE5C'"),
(u'赵\孟\頫\\', b"X'D5D45CC3CF5CEE5C5C'"),
(u'遜', b"X'DF64'")
],
'ascii': [
('\x5c\x00\x5c', b"X'5C005C'"),
],
}
cmy.connect(**config)
def create_table(charset):
cmy.query("DROP TABLE IF EXISTS {0}".format(table))
cmy.query("CREATE TABLE {0} (id INT, "
"c1 VARCHAR(400)) CHARACTER SET {1}".format(
table, charset))
insert = "INSERT INTO {0} (id, c1) VALUES ({{id}}, {{hex}})".format(
table)
select = "SELECT c1 FROM {0} WHERE id = {{id}}".format(table)
for encoding, data in cases.items():
create_table(encoding)
for i, info in enumerate(data):
case, exp = info
cmy.set_character_set(encoding)
hexed = cmy.hex_string(case.encode(encoding))
self.assertEqual(exp, hexed)
cmy.query(insert.format(id=i, hex=hexed.decode()))
cmy.query(select.format(id=i))
try:
fetched = fetch_rows(cmy)[0][0]
except UnicodeEncodeError:
self.fail("Could not encode {0}".format(encoding))
self.assertEqual(case, fetched.decode(encoding),
"Failed with case {0}/{1}".format(i, encoding))
cmy.query("DROP TABLE IF EXISTS {0}".format(table))
示例8: test_warning_count
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import connect [as 别名]
def test_warning_count(self):
cmy = MySQL()
cmy.connect(**self.connect_kwargs)
cmy.query("SELECT 'a' + 'b'", buffered=False)
fetch_rows(cmy)
self.assertEqual(2, cmy.warning_count())
cmy.query("SELECT 1 + 1", buffered=True)
self.assertEqual(0, cmy.warning_count())
fetch_rows(cmy)
示例9: test___init__
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import connect [as 别名]
def test___init__(self):
cmy = MySQL()
self.assertEqual(False, cmy.buffered())
self.assertEqual(False, cmy.raw())
cmy = MySQL(buffered=True, raw=True)
self.assertEqual(True, cmy.buffered())
self.assertEqual(True, cmy.raw())
exp = 'gbk'
cmy = MySQL(charset_name=exp)
cmy.connect(**self.connect_kwargs)
self.assertEqual(exp, cmy.character_set_name())
示例10: test_get_server_version
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import connect [as 别名]
def test_get_server_version(self):
cmy = MySQL()
self.assertRaises(MySQLInterfaceError, cmy.get_server_version)
cmy.connect(**self.connect_kwargs)
version = cmy.get_server_version()
self.assertIsInstance(version, tuple)
self.assertEqual(3, len(version))
self.assertTrue(all([isinstance(v, int) and v > 0 for v in version]))
self.assertTrue(3 < version[0] < 7)
self.assertTrue(0 < version[1] < 20)
self.assertTrue(0 < version[2] < 99)
示例11: test_select_db
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import connect [as 别名]
def test_select_db(self):
cmy = MySQL(buffered=True)
cmy.connect(**self.connect_kwargs)
cmy.select_db('mysql')
cmy.query("SELECT DATABASE()")
self.assertEqual(b'mysql', cmy.fetch_row()[0])
cmy.free_result()
cmy.select_db('myconnpy')
cmy.query("SELECT DATABASE()")
self.assertEqual(b'myconnpy', cmy.fetch_row()[0])
cmy.free_result()
示例12: test_thread_id
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import connect [as 别名]
def test_thread_id(self):
cmy = MySQL()
self.assertRaises(MySQLInterfaceError, cmy.thread_id)
cmy.connect(**self.connect_kwargs)
if tests.PY2:
self.assertIsInstance(cmy.thread_id(), long)
else:
self.assertIsInstance(cmy.thread_id(), int)
self.assertGreater(cmy.thread_id(), 0)
thread_id = cmy.thread_id()
cmy.close()
self.assertRaises(MySQLError, cmy.thread_id)
示例13: test_get_host_info
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import connect [as 别名]
def test_get_host_info(self):
config = self.connect_kwargs.copy()
cmy = MySQL(buffered=True)
self.assertRaises(MySQLInterfaceError, cmy.get_host_info)
cmy.connect(**config)
if os.name == 'posix':
# On POSIX systems we would be connected by UNIX socket
self.assertTrue('via UNIX socket' in cmy.get_host_info())
# Connect using TCP/IP
config['unix_socket'] = None
cmy.connect(**config)
self.assertTrue('via TCP/IP' in cmy.get_host_info())
示例14: test_change_user
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import connect [as 别名]
def test_change_user(self):
connect_kwargs = self.connect_kwargs.copy()
connect_kwargs['unix_socket'] = None
connect_kwargs['ssl_disabled'] = False
cmy1 = MySQL(buffered=True)
cmy1.connect(**connect_kwargs)
cmy2 = MySQL(buffered=True)
cmy2.connect(**connect_kwargs)
new_user = {
'user': 'cextuser',
'host': self.config['host'],
'database': self.connect_kwargs['database'],
'password': 'connc',
}
try:
cmy1.query("DROP USER '{user}'@'{host}'".format(**new_user))
except MySQLInterfaceError:
# Probably not created
pass
stmt = ("CREATE USER '{user}'@'{host}' IDENTIFIED WITH "
"caching_sha2_password").format(**new_user)
cmy1.query(stmt)
if tests.MYSQL_VERSION < (8, 0, 5):
cmy1.query("SET old_passwords = 0")
res = cmy1.query("SET PASSWORD FOR '{user}'@'{host}' = "
"PASSWORD('{password}')".format(**new_user))
else:
res = cmy1.query("ALTER USER '{user}'@'{host}' IDENTIFIED BY "
"'{password}'".format(**new_user))
cmy1.query("GRANT ALL ON {database}.* "
"TO '{user}'@'{host}'".format(**new_user))
cmy2.query("SHOW GRANTS FOR {user}@{host}".format(**new_user))
cmy2.query("SELECT USER()")
orig_user = cmy2.fetch_row()[0]
cmy2.free_result()
cmy2.change_user(user=new_user['user'], password=new_user['password'],
database=new_user['database'])
cmy2.query("SELECT USER()")
current_user = cmy2.fetch_row()[0]
self.assertNotEqual(orig_user, current_user)
self.assertTrue(
u"{user}@".format(**new_user) in current_user.decode('utf8'))
cmy2.free_result()
示例15: test_autocommit
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import connect [as 别名]
def test_autocommit(self):
cmy1 = MySQL(buffered=True)
cmy1.connect(**self.connect_kwargs)
cmy2 = MySQL(buffered=True)
cmy2.connect(**self.connect_kwargs)
self.assertRaises(ValueError, cmy1.autocommit, 'ham')
self.assertRaises(ValueError, cmy1.autocommit, 1)
self.assertRaises(ValueError, cmy1.autocommit, None)
table = "autocommit_test"
# For the test we start off by making sure the autocommit is off
# for both sessions
cmy1.query("SELECT @@global.autocommit")
if cmy1.fetch_row()[0] != 1:
cmy1.query("SET @@session.autocommit = 0")
cmy2.query("SET @@session.autocommit = 0")
cmy1.query("DROP TABLE IF EXISTS {0}".format(table))
cmy1.query("CREATE TABLE {0} (c1 INT)".format(table))
# Turn AUTOCOMMIT on
cmy1.autocommit(True)
cmy1.query("INSERT INTO {0} (c1) VALUES "
"(1), (2), (3)".format(table))
cmy2.query("SELECT * FROM {0}".format(table))
self.assertEqual(3, cmy2.num_rows())
rows = fetch_rows(cmy2)
# Turn AUTOCOMMIT off
cmy1.autocommit(False)
cmy1.query("INSERT INTO {0} (c1) VALUES "
"(4), (5), (6)".format(table))
cmy2.query("SELECT * FROM {0} WHERE c1 > 3".format(table))
self.assertEqual([], fetch_rows(cmy2))
cmy1.commit()
cmy2.query("SELECT * FROM {0} WHERE c1 > 3".format(table))
self.assertEqual([(4,), (5,), (6,)], fetch_rows(cmy2))
cmy1.query("DROP TABLE IF EXISTS {0}".format(table))
cmy1.close()
cmy2.close()