DBMS Exp7
DBMS Exp7
EXPERIMENT 6
THEORY:
Python SQLite3 module is used to integrate the SQLite database with Python. It is a
standardized Python DBI API 2.0 and provides a straightforward and simple-to-use
interface for interacting with SQLite databases.
The abbreviation CRUD expands to Create, Read, Update and Delete. These four are
fundamental operations in a database. In the sample database, we will create it, and do
some operations.
CREATE:The create command is used to create the table in database.
SYNTAX: CREATE TABLE table_name ( Attr1 Type1, Attr2 Type2, … , Attrn Typen ) ;
INSERT: This refers to the insertion of new data into the table. Data is inserted in the
form of a tuple. The number of attributes in the tuple must be equal to that defined in the
relation schema while creating the table.
1. To insert attributes in the order specified in the relation schema:
SYNTAX:: INSERT INTO tableName VALUES ( value1, value2, … valuen )
2.To insert attributes in the order specified in the relation schema or in a different order:
SYNTAX: INSERT INTO tableName ( Attribute1, Attribute3, Attribute2 . . . ) VALUES (
value1, value3, value2 . . . )
READ: This refers to reading data from a database. A read statement has three
clauses:
SELECT: Takes as the predicate the attributes to be queried, use * for all attributes.
FROM: Takes as the predicate a relation.
WHERE: Takes as the predicate a condition, this is not compulsory.
UPDATE: This refers to the updating of tuple values already present in the table
SYNTAX: UPDATE tableName SET Attribute1 = Value1 , Attribute2 = Value2 , . . .
WHERE condition;
The WHERE clause must be included, else all records in the table will be updated.
DELETE: This refers to the deletion of the tuple present in the table.
SYNTAX: DELETE FROM tableName WHERE condition
If WHERE clause is not used then all the records will be deleted
Some common methods in SQLite used for performing CRUD operations are as follows:
1. sqlite3.connect(): It creates a connection to the database whose name is passed as
parameter in the current working directory, implicitly creating it if it does not exist:
2. cursor(): In order to execute SQL statements and fetch results from SQL queries, we
will need to use a database cursor.So this creates cursor to the database
3. execute(): It is used to execute a SQLite script in python.
4. commit(): It is the transactional command used to save changes invoked by a
transaction to the database.
5. close(): It is used to close the SQLite connection.