How to Insert Data in tables in python?

Category: PythonHow to Insert Data in tables in python?
Editor">Editor Staff asked 2 years ago

How to Insert Data in tables in python?

1 Answers
Editor">Editor Staff answered 2 years ago

Data stored in Python variables can be inserted into database tables.
Data is passed to SQL statements through parameter substitution.
Single rows are inserted using execute and multiple rows using executeMany method of created cursor object.
 
Inserting a Sample Data…
Example 1…

# executing sql statement using try … except blocks
try:
cursor.execute(sql, rec)
con.commit()
except Exception as e:
print(“Error Message :”, str(e))
con.rollback()
 
# closing the database connection
con.close()
A single record is inserted into table EMPLOYEE.