本文整理匯總了Python中pymysql.Warning方法的典型用法代碼示例。如果您正苦於以下問題:Python pymysql.Warning方法的具體用法?Python pymysql.Warning怎麽用?Python pymysql.Warning使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pymysql
的用法示例。
在下文中一共展示了pymysql.Warning方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_load_warnings
# 需要導入模塊: import pymysql [as 別名]
# 或者: from pymysql import Warning [as 別名]
def test_load_warnings(self):
"""Test load local infile produces the appropriate warnings"""
conn = self.connections[0]
c = conn.cursor()
c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)")
filename = os.path.join(os.path.dirname(os.path.realpath(__file__)),
'data',
'load_local_warn_data.txt')
try:
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
c.execute(
("LOAD DATA LOCAL INFILE '{0}' INTO TABLE " +
"test_load_local FIELDS TERMINATED BY ','").format(filename)
)
self.assertEqual(w[0].category, Warning)
expected_message = "Incorrect integer value"
if expected_message not in str(w[-1].message):
self.fail("%r not in %r" % (expected_message, w[-1].message))
finally:
c.execute("DROP TABLE test_load_local")
c.close()
示例2: test_issue_491
# 需要導入模塊: import pymysql [as 別名]
# 或者: from pymysql import Warning [as 別名]
def test_issue_491(self):
""" Test warning propagation """
conn = pymysql.connect(charset="utf8", **self.databases[0])
with warnings.catch_warnings():
# Ignore all warnings other than pymysql generated ones
warnings.simplefilter("ignore")
warnings.simplefilter("error", category=pymysql.Warning)
# verify for both buffered and unbuffered cursor types
for cursor_class in (cursors.Cursor, cursors.SSCursor):
c = conn.cursor(cursor_class)
try:
c.execute("SELECT CAST('124b' AS SIGNED)")
c.fetchall()
except pymysql.Warning as e:
# Warnings should have errorcode and string message, just like exceptions
self.assertEqual(len(e.args), 2)
self.assertEqual(e.args[0], 1292)
self.assertTrue(isinstance(e.args[1], text_type))
else:
self.fail("Should raise Warning")
finally:
c.close()
示例3: test_load_warnings
# 需要導入模塊: import pymysql [as 別名]
# 或者: from pymysql import Warning [as 別名]
def test_load_warnings(self):
"""Test load local infile produces the appropriate warnings"""
conn = self.connections[0]
c = conn.cursor()
c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)")
filename = os.path.join(os.path.dirname(os.path.realpath(__file__)),
'data',
'load_local_warn_data.txt')
try:
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
c.execute(
("LOAD DATA LOCAL INFILE '{0}' INTO TABLE " +
"test_load_local FIELDS TERMINATED BY ','").format(filename)
)
self.assertEqual(w[0].category, Warning)
self.assertTrue("Incorrect integer value" in str(w[-1].message))
finally:
c.execute("DROP TABLE test_load_local")
示例4: test_load_warnings
# 需要導入模塊: import pymysql [as 別名]
# 或者: from pymysql import Warning [as 別名]
def test_load_warnings(self):
"""Test load local infile produces the appropriate warnings"""
conn = self.connect()
c = conn.cursor()
c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)")
filename = os.path.join(os.path.dirname(os.path.realpath(__file__)),
'data',
'load_local_warn_data.txt')
try:
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
c.execute(
("LOAD DATA LOCAL INFILE '{0}' INTO TABLE " +
"test_load_local FIELDS TERMINATED BY ','").format(filename)
)
self.assertEqual(w[0].category, Warning)
expected_message = "Incorrect integer value"
if expected_message not in str(w[-1].message):
self.fail("%r not in %r" % (expected_message, w[-1].message))
finally:
c.execute("DROP TABLE test_load_local")
c.close()
示例5: test_issue_363
# 需要導入模塊: import pymysql [as 別名]
# 或者: from pymysql import Warning [as 別名]
def test_issue_363(self):
""" Test binary / geometry types. """
conn = pymysql.connect(charset="utf8", **self.databases[0])
self.safe_create_table(
conn, "issue363",
"CREATE TABLE issue363 ( "
"id INTEGER PRIMARY KEY, geom LINESTRING NOT NULL, "
"SPATIAL KEY geom (geom)) "
"ENGINE=MyISAM default charset=utf8")
cur = conn.cursor()
query = ("INSERT INTO issue363 (id, geom) VALUES"
"(1998, GeomFromText('LINESTRING(1.1 1.1,2.2 2.2)'))")
# From MySQL 5.7, ST_GeomFromText is added and GeomFromText is deprecated.
if self.mysql_server_is(conn, (5, 7, 0)):
with self.assertWarns(pymysql.err.Warning) as cm:
cur.execute(query)
else:
cur.execute(query)
# select WKT
query = "SELECT AsText(geom) FROM issue363"
if self.mysql_server_is(conn, (5, 7, 0)):
with self.assertWarns(pymysql.err.Warning) as cm:
cur.execute(query)
else:
cur.execute(query)
row = cur.fetchone()
self.assertEqual(row, ("LINESTRING(1.1 1.1,2.2 2.2)", ))
# select WKB
query = "SELECT AsBinary(geom) FROM issue363"
if self.mysql_server_is(conn, (5, 7, 0)):
with self.assertWarns(pymysql.err.Warning) as cm:
cur.execute(query)
else:
cur.execute(query)
row = cur.fetchone()
self.assertEqual(row,
(b"\x01\x02\x00\x00\x00\x02\x00\x00\x00"
b"\x9a\x99\x99\x99\x99\x99\xf1?"
b"\x9a\x99\x99\x99\x99\x99\xf1?"
b"\x9a\x99\x99\x99\x99\x99\x01@"
b"\x9a\x99\x99\x99\x99\x99\x01@", ))
# select internal binary
cur.execute("SELECT geom FROM issue363")
row = cur.fetchone()
# don't assert the exact internal binary value, as it could
# vary across implementations
self.assertTrue(isinstance(row[0], bytes))
示例6: driver_path
# 需要導入模塊: import pymysql [as 別名]
# 或者: from pymysql import Warning [as 別名]
def driver_path(provider, tmpdir, mysql_server):
"""Get a valid, uninitialized driver path for given provider"""
import random
import string
from urllib.parse import urlparse
def validate_con_info(con_info):
return (con_info.scheme == 'mysql'
and con_info.hostname
and con_info.username
and not con_info.path)
def random_string(length):
return ''.join(random.choices(string.ascii_uppercase, k=length))
if provider == 'sqlite':
dbfile = tmpdir.join('test.sqlite')
yield str(dbfile)
elif provider == 'mysql':
if not mysql_server:
return pytest.skip('mysql_server argument not given')
if not mysql_server.startswith('mysql://'):
mysql_server = f'mysql://{mysql_server}'
con_info = urlparse(mysql_server)
if not validate_con_info(con_info):
raise ValueError('invalid value for mysql_server')
dbpath = random_string(24)
import pymysql
try:
with pymysql.connect(con_info.hostname, user=con_info.username,
password=con_info.password) as con:
pass
except pymysql.OperationalError as exc:
raise RuntimeError('error connecting to MySQL server') from exc
try:
yield f'{mysql_server}/{dbpath}'
finally: # cleanup
with pymysql.connect(con_info.hostname, user=con_info.username,
password=con_info.password) as con:
try:
con.execute(f'DROP DATABASE IF EXISTS {dbpath}')
except pymysql.Warning:
pass
else:
return NotImplementedError(f'unknown provider {provider}')