I'm trying to get the rowcount of a database table before and after having inserted a new row into the table. At a basic level I want to take tthe rowcount, insert a row, then take the rowcount again (i.e. it should be 1 more). However, the second call of cursor.rowcount always returns the same number as the first cursor.rowcount call.
My code:
from mysql.connector import MySQLConnection, Error
from single_api_insert import insert
def query_insert():
try:
con = MySQLConnection(host='localhost',
database='test',
user='root',
password='')
if con.is_connected():
print('Connected to database')
cursor = con.cursor()
cursor.execute("select * from testapi")
row = cursor.fetchall()
print(cursor.rowcount)
insert(2, 'mk', 'km')
print(cursor.rowcount)
except Error:
print(Error)
finally:
cursor.close()
con.close()
if __name__ == '__main__':
query_insert()
The insert method inserts a row into my table testapi. With an empty table this should print
Connected to database
0
1
but the rowcount does not increase to 1.
Is there a better way to do this or a way to solve rowcount not working correctly the second time?
Thanks