本文整理汇总了Python中pymysql.install_as_MySQLdb方法的典型用法代码示例。如果您正苦于以下问题:Python pymysql.install_as_MySQLdb方法的具体用法?Python pymysql.install_as_MySQLdb怎么用?Python pymysql.install_as_MySQLdb使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymysql
的用法示例。
在下文中一共展示了pymysql.install_as_MySQLdb方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import pymysql [as 别名]
# 或者: from pymysql import install_as_MySQLdb [as 别名]
def __init__(self, dialect, database, username=None, password=None,
host=None, port=None, query=None):
"""
Accepts configuration for a database connection, and defines SQLAlchemy
ORM objects for all the tables in the database.
"""
# Prepare environment for pymysql
pymysql.install_as_MySQLdb()
pymysql.converters.encoders[np.float64] = pymysql.converters.escape_float
pymysql.converters.conversions = pymysql.converters.encoders.copy()
pymysql.converters.conversions.update(pymysql.converters.decoders)
db_url = URL(drivername=dialect, database=database, username=username,
password=password, host=host, port=port, query=query)
self.engine = create_engine(db_url)
self.session = None
self.get_session = sessionmaker(bind=self.engine,
expire_on_commit=False)
# create ORM objects for the tables
self._define_tables()
示例2: setup_mysql
# 需要导入模块: import pymysql [as 别名]
# 或者: from pymysql import install_as_MySQLdb [as 别名]
def setup_mysql(username, password, host, port):
"""
ModuleNotFoundError: No module named 'MySQLdb'
pip install mysqlclient
Python 2: pip install mysqlclient -> MySQLdb/_mysql.c(29) :
fatal error C1083: Cannot open include file: 'mysql.h': No such file or directory
https://stackoverflow.com/questions/51294268/pip-install-mysqlclient-returns-fatal-error-c1083-cannot-open-file-mysql-h
https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient
pip install "path to the downloaded mysqlclient.whl file"
"""
require_version = '0.9.3' # Dec 18, 2018
install_command = "pip install --upgrade pymysql"
try:
import pymysql
assert pymysql.__version__ >= require_version, install_command
except (ImportError, AssertionError):
sys.exit("Run command: %s" % install_command)
else:
# Run scrapydweb: ModuleNotFoundError: No module named 'MySQLdb'
pymysql.install_as_MySQLdb()
conn = pymysql.connect(host=host, port=int(port), user=username, password=password,
charset='utf8', cursorclass=pymysql.cursors.DictCursor)
cur = conn.cursor()
for dbname in DBS:
if SCRAPYDWEB_TESTMODE:
drop_database(cur, dbname)
# pymysql.err.ProgrammingError: (1007, "Can't create database 'scrapydweb_apscheduler'; database exists")
# cur.execute("CREATE DATABASE IF NOT EXISTS %s CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'" % dbname)
try:
cur.execute("CREATE DATABASE %s CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'" % dbname)
except Exception as err:
if 'exists' in str(err):
pass
else:
raise
cur.close()
conn.close()