M4 Python SQL
M4 Python SQL
Python SQLite
The SQLite3 module
SQLite Methods:
connect, cursor, execute, close
Connect to Database:
Create Table;
Operations on Tables:
Insert, Select, Update, Delete, Drop Records
Python SQLite3 module is used to integrate the SQLite
database with Python.
It provides a straightforward and simple-to-use
interface for interacting with SQLite databases.
SQLite Methods
connect():
To use SQLite3 in Python, first we have to import the sqlite3
module and then create a connection object.
Connection object allows to connect to the database and will
let us execute the SQL statements.
Creating Connection object using the connect() function:
This will create a new file with the name ‘mydatabase.db’.
Syntax:
import sqlite3
con = sqlite3.connect('mydatabase.db‘)
cursor()
To execute SQLite statements in Python, we need a cursor object.
We can create it using the cursor() method.
The SQLite3 cursor is a method of the connection object.
To execute the SQLite3 statements,
should establish a connection at first and
then create an object of the cursor
Using the connection object as follows:
Syntax:
import sqlite3
con = sqlite3.connect('mydatabase.db‘)
cursorObj = con.cursor()
Syntax:
import sqlite3
con = sqlite3.connect('mydatabase.db‘)
cursorObj = con.cursor()
cursorObj.execute(''' CREATE TABLE movie(title text,
year int, score real) ''' )
close():
Once we are done with our database, it is a good practice
to close the connection.
We can close the connection by using the close()
method.
To close a connection, use the connection object and call
the close() method as follows:
con.close()
SQLite database operations
Creating and connecting to Database
When you create a connection with SQLite, that will
create a database file automatically if it doesn’t already
exist.
This database file is created on disk with the connect
function.
Following Python code shows how to connect to an
existing database.
If the database does not exist, then it will be created
and finally a database object will be returned.
execute() : This method accepts a MySQL query as a parameter
and executes the given query.
fetchall(): This method retrieves all the rows in the result set
of a query and returns them as list of tuples.
(If we execute this after retrieving few rows it returns the remaining
ones)
import sqlite3
con = sqlite3.connect('mydatabase.db')
cursorObj = con.cursor()
cursorObj.execute('''CREATE TABLE movie(title text, year int, score real)''')
con.commit()
con.close()
import sqlite3
con = sqlite3.connect('mydatabase.db')
cursorObj = con.cursor()
cursorObj.execute('''CREATE TABLE movie(title text, year int,
score real)''')
cursorObj.execute('''INSERT INTO movie VALUES ("Titanic",1997,
9.5)''')
con.commit()
con.close()
Update Table
To update the table:
First, create a connection,
create a cursor object using the connection and
finally use the UPDATE statement in the execute() method.
Suppose that we want to update the score with the movie title Dil.
For updating, we will use the UPDATE statement and for the movie whose title
equals Dil. We will use the WHERE clause as a condition to select this
employee.
Consider the following code:
import sqlite3
con = sqlite3.connect('mydatabase.db')
cursorObj = con.cursor()
cursorObj.execute("UPDATE MOVIE SET SCORE=10 WHERE TITLE='Dil' ")
con.commit()
con.close()
Select statement
You can use the select statement to select data from a
particular table.
If you want to select all the columns of the data from a
table, you can use the asterisk (*).
The syntax for this will be as follows:
cursorObj.execute("SELECT * FROM movie ")
OR
cursorObj.execute( "select column1, column2
from movie ")
Fetch all data
To fetch the data from a database, we will execute the SELECT statement and
then will use the fetchall() method of the cursor object to store the values into a
variable.
After that, we will loop through the variable and print all values.
The code will be like this:
import sqlite3
con = sqlite3.connect('mydatabase.db')
cursorObj = con.cursor()
con.commit()
con.close()
Delete
In SQLite database we use the following syntax to delete data from a
table:
Import the required module.
Create a connection object with the database using to connect().
Create a Cursor object by calling the cursor().
Finally, use execute() method by passing a DELETE statement as a parameter to it.
import sqlite3
con = sqlite3.connect('mydatabase.db')
cursorObj = con.cursor()
#Deleting
cursorObj.execute("Delete from movie where title='Titanic' ")
con.commit()
con.close()
Drop table
You can drop/delete a table using the DROP statement.
The syntax of the DROP statement is as follows:
import sqlite3
con = sqlite3.connect('mydatabase.db')
cursorObj = con.cursor()
con.commit()
con.close()
Connection using MySql
import mysql.connector
conn=mysql.connector.connect(host="localhost",user
="root",password="",database="students",buffered=Tr
ue)
cursor=conn.cursor()
Program to demonstrate a database operations-
create, insert, select, update, delete and drop
import sqlite3 #importing the module
con = sqlite3.connect('mydatabase.db') #create connection object
cursorObj = con.cursor() #crete a cursor
#Deleting
cursorObj.execute("Delete from movie where title='Titanic' ")
print("After deleting...") #Print the after Deleting
37 41,40,28() 31,41,42
29 43
33,36
27 25
23 24
19 22
18
Key points to remember:
Step 1: Check python version
Open Command prompt run as administrator
Type python
Check the version
Step 2: Open Xampp control panel
Enable Apache & Mysql
Open mysql admin panel using link :
localhost:80/phpmyadmin
Create respective Database, table and fields
Step 3: Python SQL Code
Open IDLE script mode
Write the code & save the file
Using Run Module option execute the code