Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python SQLite connection.excutescript() Function



The Pythonconnection.executescript() function executes the provided SQL script from the connection object. This function is used for specifying database and batch operations. It can handle multiple statements for batch processing.

A Connection object represents the database and allows us to interact with the database by executing SQL commands and managing transactions in the database.

Execute is part of the connection object in SQLite. It takes a single argument containing one or more SQL statements separated by semicolons and executes all the SQL statements in the string one by one.

Syntax

Following is the syntax for theconnection.executescript() function.

connection.executescript(sql_script)

Parameters

A string containing one or more SQL statements is separated by semicolons, and this script can include any SQL commands.

Return Value

This function returns None after executing the provided SQL script.

Example

Consider the following EMPLOYEES table, which stores employees ID, Name, Age, Salary, City and Country −

IDNameAgeSalaryCityCountry
1Ramesh322000.00MarylandUSA
2Mukesh405000.00New YorkUSA
3Sumit454500.00MuscatOman
4Kaushik252500.00KolkataIndia
5Hardik293500.00BhopalIndia
6Komal383500.00SaharanpurIndia
7Ayush253500.00DelhiIndia

Example 1

Consider the above table, this represents employees from India who are 30 years old or younger, using theconnection.executescript() function.

import sqlite3conn = sqlite3.connect('res.db')conn.executescript(""" UPDATE employees SET Salary = Salary * 2.2 WHERE Country = 'India';DELETE FROM employees WHERE Age > 35;""")conn.commit()conn.close()

Output

The result is obtained as follows −

IDNameAgeSalaryCityCountry
1Ramesh322000.00MarylandUSA
4Kaushik252500.00KolkataIndia
5Hardik293500.00BhopalIndia
7Ayush253500.00DelhiIndia

Example 2

In the below example, we are deleting multiple rows from the SQLite table using theconnection.executescript() function.

import sqlite3conn = sqlite3.connect('res.db')conn.executescript("""DELETE FROM employees WHERE Salary < 4000;""")conn.commit()conn.close()

Output

When we run the above code, we will get the following result −

IDNameAgeSalaryCityCountry
2Mukesh405000.00New YorkUSA
3Sumit454500.00MuscatOman

Example 3

Here, this example inserts a new row into the employee table usingconnection.executescript() function.

import sqlite3conn = sqlite3.connect('res.db')conn.executescript("""INSERT INTO employees (ID, Name, Age, Salary, City, Country) VALUES (8, 'Sharon', 22, 4300.00, 'Mumbai', 'India');""")conn.commit()conn.close()

Output

The result is produced as follows −

(8, 'Sharon', 22, 4300.00, 'Mumbai', 'India')

Example 4

In this example, a list is passed to thisconnection.executescript function, which throws a TypeError because the expected datatype is not given to this function.

import sqlite3conn = sqlite3.connect(':data:')conn.executescript("CREATE TABLE employees (ID INTEGER, Name TEXT);")conn.executescript(["INSERT INTO employees VALUES(2, 'Mukesh');"])

Output

We will get the result as follows −

TypeError: executescript() argument 2 must be str, not list
python_modules.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp