本文整理汇总了Python中MySQLdb.ProgrammingError方法的典型用法代码示例。如果您正苦于以下问题:Python MySQLdb.ProgrammingError方法的具体用法?Python MySQLdb.ProgrammingError怎么用?Python MySQLdb.ProgrammingError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MySQLdb
的用法示例。
在下文中一共展示了MySQLdb.ProgrammingError方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import ProgrammingError [as 别名]
def setUp(self):
_skip_if_no_MySQLdb()
import MySQLdb
try:
# Try Travis defaults.
# No real user should allow root access with a blank password.
self.db = MySQLdb.connect(host='localhost', user='root', passwd='',
db='pandas_nosetest')
except:
pass
else:
return
try:
self.db = MySQLdb.connect(read_default_group='pandas')
except MySQLdb.ProgrammingError as e:
raise nose.SkipTest(
"Create a group of connection parameters under the heading "
"[pandas] in your system's mysql default file, "
"typically located at ~/.my.cnf or /etc/.my.cnf. ")
except MySQLdb.Error as e:
raise nose.SkipTest(
"Cannot connect to database. "
"Create a group of connection parameters under the heading "
"[pandas] in your system's mysql default file, "
"typically located at ~/.my.cnf or /etc/.my.cnf. ")
示例2: test_tquery
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import ProgrammingError [as 别名]
def test_tquery(self):
try:
import MySQLdb
except ImportError:
raise nose.SkipTest("no MySQLdb")
frame = tm.makeTimeDataFrame()
drop_sql = "DROP TABLE IF EXISTS test_table"
cur = self.db.cursor()
cur.execute(drop_sql)
sql.write_frame(frame, name='test_table', con=self.db, flavor='mysql')
result = sql.tquery("select A from test_table", self.db)
expected = frame.A
result = Series(result, frame.index)
tm.assert_series_equal(result, expected)
try:
sys.stdout = StringIO()
self.assertRaises(MySQLdb.ProgrammingError, sql.tquery,
'select * from blah', con=self.db)
self.assertRaises(MySQLdb.ProgrammingError, sql.tquery,
'select * from blah', con=self.db, retry=True)
finally:
sys.stdout = sys.__stdout__
示例3: test_uquery
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import ProgrammingError [as 别名]
def test_uquery(self):
try:
import MySQLdb
except ImportError:
raise nose.SkipTest("no MySQLdb")
frame = tm.makeTimeDataFrame()
drop_sql = "DROP TABLE IF EXISTS test_table"
cur = self.db.cursor()
cur.execute(drop_sql)
sql.write_frame(frame, name='test_table', con=self.db, flavor='mysql')
stmt = 'INSERT INTO test_table VALUES(2.314, -123.1, 1.234, 2.3)'
self.assertEqual(sql.uquery(stmt, con=self.db), 1)
try:
sys.stdout = StringIO()
self.assertRaises(MySQLdb.ProgrammingError, sql.tquery,
'insert into blah values (1)', con=self.db)
self.assertRaises(MySQLdb.ProgrammingError, sql.tquery,
'insert into blah values (1)', con=self.db,
retry=True)
finally:
sys.stdout = sys.__stdout__
示例4: set_db_data
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import ProgrammingError [as 别名]
def set_db_data(db_call, values):
"""
Enter data into DB
"""
db = MySQLdb.connect(host=DB_HOST, port=3306, user=DB_USER, passwd=DB_PW, db=DB_SCHEMA, use_unicode=True,
charset="utf8mb4")
try:
db_cursor = db.cursor()
db_cursor.execute(db_call, values)
db.commit()
db_cursor.close()
db.close()
logger.info("{}: record inserted into DB".format(datetime.now()))
return None
except MySQLdb.ProgrammingError as e:
logger.info("{}: Exception entering data into database".format(datetime.now()))
logger.info("{}: {}".format(datetime.now(), e))
return e
示例5: check_row_exists
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import ProgrammingError [as 别名]
def check_row_exists(cursor, table, identifiers):
"""
check a specified row only for existence
:param cursor: cursor object able to execute queries
:param table: name of the table to look into
:param identifiers:
:return: True if the row exists, False otherwise
"""
query = "select exists(select 1 from {table} where {values})".format(
table=table,
values=" AND ".join(generate_where_segment(identifiers.items())),
)
try:
res = cursor.execute(query)
except mysql_driver.ProgrammingError as e:
(errcode, message) = e.args
if errcode == 1146:
return ERR_NO_SUCH_TABLE
else:
raise e
exists, = cursor.fetchone()
return exists == 1
示例6: maybe_create_tables
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import ProgrammingError [as 别名]
def maybe_create_tables(self):
try:
self.db.get("SELECT COUNT(*) from entries;")
except MySQLdb.ProgrammingError:
subprocess.check_call(['mysql',
'--host=' + options.mysql_host,
'--database=' + options.mysql_database,
'--user=' + options.mysql_user,
'--password=' + options.mysql_password],
stdin=open('schema.sql'))
示例7: show_create_table
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import ProgrammingError [as 别名]
def show_create_table(instance, db, table, standardize=True):
""" Get a standardized CREATE TABLE statement
Args:
instance - a hostAddr object
db - the MySQL database to run against
table - the table on the db database to run against
standardize - Remove AUTO_INCREMENT=$NUM and similar
Returns:
A string of the CREATE TABLE statement
"""
conn = connect_mysql(instance)
cursor = conn.cursor()
try:
cursor.execute('SHOW CREATE TABLE `{db}`.`{table}`'.format(table=table,
db=db))
ret = cursor.fetchone()['Create Table']
if standardize is True:
ret = re.sub('AUTO_INCREMENT=[0-9]+ ', '', ret)
except MySQLdb.ProgrammingError as detail:
(error_code, msg) = detail.args
if error_code != MYSQL_ERROR_NO_SUCH_TABLE:
raise
ret = ''
return ret
示例8: test_bug_2671682
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import ProgrammingError [as 别名]
def test_bug_2671682(self):
from MySQLdb.constants import ER
try:
self.cursor.execute("describe some_non_existent_table")
except self.connection.ProgrammingError as msg:
self.assertTrue(str(ER.NO_SUCH_TABLE) in str(msg))
示例9: test_reraise_exception
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import ProgrammingError [as 别名]
def test_reraise_exception(self):
c = self.cursor
try:
c.execute("SELECT x FROM not_existing_table")
except MySQLdb.ProgrammingError as e:
self.assertEqual(e.args[0], 1146)
return
self.fail("Should raise ProgrammingError")
示例10: _crawl
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import ProgrammingError [as 别名]
def _crawl(self, mid, cur):
"""
抓取并持久化用户信息
:param mid: B站用户id
:param cur: mysql游标
:return: None
"""
if self._is_member_exist(cur, mid):
print(f'数据库中已存在此用户mid:{mid}, 忽略')
return
member_info = self._get_member_by_mid(mid)
if member_info is None:
return
mid = member_info['mid']
name = member_info['name']
sign = member_info['sign'].replace("'", "\\\'")
rank = member_info['rank']
level = member_info['level']
jointime = member_info['jointime']
moral = member_info['moral']
silence = member_info['silence']
birthday = member_info['birthday']
coins = member_info['coins']
fans_badge = member_info['fans_badge']
vip_type = member_info['vip']['type']
vip_status = member_info['vip']['status']
try:
cur.execute(f"INSERT INTO bilibili_member "
f"(mid, name, sign, `rank`, `level`, jointime, moral, silence, birthday, coins, fans_badge, vip_type, vip_status) "
f"VALUES "
f"({mid}, '{name}', '{sign}', {rank}, {level}, {jointime}, {moral}, {silence}, '{birthday}', "
f"{coins}, {fans_badge}, {vip_type}, {vip_status})"
)
print(f'成功插入用户数据: {mid}, 当前代理:{self.cur_proxy["https"]}')
except MySQLdb.ProgrammingError as e:
print(f'插入用户: {mid} 数据出错:{e}')
raise SqlInsertException(str(e))
except MySQLdb.IntegrityError:
print(f'用户: {mid} 数据已存在,不作插入')
raise SqlAlreadyExistsException('数据已存在')
示例11: _insert_failure_record
# 需要导入模块: import MySQLdb [as 别名]
# 或者: from MySQLdb import ProgrammingError [as 别名]
def _insert_failure_record(cur, mid, state, remark):
remark = remark.replace("'", "\\\'")
try:
cur.execute(
"INSERT INTO failure_record (mid, remark, state) "
f"VALUES ({mid}, '{remark}', '{state}')"
)
except MySQLdb.ProgrammingError as e:
print(f'插入失败日志: {mid} 数据出错:{e}')
except MySQLdb.IntegrityError:
print(f'失败日志: {mid} 数据已存在,不作插入')