2021年3月21日星期日

How to get the data object of a newly inserted data row and flask-mysqldb?

I have work in Perl where I am able to get the newly created data object ID by passing the result back to a variable. For example:

my $data_obj = $schema->resultset('PersonTable')->create(\%psw_rec_hash);  

Where the $data_obj contains the primary key's column value.

I want to be able to do the same thing using Python 3.7, Flask and flask-mysqldb, but without having to do another query. I want to be able to use the specific record's primary key column value for another method.

Python and flask-mysqldb inserts data like so:

query = "INSERT INTO PersonTable (fname, mname, lname) VALUES('Phil','','Vil')  cursor = db.connection.cursor()  cursor.execute(query)  db.connection.commit()  cursor.close()  

The PersonTable has a primary key column called, id. So, the newly inserted data row would look like: 23, 'Phil', 'Vil'

Because there are 22 rows of data before the last inserted data, I don't want to perform a search for the data, because there could be more than one entry with the same data. However, all I want the most recent data row.

Can I do something similar to Perl with python 3.7 and flask-mysqldb?

https://stackoverflow.com/questions/66739365/how-to-get-the-data-object-of-a-newly-inserted-data-row-and-flask-mysqldb March 22, 2021 at 09:45AM

没有评论:

发表评论