HomeDATA BASESQLITEAll about SQLite3 command in Python.

All about SQLite3 command in Python.

SQLite3 command in Python module is used to interact with SQLite databases. Below are the definitions and purposes of various SQLite3 command in Python.

  1. Connection Management Commands:
  2. Data Definition Language (DDL) commands:
  3. Data Manipulation Language (DML) commands:
  4. Transaction Management Language (TCL) commands:
  5. Data Query Language (DQL) commands:
  6. Miscellaneous Commands:
import sqlite3
con=sqlite3.connect('database_name')
  • This command connects to the specified SQLite3 database.
  • If data base does not exist, it will be created.
  • To connect to an SQLite3 database in Python, you use the ‘sqlite3.connect()’ function.

Understanding the connection object:

  • The connection object represents the database connection.
  • It is used to create cursor objects,manage transactions, and execute SQLite3 commands in Python.
  • Purpose: To initiate a session with the SQLite database, enabling subsequent database operations.

Data Definition Language (DDL) SQLite3 command in Python are used to define and manage database schema structures, such as tables, indexes, and views.

In Python using the ‘SQLite3’ module you can execute Data Definition Language (DDL) commands to create table, modify table structure and content, and delete these structures and content.

  1. CREATE TABLE
  2. ALTER TABLE
  3. DROP TABLE

CREATE TABLE:-

‘CREATE TABLE’ SQLite3 command in Python is used to create a new table in the database.

Purpose of CREATE TABLE: To define the structure of a table, including columns and there data types.

Syntax: SQLite Query to create table:-

In above query con and query is user defined constant.

Example: Create Student table with St_id as a primary key constraint.

Example: Create Student_3 table with multiple field as UNIQUE constraint.

In this example St_id as a primary key, St_email and state field is as a unique key.

Example: Create student_4 table with NOT NULL Constraint.

In this example st_id is primary key constraint, st_email and state is unique constraint, st_name is NOT NULL constraint.

Example: Create student_5 table with check constraint.

In this above example st_id as a primary key and as a check constraint, st_name as NOT NULL constraint, st_email,Mo_no and state as an unique constraint.

Example: Create Student_6 table with DEFAULT constraint.

In this above example st_id as a primary key and check constraint, St_name as a NOT NULL constraint, st_email,Mo_no and state as a unique constraint, st_class as a DEFAULT constraint.

ALTER TABLE:-

This ALTER TABLE SQLite3 command in python is used to modify an existing table structure in a database.

Purpose: To add or modify columns in a table.

Syntax of add column in table:-

Example: Add a new column in existing table in SQLite3 database.

CODE: You can directly copy and execute this code.

In this above example the new column Admission_year has been added to the student_6 table.

DROP TABLE:-

SQLite3 command in python used to delete a table from the database.

Purpose: To remove an entire table and its data from the database.

Syntax for DROP TABLE:-

Example: DROP TABLE from database.

CODE: You can directly copy and execute this code.

  • Data Manipulation Language (DML) commands in sqlite3 command in python are used to manipulate data stored in the database.
  • These commands allow you to insert new data, update existing data, and delete data from the database.
  • In python using the ‘SQLite3’ module you can execute DML commands to intrect with the database.

There are manly three types of Data Manipulation Language (DML) sQLite3 command in Python:

  1. INSERT INTO
  2. UPDATE
  3. DELETE FROM

INSERT INTO Command:-

SQLite3 command in Python used to insert new rows into a table.

We can at a time one new row or more than one row insert into a table.

Purpose: To add new records to a table.

Syntax: For insert one new rows/records of data in table:

Example: For insert single new rows of data in table.

Code: for insert single records in table.

Syntax for insert more than one row/records of data in table:

Example: To insert multiple rows/records of data in table:

Code: To insert a multiple records in table:

UPDATE Command:-

SQLite3 command in Python is a used to modify existing rows in a table.

Purpose:- To update the data of existing records.

Syntax: For UPDATE command:-

Example: To update a records from a table:

Code: To UPDATE a records in table:

DELETE FROM Command:-

SQLite3 command in Python used to delete rows from a table.

Purpose: To remove specific records from a table.

Syntax: For Deleting data from a table:

Example: For Deleting data from a Table:

Code: For Deleting data from a Table:

  • Transaction Control Language (TCL) SQLite3 command in Python are used to manage transactions within the database.
  • Transactions are sequences of SQLite commands that are executed as a single unit of work.
  • TCL(Transaction Control Language) command ensure the integrity and consistency of the database by following you to commit or roll back changes.
  • In Python using the ‘SQLIte3’ module, you can execute TCL commands to manage transaction.

There are manly three type of Transaction Control Language(TCL) SQLite3 command in Python.

  1. BEGIN TRANSACTION
  2. COMMIT
  3. ROLLBACK

BEGIN TRANSACTION:

This command begins a new transaction.

Syntax:

Purpose: To start a new transaction so that subsequent SQLite3 commands are execute within this transaction.

Example in Python:

COMMIT:

TCL command commits the current transaction,making all changes permanent.

Syntax:

Purpose: To save the changes made during the transaction to the database.

Example In Python:

ROLLBACK:

Roll back the current transaction, undoing all changes made during the transaction.

Syntax:

Purpose: To revert the database to its previous state in case of an error.

Example in Python:

Data Query Language(DQL) commands in SQLite3 are used to query and retrieve data from the database.

The primary DQL command is ‘SELECT’ which allows you to specify the columns,tables, and conditions for retrieving the desired data.

In python using the SQLite3 module you can execute this command to fetch data from the database.

There are only one command in DQL.

  1. SELECT

SELECT Command:-

The SELECT command is the most important command in the entire database SQLit3 command.

This command is used to retrieve data from one or more tables.

Syntax:

Purpose: To fetch data based on specified criteria from the database.

Example in Python:

Above code output:

  • connection.close(): Close the connection to the database.
  • connection.execute(): Execute a single SQLite3 statement.
  • connection.executemany(): Execute a SQLite3 command against all parameter sequences or mappings found in the sequence.
  • connection.fetchall(): Fetch all(remaining) rows of a query result.
  • connection.fetchmany(size): Fetches the next set of rows of a query result, returning a list.
  1. Q 1: Which command is used to connect to an SQLite3 database in Python?
    A. sqlite.connect(‘database_name.db’)
    B. database.connect(‘database_name.db’)
    C. sqlite3.connect(‘database_name.db’)
    D. connect(‘database_name.db’)

    Ans:C. sqlite3.connect(‘database_name.db’)

  2. Q 2: Which of the following is correct option to create a table in SQLite?
    A. create database table_name
    B. create index table_name
    C. create schema table_name
    D. create table table_name

    Ans:D. create table table_name

  3. Q 3: What is the correct syntax to insert data into an SQLite3 table?
    A. INSERT INTO table_name (column_1, column_2) SET (value_1,value_2)
    B. INSERT table_name(column_1,column_2)VALUES(column_1,column_2)
    C. INSERT INTO
    table_name(column_1,column_2)VALUES(value_1,value_2)
    D. INSERT INTO table_name SET column_1=value_1,column_2=value_2

    Ans:C. INSERT INTO table_name(column_1,column_2)VALUES(value_1,value_2)

  4. Q 4: Which command is used to fetch all rows from the result of a query in SQLite3?
    A. connection.getrow()
    B. connection.fetchall()
    C. connection.fetchrow()
    D. connection.fetchallrows()

    Ans:B. connection.fetchall()

  5. Q 5: How do you delete row from an SQLite3 database table?
    A. DELETE FROM table_name WHERE condition
    B. DELETE ROW FROM table_name WHERE condition
    C. REMOVE FROM table_name WHERE condition
    D. DROP FROM table_name WHERE condition

    Ans:A. DELETE FROM table_name WHERE Condition

  6. Q 6: What is the correct SQLite3 command to update a value in an SQLite3 database table?
    A. MODIFY table_name SET column_1=value_1 WHERE condition
    B. CHANGE table_name SET column_1=value_1 WHERE condition
    C. UPDATE table_name SET column_1=value_1 WHERE condition
    D. SET table_name UPDATE column_1=value_1 WHERE condition

    Ans:C. UPDATE table_name SET column_1=value_1 WHERE condition

  7. Q 7: How do you commit a transaction in SQLite3 after executing SQLite3 commands in Python?
    A. cursor.commit()
    B. connection.commit()
    C. database.commit()
    D. sqlite3.commit()

    Ans:B. connection.commit()

  8. Q 8: Which command is used to delete a table stricture and his data from an SQLite database?
    A. DELETE TABLE table_name
    B. REMOVE TABLE table_name
    C. DESTROY TABLE table_name
    D. DROP TABLE table_name

    Ans:D. DROP TABLE table_name

  9. Q 9: What is the proper way to close an SQLite3 database connection in Python?
    A. connection.close()
    B. cursor.close()
    C. db.close()
    D. database.close()

    Ans:A. connection.close()

  10. Q 10: Which command allows you to execute multiple SQLite3 statements at once in SQLite3?
    A. connection.executesql()
    B. connection.executescript()
    C. connection.executemany()
    D. connection.executeall()

    Ans:B. connection.executescript()

RELATED ARTICLES

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

SQLite3 Command in Python: SQLite3 command in Python module is used to interact with SQLite databases. Below are the definitions and purposes of various SQLite3 command in Python. SQLite3 Commands in Python can be broadly categorized into the following types: Connection Management Commands: Data Definition Language...All about SQLite3 command in Python.