本文整理匯總了Python中peewee.MySQLDatabase方法的典型用法代碼示例。如果您正苦於以下問題:Python peewee.MySQLDatabase方法的具體用法?Python peewee.MySQLDatabase怎麽用?Python peewee.MySQLDatabase使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類peewee
的用法示例。
在下文中一共展示了peewee.MySQLDatabase方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_db_conn
# 需要導入模塊: import peewee [as 別名]
# 或者: from peewee import MySQLDatabase [as 別名]
def get_db_conn():
import peewee
env = os.environ.get('SENTINEL_ENV', 'production')
# default values should be used unless you need a different config for development
db_host = sentinel_cfg.get('db_host', '127.0.0.1')
db_port = sentinel_cfg.get('db_port', None)
db_name = sentinel_cfg.get('db_name', 'sentinel')
db_user = sentinel_cfg.get('db_user', 'sentinel')
db_password = sentinel_cfg.get('db_password', 'sentinel')
db_charset = sentinel_cfg.get('db_charset', 'utf8mb4')
db_driver = sentinel_cfg.get('db_driver', 'sqlite')
if (env == 'test'):
if db_driver == 'sqlite':
db_name = sqlite_test_db_name(db_name)
else:
db_name = "%s_test" % db_name
peewee_drivers = {
'mysql': peewee.MySQLDatabase,
'postgres': peewee.PostgresqlDatabase,
'sqlite': peewee.SqliteDatabase,
}
driver = peewee_drivers.get(db_driver)
dbpfn = 'passwd' if db_driver == 'mysql' else 'password'
db_conn = {
'host': db_host,
'user': db_user,
dbpfn: db_password,
}
if db_port:
db_conn['port'] = int(db_port)
if driver == peewee.SqliteDatabase:
db_conn = {}
db = driver(db_name, **db_conn)
return db
示例2: init_mysql
# 需要導入模塊: import peewee [as 別名]
# 或者: from peewee import MySQLDatabase [as 別名]
def init_mysql(url):
global DB
db_name = url.path.strip("/")
DB.initialize(MySQLDatabase(
database=db_name,
user=url.user or '',
password=url.password or '',
host=url.host,
autocommit=bool(url.get('autocommit', True)),
autorollback=bool(url.get('autorollback', True))
))
log.info("Database initialized as '%s'. Checking migrations...", db_name)
return DB, MySQLMigrator(DB)
示例3: get_database
# 需要導入模塊: import peewee [as 別名]
# 或者: from peewee import MySQLDatabase [as 別名]
def get_database(self):
"""Returns the correct database driver
Returns:
:obj:`pw.Database`
Raises:
AttributeError: if `--username` or `--password` not passed
(if `--dbtype [postgres|mysql]`)
"""
if self.get('dbtype') == 'sqlite':
return pw.SqliteDatabase(self.get('dbname'),
pragmas={'journal_mode': 'wal',
'cache_size': -1 * 64000})
else:
if self.get('username') is None or self.get('password') is None:
raise AttributeError('[ERROR] dbtype %s requires username and'
' password.\n' % str(self.get('dbtype')))
if self.get('hostname') is None:
self.set('hostname', 'localhost')
if self.get('dbtype') == 'mysql':
if self.get('port') is None or self.get('port') == '':
self.set('port', str(3306))
return pw.MySQLDatabase(
self.get('dbname'),
user=self.get('username'),
password=self.get('password'),
host=self.get('hostname'),
port=int(self.get('port')))
elif self.get('dbtype') == 'postgres':
if self.get('port') is None or self.get('port') == '':
self.set('port', str(5432))
return pw.PostgresqlDatabase(
self.get('dbname'),
user=self.get('username'),
password=self.get('password'),
host=self.get('hostname'),
port=int(self.get('port')))
示例4: _swap_database
# 需要導入模塊: import peewee [as 別名]
# 或者: from peewee import MySQLDatabase [as 別名]
def _swap_database(self, query):
"""Swap database for query if swappable. Return **new query**
with swapped database.
This is experimental feature which allows us to have multiple
managers configured against different databases for single model
definition.
The essential limitation though is that database backend have
to be **the same type** for model and manager!
"""
database = _query_db(query)
if database == self.database:
return query
if self._subclassed(peewee.PostgresqlDatabase, database,
self.database):
can_swap = True
elif self._subclassed(peewee.MySQLDatabase, database,
self.database):
can_swap = True
else:
can_swap = False
if can_swap:
# **Experimental** database swapping!
query = query.clone()
query._database = self.database
return query
assert False, (
"Error, query's database and manager's database are "
"different. Query: %s Manager: %s" % (database, self.database)
)
return None
示例5: is_mysql
# 需要導入模塊: import peewee [as 別名]
# 或者: from peewee import MySQLDatabase [as 別名]
def is_mysql(db):
return isinstance(db, pw.MySQLDatabase)
示例6: setUp
# 需要導入模塊: import peewee [as 別名]
# 或者: from peewee import MySQLDatabase [as 別名]
def setUp(self):
os.system('echo "create database peeweedbevolve_test;" | mysql')
self.db = pw.MySQLDatabase('peeweedbevolve_test')
self.db.connect()
peeweedbevolve.clear()
示例7: __get_migrator
# 需要導入模塊: import peewee [as 別名]
# 或者: from peewee import MySQLDatabase [as 別名]
def __get_migrator(self):
if isinstance(self.db.engine, peewee.SqliteDatabase) or isinstance(self.db.engine, SqliteExtDatabase):
return SqliteMigrator(self.db.engine)
elif isinstance(self.db.engine, peewee.MySQLDatabase):
return MySQLMigrator(self.db.engine)
elif isinstance(self.db.engine, peewee.PostgresqlDatabase):
return PostgresqlMigrator(self.db.engine)
raise ImproperlyConfigured('Database engine doesn\'t support Migrations!')
示例8: __init__
# 需要導入模塊: import peewee [as 別名]
# 或者: from peewee import MySQLDatabase [as 別名]
def __init__(self, table_prefix=None, use_async=True, create_table=True, multiplexing_conn=False):
"""
初始化
Parameters:
table_prefix - 表名前綴,由掃描目標的 host + "_" + str(port) 組成
use_async - 是否開啟數據庫連接的異步查詢功能,默認為True
create_table - 數據表不存在時是否創建,默認為True
multiplexing_conn - 是否複用連接,為True時,相同的Model的實例會使用同一個連接, 為int時使用該int大小的連接池, 默認為False
Raises:
create_table為Fasle且目標數據表不存在時,引發exceptions.TableNotExist
"""
self.use_async = use_async
try:
if multiplexing_conn:
database = BaseModel.mul_database
else:
if self.use_async:
database = peewee_async.MySQLDatabase(**self.connect_para)
else:
database = peewee.MySQLDatabase(**self.connect_para)
database.connect()
# table_prefix 為None則不建立數據表實例,僅用於調用基類方法
if table_prefix is not None:
self._model = self._create_model(database, table_prefix)
if not self._model.table_exists():
if create_table:
try:
database.create_tables([self._model])
Logger().debug("Create table {}_{}".format(table_prefix, self.__class__.__name__))
if self.__class__.__name__ == "NewRequestModel":
Communicator().update_target_list_status()
except peewee.InternalError:
pass
else:
raise exceptions.TableNotExist
self.database = database
except exceptions.TableNotExist as e:
self._handle_exception("Table with prefix {} not found!".format(table_prefix), e)
except Exception as e:
self._handle_exception("Mysql Connection Fail!", e)