Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python SQLite connection.commit() Function



The Pythonconnection.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 theconnection.commit() function.

conn.commit()

Parameter

This function doesn't accept any parameters.

Return Value

Return Values

Thisconnection.commit() function has no return value.

Example 1

In the below example, we are inserting two values into the table using theconnection.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-------------ShinyVarsha

Example 2

Now, we are deleting one of the values from the above example using theconnection.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 theconnection.commit() function.

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

Output

The output will be displayed as follows −

first_name-----------VarshaJohn
python_modules.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp