SQL Using Python
SQL Using Python
SQLITE3
Databases offer numerous functionalities by which one can manage large
amounts of information easily over the web, and high-volume data input and
output over a typical file such as a text file. SQL is a query language and is very
popular in databases. Many websites use MySQL. SQLite is a “light” version
that works over syntax very much similar to SQL.
SQLite is a self-contained, high-reliability, embedded, full-featured, public-
domain, SQL database engine. It is the most used database engine in the world
wide web.
Python has a library to access SQLite databases, called sqlite3, intended for
working with this database which has been included with Python package since
version 2.5.
SQL USING PYTHON
Before moving further to SQLite3 and Python let’s discuss the cursor
object in brief.
The cursor object is used to make the connection for executing SQL
queries.
It acts as middleware between SQLite database connection and SQL
query. It is created after giving connection to SQLite database.
The cursor is a control structure used to traverse and fetch the
records of the database.
All the commands will be executed using cursor object only.
EXECUTING SQLITE3 QUERIES – CREATING
TABLES
After connecting to the database and creating the cursor object let’s see how to
execute the queries.
To execute a query in the database, create an object and write the SQL command
in it with being commented. Example:- sql_comm = ”SQL statement”
And executing the command is very easy. Call the cursor method execute() and
pass the name of the sql command as a parameter in it. Save a number of
commands as the sql_comm and execute them. After you perform all your
activities, save the changes in the file by committing those changes and then lose
the connection.
EXECUTING SQLITE3 QUERIES – CREATING
TABLES
INSERTING DATA INTO TABLE
For updating the data in the SQLite3 table we will use the UPDATE statement. We can update
single columns as well as multiple columns using the UPDATE statement as per our
requirement.
DELETING DATA
DROP is used to delete the entire database or a table. It deleted both records in the table along
with the table structure.
DELETING TABLE
DROP is used to delete the entire database or a table. It deleted both records in the table along
with the table structure.