Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Python SQLite connection.commit() Function



The Python connection.commit() function in SQLite is used to save the current state of the database. This function permanently saves all changes from the last commit or rollback. If the commit() function is not specified, then whatever changes are made in the function will not be saved, and the connection will be lost if a rollback is performed.

In SQLite, a connection refers to an active link between the application and database. This connection allows us to execute SQL commands and queries in the database.

The commit function is used to save all changes made within the current transaction. In a database, a transaction is a sequence of operations that is performed as a single logical unit of work.

Syntax

Following is the syntax for the connection.commit() function.

conn.commit()

Parameter

This function doesn't accept any parameters.

Return Value

Return Values

This connection.commit() function has no return value.

Example 1

In the below example, we are inserting two values into the table using the connection.commit() function.

cursor.execute("INSERT INTO employees(first_name) VALUES(%s), (%s)", ('Shiny', 'Varsha'))
conn.commit()

Output

We will get the output as follows −

first_name
-------------
Shiny
Varsha

Example 2

Now, we are deleting one of the values from the above example using the connection.commit() function.

cursor.execute("DELETE FROM employees WHERE first_name = ? ", ('Varsha,'))
conn.commit()

Output

The result is produced as follows −

first_name
-----------
Shiny

Example 3

Here, we are inserting new values into a table using the connection.commit() function.

cursor.execute("UPDATE employees SET first_name = ?", ('John', 'Shiny'))
conn.commit()

Output

The output will be displayed as follows −

first_name
-----------
Varsha
John
python_modules.htm
Advertisements