DB Python Unit 3
DB Python Unit 3
Python path is environment variable. When we wish to execute the codes that we save in
our folder using the python idle, we need to locate folder.
Open control panel-> select system and security-> select system ->
select advance setting-> select environment variable from advance tab.
Dr. Mallika Bhatt Page 1
Create new path as PYTHONPATH. Here add SQLite folder path which is available in c:
drive.
Example:
# a is in the global namespace
a=5
def some_func():
a
b b e
f c
c
def some_func():
print("Inside some_func")
def some_inner_func():
var = 10
print("Inside inner function, value of var:",var)
some_inner_func()
print("Try printing var from outer function: ",var)
some_func()
Output:
Inside some_func
Inside inner function, value of var: 10
To connect sqlite database, first we need to import the sqlite3 module. Then, we create
the connection to the sqlite database file using sqlite3.connect() function. Finally, once
we are done, we should close the connection using sqlite3.close() function.
Python sqlite3 module will look for the database you specify, if the database is not exist
python will create a new database.
Example:
connection = sqlite3.connect("students.db")
connection.close()
Dr. Mallika Bhatt Page 5
As per the above example python will look for the database file “students.db” on the
current working directory (Since we didn’t specify the absolute path) and connect. If the
database “students.db” not exists, then python will create the “students.db” in the current
working directory.
Example 2
import sqlite3
connection = sqlite3.connect("/sqlite3/students.db")
connection.close()
Similar to the Example 1, but this time we specified the absolute path to the database file.
So python will look inside the /sqlite3 folder for the “students.db” which will be created
if does not exist.
To create sqlite tables, we need to execute SQL commands, to execute SQL commands
we need to add a cursor to our connection. In python sqlite cursor use to execute SQL
commands. A cursor is a temporary work area created in the system memory when a
SQL statement is executed. A cursor contains information on a select statement and the
rows of data accessed by it. This temporary work area is used to store the data retrieved
from the database, and manipulate this data.
import sqlite3
connection = sqlite3.connect("students.db")
cursor = connection.cursor()
connection.close()
import sqlite3
connection = sqlite3.connect("students.db")
cursor = connection.cursor()
connection.close()
Same way using the python cursor, we can add data to the sqlite database table.
import sqlite3
connection = sqlite3.connect("students.db")
cursor = connection.cursor()
Once we insert the data, we need to save changes to the database file using commit()
function, before close the db connection.
import sqlite3
connection = sqlite3.connect("students.db")
cursor = connection.cursor()
connection.commit()
connection.close()
In python 3, we can use for loop to read data from sqlite3 table.
import sqlite3
connection = sqlite3.connect("students.db")
cursor = connection.cursor()
for i in cursor.execute(select):
print(i)
We can update a record in table with using update command. In created file we will write
this update query with execute command.
We can delete a record in table with using delete command. In created file we will write
this delete query with execute command.