0% found this document useful (0 votes)
2 views

DB-API

Uploaded by

sahiljamwal2720
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DB-API

Uploaded by

sahiljamwal2720
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

DB – API

Module Attributes of DB-API

Data Attributes
apilevel
This string (not float) indicates the highest version of the DB-API the module is compliant
with, i.e., "1.0", "2.0", etc.

threadsafety
This is an integer with these possible values:
● 0: Not threadsafe
● 1: Minimally threadsafe
● 2: Moderately threadsafe

● 3: Fully threadsafe
Paramstyle
This argument is just a string which tells about the SQL statement parameter style

Function Attribute
connect()
The connect() function establishes a connection to the database. It takes database-specific
parameters (e.g., database name, host, username, password) and returns a Connection
object.
Example :

Exception
Exceptions that should also be included in the compliant module as globals
Connection Object
A connection object represents a connection between a Python program and a
database. It is created when you establish a connection to the database using a
connect() method.

con is cursor object


Connection Object methods
1. cursor()
 Creates a new cursor object, which is used to execute SQL commands
and fetch results.

2. commit()
 Commits the current transaction, making all changes made during the
transaction permanent.

3. rollback()
 Rolls back any changes made during the current transaction, undoing
uncommitted operations.

4. close()
 Closes the connection to the database. This is essential for releasing
resources.
Cursor Object
The cursor object in Python's DB-API is a key component that allows interaction with the
database. It acts as an interface for executing SQL queries, fetching data, and managing the
result sets returned by the database.
Cursor Object Attributes
description:
 Returns a tuple of tuples, where each inner tuple contains metadata about a column
in the result set.

 Each tuple usually includes:


1. Column name
2. Data type
3. Display size (if applicable)
4. Internal size

5. Precision
6. Scale
7. Whether the column can accept NULL
rowcount:
 Indicates the number of rows affected by the last executed statement.
 For SELECT, it may not always give accurate results as it returns number of rows
affected but with select no row is affected it just display rows so give output as -1.
 Sbse imp points agr teen chaar execute statements hai to jo last mein run hui hai use
jitni rows affect hongi who return krega

Isme maine commit nhi kiya hai to database mein kor frk ni aayega

arraysize:
 Specifies the number of rows returned by the fetchmany() method.
 Default is typically 1 but can be adjusted.
Default arraysize hota hai one toh ek row fetch ki ftechmany ne , doosre mein hmne
arraysoze ko change kr diya to fir stechmnay ne utni rows fetch ki

lastrowid

It provides the row id of the last inserted row. It is only updated after
successful INSERT or REPLACE statements using the execute() method.
For other statements, after executemany() or executescript(), or if the insertion failed, the
value of lastrowid is left unchanged. The initial value of lastrowid is None.
Cursor Methods
execute(sql [, parameters] ):

 Executes a single SQL query.


 Supports parameterized queries to prevent SQL injection.

Ek cheez dhyaan rakhna ke yeh tuple mein pass krna hota hai , isliye agr ek hi pass krna
hai to comma bhi aage saath mein like ( 4, )

executemany(sql, seq_of_parameters):

 Executes a SQL query multiple times using a sequence of parameter sets.


 Useful for bulk inserts or updates.

fetchone()
 Fetches the next row from the query result set.
 Returns a single row as a tuple or None if no more rows are available.
First row ko fetch kr liya isne

fetchmany(size)
 Fetches a specified number of rows from the query result set.

 The default number of rows is determined by the arraysize attribute.


fetchmany(size=cursor.arraysize)

Yeh next rows ko fetch krta hai agr isse pehle maine koi row fetch krli hogi to yeh use
agli row se start krega fetch krna
fetchall()
 Fetches all remaining rows from the query result set.
 Returns a list of tuples, where each tuple represents a row.

Do maine pehle krli thi fetchmany se to h fir fetch all uske aage ki sabhi dedega

close()
 Closes the cursor, releasing any associated resources.
 No further operations can be performed on a closed cursor.

You might also like