0

I am using aiomysql and MariaDB. I can create a table or select data, but I can't insert data into the table. If you SELECT data with fetchall(), then it will show what you just inserted, but immediately delete from the database.

async def test_example(loop):
    pool = await aiomysql.create_pool(host='127.0.0.1', port=3306,
                                      user='root', password='',
                                      db='test', loop=loop)
    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute("INSERT INTO `tbl`(`id`, `val`) VALUES (37, 'z');")
            print(cur.fetchall())
    pool.close()
    await pool.wait_closed()

loop = asyncio.get_event_loop()
loop.run_until_complete(test_example(loop))

Why?

2
  • 2
    What do you expect a fetchall() to return after an insert? Commented Aug 10, 2020 at 13:37
  • Welcome to StackOverflow. Please provide a detailed description of the error Commented Aug 10, 2020 at 13:41

2 Answers 2

1

From the PEP-249 specification:

.fetchall()

Fetch all (remaining) rows of a query result, returning them as a sequence of sequences (e.g. a list of tuples).

Since sql INSERT statement doesn't produce a result set you should try a SELECT statement before trying to obtain information from the database server.

Sign up to request clarification or add additional context in comments.

2 Comments

I know this, the select works, I am interested in the question with insertion into the table
@Dmitriy - There are no results from an INSERT, hence nothing to display. Sure, the table has some results; but you cannot see them without using a SELECT.
-1

Remove the quotes from the table and column names.

import aiomysql
import asyncio


async def select(loop, sql, pool):
    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute(sql)
            r = await cur.fetchone()
            print(r)


async def insert(loop, sql, pool):
    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute(sql)
            await conn.commit()


async def main(loop):
    pool = await aiomysql.create_pool(host='127.0.0.1', port=3306,
                                      user='root', password='',
                                      db='test', loop=loop)
    c1 = select(loop=loop, sql='select * from tbl', pool=pool)
    c2 = insert(loop=loop, sql="INSERT INTO tbl(id, val) VALUES (37, 'z');", pool=pool)

    tasks = [asyncio.ensure_future(c1), asyncio.ensure_future(c2)]
    return await asyncio.gather(*tasks)


if __name__ == '__main__':
    cur_loop = asyncio.get_event_loop()
    cur_loop.run_until_complete(main(cur_loop))

9 Comments

It did not help
I can get data, but I cannot insert it for some reason
@Dmitriy see the updated answer you are missing cur.commit()
and I tried this too, now I have checked all the cursor attributes again, it doesn't have such an attribute. Most likely this is due to the fact that everywhere in StackOverflow answers there is synchronous code.
@Dmitriy use .cursor(prepared=True) to define cur.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.