my_cursor.execute("INSERT INTO `my_tutorial`.`student` (`id` ,`name` ,`class` ,`mark` ,`gender`) \
VALUES ('36', 'King', 'Five', '45', 'male')")
print("Rows Added = ",my_cursor.rowcount)
my_connect.commit() # record added
my_connect.close()
Output
Rows Added = 1
We used rowcount to get the number of records added to our student table. my_cursor = my_connect.cursor() # my_connect is the connection
try:
my_cursor.execute("INSERT INTO `my_tutorial`.`student`(`id`,`name`,`class1`,`mark`,`gender`) \
VALUES ('36', 'King', 'Five', '45', 'male')")
print("Rows Added = ",my_cursor.rowcount)
my_connect.commit() # record added
except mysql.connector.Error as my_error:
print(my_error)
my_connect.close()
Output is here
1054 (42S22): Unknown column 'class1' in 'field list'
my_cursor = my_connect.cursor() # my_connect is the connection
try:
query="INSERT INTO `my_tutorial`.`student` (`id` ,`name` ,`class` ,`mark` ,`gender`) \
VALUES(%s,%s,%s,%s,%s)"
my_data=(36,'King','Five',45,'male')
my_cursor.execute(query,my_data)
print("Rows Added = ",my_cursor.rowcount)
my_connect.commit() # record added
except mysql.connector.Error as my_error:
print(my_error)
my_connect.close()
executemany()
. Here is the code.
my_cursor = my_connect.cursor() # my_connect is the connection
try:
query="INSERT INTO `my_tutorial`.`student` (`id` ,`name` ,`class` ,`mark` ,`gender`) \
VALUES(%s,%s,%s,%s,%s)"
my_data=[(36,'King','Five',45,'male'),
(37,'Queen','Four',44,'Female'),
(38,'Jack','Three',42,'male')]
my_cursor.executemany(query,my_data)
print("Rows Added = ",my_cursor.rowcount)
my_connect.commit() # records added
except mysql.connector.Error as my_error:
print(my_error)
my_connect.close()
Output is here
Rows Added = 3
my_cursor = my_connect.cursor() # my_connect is the connection
try:
query="INSERT INTO `my_tutorial`.`student` (`name` ,`class` ,`mark` ,`gender`) \
VALUES(%s,%s,%s,%s)"
my_data=('King','Five',45,'male')
my_cursor.execute(query,my_data)
print("Rows Added = ",my_cursor.rowcount)
print("last row id = ",my_cursor.lastrowid)
my_connect.commit() # record added
except mysql.connector.Error as my_error:
print(my_error)
my_connect.close()
Output is here
Rows Added = 1
last row id = 36
More on Error handlingAuthor
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.