class Meta: database = MySQLDatabase( 'sku_db', host='account-manager-stage.app.eng.rdu2.redhat.com', password='******', user='******') db_table = 'sku_attributes_all'
def prepare(self): self.my_database = MySQLDatabase(host='127.0.0.1', user='******', passwd='123456', database='task_manage') self.my_database.connect() return super(BaseHandler, self).prepare()
def setup(cls): """sets up the SQL System""" mode = CONFIG["database"]["mode"].value.lower() if mode == "sqlite3": database = SqliteExtDatabase( PROGRAMCONFIGLOCATION + "/Tackem.db", pragmas={ "journal_mode": "wal", "foreign_keys": 0 }, ) elif mode == "mysql": database = MySQLDatabase( CONFIG["database"]["database"].value, user=CONFIG["database"]["username"].value, password=CONFIG["database"]["password"].value, host=CONFIG["database"]["host"].value, port=CONFIG["database"]["port"].value, ) else: print(mode) cls.__db.initialize(database) if mode == "sqlite3": cls.__migrator = SqliteMigrator(cls.__db) elif mode == "mysql": cls.__migrator = MySQLMigrator(cls.__db) TableVersion.create_table()
def init_mysql(settings: dict): keys = {"database", "user", "password", "host", "port"} settings = {k: v for k, v in settings.items() if k in keys} global MySQL_con MySQL_con = pymysql.connect(**settings) db = MySQLDatabase(**settings) return db
def gendb(type, path, host, port, db, user, passwd, tablename=config.DB_TABLE): if type == 'sqlite': db = SqliteDatabase(config.DB_PATH) elif type == 'mysql': if host is None or port is None or user is None or passwd is None: return config.failed db = MySQLDatabase(db, host=host, port=port, user=user, passwd=passwd) elif type == 'postgresql': if host is None or port is None or user is None or passwd is None: return config.failed db = PostgresqlDatabase(db, host=host, port=port, user=user, password=passwd) else: logging.error('Tipo de base de datos no soportado o inválido: %s', type) return config.failed class table(BaseModel): class Meta: database = db table._meta.db_table = tablename return (db, table)
class Setting(object): #path rss_icon_path = "" main_log_path = '' #public config log_enable = True debuging = True persian_timezone_seconds = 0 home_page_news = 0 crawl_interval = 0 log_level=0 #shared objects sid = None log = LogUtil(enable=False) db = {} database = MySQLDatabase(None) def __init__(self, params): ''' Constructor ''' pass
def do_one_task(task): import peewee from peewee import MySQLDatabase db = MySQLDatabase( task["stream"]["database"], **{ 'host': connection["host"], 'password': connection["passwd"], 'port': connection["port"], 'user': connection["user"] }) pk = task["stream"].get("pk") if not pk: pk = {"field": "id", "type": "int"} class MyModel(peewee.Model): _pk = { "char": peewee.CharField(primary_key=True), "int": peewee.IntegerField(primary_key=True) }[pk["type"]] class Meta: database = db setattr(MyModel, pk["field"], MyModel._pk) # 替换 sql 中的 `?` 为上次执行 sql 语句的时间 md5 = hashlib.md5(config.__name__ + task["stream"]["database"] + task["stream"]["sql"]).hexdigest() start_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") last_start_time = r.get(md5) if not last_start_time: if task["stream"].get("init_time"): last_start_time = task["stream"].get("init_time") else: last_start_time = start_time query = MyModel.raw(task["stream"]["sql"].replace("?", "%s"), (last_start_time, )).dicts().iterator() for row in query: for job in task["jobs"]: event = {"action": "insert", "values": row} watched = job.get("watched") or job.get("filters") if watched: func_name, kwargs = watched.items()[0] func = getattr(custom_rowfilters, func_name, None) or getattr(row_filters, func_name) is_ok = func(event, **kwargs) if not is_ok: continue rows = do_pipeline(job["pipeline"], event["values"]) to_dest.make_docs(job["dest"], rows) if len(to_dest.docs) >= to_dest.bulk_size: to_dest.upload_docs() db.close() to_dest.upload_docs() r.set(md5, start_time)
def get_mysql_db(): logger.info('Connecting to Mysql database..') return MySQLDatabase(CONFIG.MYSQL_DB, user=CONFIG.MYSQL_USER, password=CONFIG.MYSQL_PASSWORD, host=CONFIG.MYSQL_SERVER, port=int(CONFIG.MYSQL_PROT))
class Meta: database = MySQLDatabase(testdata_db_cfg['db'], user=testdata_db_cfg['username'], password=testdata_db_cfg['password'], host=testdata_db_cfg['host'], port=3306) table_name = 'tb_testdata'
def get_system_database(): """Returns connection to database received from Config""" database_type = Config.DATABASE_TYPE.lower() logger.debug("Connection to %s database (%s)", database_type, Config.DATABASE_NAME) if database_type == "mysql": port = Config.DATABASE_PORT or 3306 return MySQLDatabase(Config.DATABASE_NAME, host=Config.DATABASE_HOST, user=Config.DATABASE_USER, password=Config.DATABASE_PASSWORD, port=port, charset="utf8mb4") elif database_type == "postgres": port = Config.DATABASE_PORT or 5432 return PostgresqlDatabase(Config.DATABASE_NAME, host=Config.DATABASE_HOST, user=Config.DATABASE_USER, password=Config.DATABASE_PASSWORD, port=port) elif database_type == "sqlite": return SqliteDatabase(Config.DATABASE_NAME) else: raise DatabaseException( "Supports sqlite, postgres or mysql(mariadb) databases, not '{}'". format(database_type))
class ConfigBase(object): """MySQL Database handler""" DATABASE = MySQLDatabase( 'DATABASE', **{ 'charset': 'utf8', 'use_unicode': True, 'user': '******', 'passwd': 'PASSWORD' }) SECRET_KEY = "SECRET KEY" ENV = 'development' # Spotify API ID = "CLIENT ID" SECRET = "SECRET CLIENT ID" # Genius API TOKEN = "TOKEN" LEXICON_SADNESS = [ line.strip() for line in open("./lexicon/sadness.txt", 'r') ] LEXICON_FEAR = [line.strip() for line in open("./lexicon/fear.txt", 'r')] LEXICON_ANGER = [line.strip() for line in open("./lexicon/anger.txt", 'r')] STOP_WORDS = [ line.strip() for line in open("./lexicon/stopwords.txt", 'r') ]
def init_db(): global DB DB = MySQLDatabase(host="ofmc.me", port=3306, user="******", passwd="password", database="cs336")
def db(self): global _MySQLDatabase _MySQLDatabase = MySQLDatabase(self.conf["db"], host=self.conf["host"], user=self.conf["user"], passwd=self.conf["passwd"]) return _MySQLDatabase
def get_mysql(): db = MySQLDatabase('my_app', user='******', password='******', host='localhost', port=3306) return db
def populate_db(mysql_config: Dict[str, str], drop_exist: bool = False) -> None: db = MySQLDatabase(**mysql_config) db.bind(MODELS, bind_refs=False, bind_backrefs=False) db.connect() if drop_exist: db.drop_tables(MODELS) db.create_tables(MODELS)
def create_database(instrument_host, credentials): if not credentials: username, password = get_credentials(CREDS_GROUP, "ExpDatabaseWrite") else: username, password = credentials return MySQLDatabase("exp_data", user=username, password=password, host=instrument_host)
class ConfigBase(object): """MySQL Database handler""" DATABASE = MySQLDatabase( '<database>', **{ 'charset': 'utf8', 'use_unicode': True, 'user': '******', 'passwd': '<password>' })
def create_db_interface(**mysql_kwargs): """Create MySQL database interface using given connection parameters. `mysql_kwargs` are forwarded to `pymysql.connect`. Configure primary keys to be unsigned integer. """ return MySQLDatabase(**mysql_kwargs, field_types={ "AUTO": "INTEGER UNSIGNED AUTO_INCREMENT" })
def __init_mysql(dbconf): db = MySQLDatabase(database=dbconf['name'], host=dbconf['host'], port=dbconf['port'], user=dbconf['user'], passwd=dbconf['password']) DbConnection.__instance.val = db return db
def Conn(self): return MySQLDatabase( host=cfg.get("mysql", "server") ,port=int(cfg.get("mysql", "port")) ,database=cfg.get("mysql", "database") , user=cfg.get("mysql", "user") , passwd=cfg.get("mysql", "passwd") , charset='utf8' )
def create_client(self, **kwargs): client = MySQLDatabase(self.mysql_config.dbname, host=self.mysql_config.host, user=self.mysql_config.username, port=self.mysql_config.port, passwd=self.mysql_config.password, **kwargs) logger.debug("create mysql client:{}".format(client)) return client
class Meta(object): """表配置信息 """ database = MySQLDatabase(database="xyz", host="127.0.0.1", password="******", user="******", port=3306) db_table = "tb_raw"
def __connect(self): self.oDbBase = MySQLDatabase( 'chatterbot', **{ 'host': '192.168.241.45', 'password': '******', 'port': 3306, 'user': '******', 'charset': 'utf8' })
class Setting(object): #path rss_icon_path = "" main_log_path = '' #public config log_enable = True debuging = True persian_timezone_seconds = 0 home_page_news = 0 crawl_interval = 0 log_level=0 #shared objects sid = None log = LogUtil(enable=False) db = {} database = MySQLDatabase(None) def __init__(self, params): pass def init_setting(app_info): mode = app_info.get('app:main','mode') Setting.db['host'] = app_info.get('yeksatr:config','db_host') Setting.db['db_name'] = app_info.get('yeksatr:config','db_name') Setting.db['user'] = app_info.get('yeksatr:config','db_user') Setting.db['pass'] = app_info.get('yeksatr:config','db_pass') Setting.mode = mode Setting.rss_icon_path = app_info.get('yeksatr:config','rss_icon_path') Setting.main_log_path = app_info.get('yeksatr:config','main_log_path') #public config Setting.log_enable = app_info.getboolean('yeksatr:config','log_enable') Setting.debuging = app_info.getboolean('yeksatr:config','debuging') Setting.persian_timezone_seconds =app_info.getint('yeksatr:config','persian_timezone_seconds') Setting.home_page_news = app_info.getint('yeksatr:config','home_page_news') Setting.crawl_interval = app_info.getint('yeksatr:config','crawl_interval') Setting.log_level = app_info.getint('yeksatr:config','log_level') Setting.log = LogUtil(Setting.main_log_path,Setting.log_level,Setting.log_enable) def init_db(): try: try: Setting.database.close() except Exception as e: Setting.log.log_exception(e,' error in closing previus connection (Setting.init_db): ') pass Setting.database.init(Setting.db['db_name'], host=Setting.db['host'],passwd = Setting.db['pass'], user = Setting.db['user']) except Exception as ex: Setting.log.log_exception(ex,' error in connecting to database: ') def test(request,a): print(request,a)
def _init_database(self, database, database_type_str, database_options): # Will raise ValueError if database type is not supported database_type = Databases(database_type_str) if database_type == Databases.SQLITE: return SqliteDatabase(database, **(database_options or {})) elif database_type == Databases.MYSQL: return MySQLDatabase(database, **(database_options or {})) elif database_type == Databases.POSTGRES: return PostgresqlDatabase(database, **(database_options or {}))
def connect(): db = MySQLDatabase( host='localhost', user='******', password='******', database='guestbook' ) db.connect() return db
def connect(self): db = MySQLDatabase('hlook', host=self.args.host, port=self.args.port, user=self.args.username, passwd=self.args.password) db.connect() databaseProxy.initialize(db) print('Connection opened.') return db
def get_db(cls): if cls.db is None: cls.db = MySQLDatabase(conf.DATABASE_NAME, host=conf.LOCAL_HOST, port=conf.DATABASE_PORT, user=conf.LOGIN_USERNAME, password=conf.LOGIN_PASSWORD, charset=conf.DATABASE_CHARSET) cls.db.connect() return cls.db
def __connect(self): if self.dbname == 'dm_base': self.oDbBase = MySQLDatabase( 'dm_base', **{ 'host': '192.168.241.45', 'password': '******', 'port': 3306, 'user': '******', 'charset': 'utf8' }) if self.dbname == 'cognitive_phrases': self.oDbBase = MySQLDatabase( 'cognitive_phrases', **{ 'host': '192.168.241.45', 'password': '******', 'port': 3306, 'user': '******', 'charset': 'utf8' })
def get_conn(self): conn = MySQLDatabase( database=mysql['name'], host=mysql['host'], password=mysql['password'], min_connections=mysql['min_connections'], max_connections=mysql['max_connections'], port=mysql['port'], user=mysql['user']) return conn