DB-API
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.
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.
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] ):
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):
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.
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.