How to use the aiomysql.connect function in aiomysql

To help you get started, we’ve selected a few aiomysql examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github aio-libs / aiomysql / tests / sa / test_sa_connection.py View on Github external
async def connect(self, **kwargs):
        conn = await connect(db=self.db,
                             user=self.user,
                             password=self.password,
                             host=self.host,
                             loop=self.loop,
                             port=self.port,
                             **kwargs)
        await conn.autocommit(True)
        cur = await conn.cursor()
        await cur.execute("DROP TABLE IF EXISTS sa_tbl")
        await cur.execute("CREATE TABLE sa_tbl "
                          "(id serial, name varchar(255))")
        await cur.execute("INSERT INTO sa_tbl (name)"
                          "VALUES ('first')")

        await cur._connection.commit()
        # yield from cur.close()
github aio-libs / aiomysql / tests / sa / test_sa_transaction.py View on Github external
async def connect(self, **kwargs):
        conn = await connect(db=self.db,
                             user=self.user,
                             password=self.password,
                             host=self.host,
                             port=self.port,
                             loop=self.loop,
                             **kwargs)
        # TODO: fix this, should autocommit be enabled by default?
        await conn.autocommit(True)
        engine = mock.Mock()
        engine.dialect = sa.engine._dialect

        def release(*args):
            return
        engine.release = release

        ret = sa.SAConnection(conn, engine)
github jettify / aiomysql_replication / tests / base.py View on Github external
def connect_conn_control(self, db):
        if self.conn_control is not None:
            self.conn_control.close()
        self.conn_control = yield from aiomysql.connect(**db)
github codelv / atom-db / tests / test_sql.py View on Github external
from aiomysql.sa import create_engine
        from aiomysql import connect
    elif schema == 'postgres':
        from aiopg.sa import create_engine
        from aiopg import connect
    else:
        raise ValueError("Unsupported database schema: %s" % schema)

    params = dict(
        host=host, port=int(port), user=user, password=pwd, loop=event_loop)

    if schema == 'mysql':
        params['autocommit'] = True

    # Create the DB
    async with connect(**params) as conn:
        async with conn.cursor() as c:
            # WARNING: Not safe
            await c.execute('DROP DATABASE IF EXISTS %s;' % db)
            await c.execute('CREATE DATABASE %s;' % db)

    if schema == 'mysql':
        params['db'] = db
    elif schema == 'postgres':
        params['dbname'] = db

    async with create_engine(**params) as engine:
        mgr = SQLModelManager.instance()
        mgr.database = engine
        yield engine
github aio-libs / aiomysql / tests / test_async_with.py View on Github external
async def test_connect_method(mysql_params, loop):
    async with aiomysql.connect(loop=loop, **mysql_params) as connection:
        async with connection.cursor() as cursor:
            await cursor.execute("SELECT 42")
            value = await cursor.fetchone()
            assert value, (42,)

    assert cursor.closed
    assert connection.closed
github aio-libs / aiomysql / tests / test_issues.py View on Github external
def test_issue_34(self):
        try:
            yield from aiomysql.connect(host="localhost", port=1237,
                                        user="root", loop=self.loop)
            self.fail()
        except aiomysql.OperationalError as e:
            self.assertEqual(2003, e.args[0])
        except Exception:
            self.fail()
github aio-libs / aiomysql / tests / base.py View on Github external
def _connect_all(self):
        conn1 = yield from aiomysql.connect(loop=self.loop, host=self.host,
                                            port=self.port, user=self.user,
                                            db=self.db,
                                            password=self.password,
                                            use_unicode=True, echo=True)
        conn2 = yield from aiomysql.connect(loop=self.loop, host=self.host,
                                            port=self.port, user=self.user,
                                            db=self.other_db,
                                            password=self.password,
                                            use_unicode=False, echo=True)
        conn3 = yield from aiomysql.connect(loop=self.loop, host=self.host,
                                            port=self.port, user=self.user,
                                            db=self.db,
                                            password=self.password,
                                            use_unicode=True, echo=True,
                                            local_infile=True)

        self.connections = [conn1, conn2, conn3]
github aio-libs / aiomysql / examples / example_transaction_oldstyle.py View on Github external
def test_example_transaction():
    conn = yield from aiomysql.connect(host='127.0.0.1', port=3306,
                                       user='root', password='',
                                       db='test_pymysql', autocommit=False,
                                       loop=loop)

    cursor = yield from conn.cursor()
    stmt_drop = "DROP TABLE IF EXISTS names"
    yield from cursor.execute(stmt_drop)
    yield from cursor.execute("""
        CREATE TABLE names (
        id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
        name VARCHAR(30) DEFAULT '' NOT NULL,
        cnt TINYINT UNSIGNED DEFAULT 0,
        PRIMARY KEY (id))""")
    yield from conn.commit()

    # Insert 3 records
github aio-libs / aiomysql / examples / example_callproc_oldstyle.py View on Github external
def test_example():
    conn = yield from aiomysql.connect(host='127.0.0.1', port=3306,
                                       user='root', password='',
                                       db='test_pymysql', loop=loop)

    cur = yield from conn.cursor()
    yield from cur.execute("DROP PROCEDURE IF EXISTS myinc;")
    yield from cur.execute("""CREATE PROCEDURE myinc(p1 INT)
                           BEGIN
                               SELECT p1 + 1;
                           END
                           """)

    yield from cur.callproc('myinc', [1])
    (ret, ) = yield from cur.fetchone()
    assert 2, ret
    print(ret)