Get last row of SQL database and update - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Get last row of SQL database and update (/thread-35262.html) |
Get last row of SQL database and update - Turtle - Oct-14-2021 First time working with a SQL database... I have the following database: +------------+-------------------+--------------+--------------+-----------------+--------------+------------+ | meeting_id | user_id | meeting_date | meeting_time | meeting_name | participants | message_id | +------------+-------------------+--------------+--------------+-----------------+--------------+------------+ | 1 | 95707125543604224 | 10/12/2021 | 8:00 | Interivew | | 8953470 | +------------+-------------------+--------------+--------------+-----------------+--------------+------------+ | 2 | 95707125543604224 | 10/14/2021 | 12:00 | 1 on 1 w/ John | | 5270659 | +------------+-------------------+--------------+--------------+-----------------+--------------+------------+ | 3 | 75123547643604126 | 12/25/2021 | 13:30 | Christmas Lunch | | 5468943 | +------------+-------------------+--------------+--------------+-----------------+--------------+------------+ | 4 | 75123547643604126 | 12/28/2021 | 11:00 | Staff Meeting | | | +------------+-------------------+--------------+--------------+-----------------+--------------+------------+I need to update the message_id and participants field once users start accepting the meeting invite. I'm using the following code to grab the last row based on the unique, sequential meeting_id field: last_meeting = db.execute("SELECT TOP 1 * FROM meetingData ORDER BY meeting_id DESC")I'm grabbing the last row because this will be executed just after the meeting row is created in the database and passed to another function to modify. The output of last_meeting is: My question is how do I use this information now to update the fields for this row?Thanks in advanced. RE: Get last row of SQL database and update - Larz60+ - Oct-14-2021 Try this query: last_meeting = db.execute("SELECT * FROM meetingData ORDER BY meeting_id DESC LIMIT 1;")
RE: Get last row of SQL database and update - buran - Oct-14-2021 You need to fetch the result, e.g. print(result.fetchone())At the moment you print the result Same apply if you use Larz's SQL statement. RE: Get last row of SQL database and update - Turtle - Oct-14-2021 Thanks for the replies. Below is what I've come up with now however I'm getting an error. cursor = db.execute( "SELECT * FROM meetingData ORDER BY meeting_id DESC LIMIT 1;") meeting_entry = await cursor.fetchone() print(meeting_entry[0]) I get this error with both my original query and Larz's suggestion.
RE: Get last row of SQL database and update - buran - Oct-14-2021 @Turtle, I overlooked the fact that you use aiosqlite, which you didn't mention explicitly, but was visible in the output. Anyway, according to docs it should work - there is an example. Anyway, can you show the full traceback and ideally the full minimal reproducible code. The strange thing is, that assuming db is Connection object, db.execute() should return Cursor object, which has .fetchone() method. However according to error it's Result object.This should work, tested with sample дb import aiosqlite import asyncio async def main(): async with aiosqlite.connect('your_db.sqlite') as db: cursor = await db.execute("SELECT * FROM meetingData ORDER BY meeting_id DESC LIMIT 1") row = await cursor.fetchone() print(row) # rows = await cursor.fetchall() # print(rows) # async for row in cursor: # print(row) asyncio.run(main()) RE: Get last row of SQL database and update - Turtle - Oct-14-2021 @buran - Thanks for the reply. Apologies for leaving out the library I was using. I'll be sure to include that in the future as in this case, it was an important factor into the issue. The issue I was having was that I did not await the db.execute() . Once I added in that await statement, it returned and printed as expected.Thanks again! |