Skip to content

Latest commit

 

History

History
72 lines (52 loc) · 1.67 KB

README.md

File metadata and controls

72 lines (52 loc) · 1.67 KB

sanic-aiomysql

Downloads Downloads Downloads

setup

install

pip install sanic-aiomysql

or

pip install git+https://github.com/tuna2134/sanic-aiomysql.git

Use

from sanic import Sanic, response
from sanic_mysql import ExtendMySQL

app = Sanic("app")
ExtendMySQL(app, auto=True, user="root", host=127.0.0.1, password="hello", autocommit=True)

@app.get("/")
async def main(request):
    await request.ctx.cursor.execute("CREATE TABLE data(name TEXT, value BIGINT)")
    return response.text("create a table")
    
app.run()

or

from sanic import Sanic, response
from sanic_mysql import ExtendMySQL

app = Sanic("app")
ExtendMySQL(app, user="root", host=127.0.0.1, password="hello", autocommit=True)

@app.get("/")
async def main(request):
    async with request.ctx.pool.acquire() as conn:
        async with conn.cursor() as cursor:
            await cursor.execute("CREATE TABLE data(name TEXT, value BIGINT)")
    return response.text("create a table")
    
app.run()

or

from sanic import Sanic, response
from sanic_mysql import ExtendMySQL, cursor

app = Sanic("app")
ExtendMySQL(app, user="root", host=127.0.0.1, password="hello", autocommit=True)

@app.get("/")
@cursor()
async def main(request, connection, cursor):
    await cursor.execute("CREATE TABLE data(name TEXT, value BIGINT)")
    return response.text("create a table")
    
app.run()