I
I
Ivan Yakushenko2019-07-30 12:18:12
Python
Ivan Yakushenko, 2019-07-30 12:18:12

How to get the result of executing a SQL query in Python?

For example, there is a sql query:

INSERT INTO categories(category, sub_category) 
    SELECT category, sub_category 
    FROM (SELECT "category" as category, "Final" as sub_category) temp 
    WHERE NOT EXISTS (SELECT 1 FROM categories WHERE categories.category = temp.category AND categories.sub_category = temp.sub_category)

Which is done like this:
async with aiosqlite.connect('categories.db') as db:
    await db.execute(sql)
    await db.commit()

How can I get the status of its execution?
Like this:
Result: Request completed successfully. Took 1ms, 1 lines changed

Here it doesn't work:
print(await db.execute(sql))
print(await db.commit)
status = await db.fetch()
print(status)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Yakushenko, 2019-07-30
@kshnkvn

Judging by the office. there is no documentation for such a function and the only way is something like this:

if db.total_changes > 0:
    print('Insertion category {} with sub_category {} complete'.format(value, categories[index]))

Or I'm wrong?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question