×

Python SQLite

SQLite

It is an RDBMS (Relational Database Management System). It is an embedded, serverless, transactional SQL database engine. It is an open-source application. It is named SQLite because of its lightweight. It is less than 500kb, unlike many other database systems such as SQL and Oracle. It was designed in August 2000.

Why do we use SQLite?

  • It is serverless. It does not need different servers to operate.
  • It is very flexible. It allows you to work on multiple databases at the same time.
  • It is free. It does not require any license to work on it.
  • It doesn’t require configuration. We can start working on it just by installing it.
  • It is a cross-platform DBMS.
  • We can easily store data.
  • It also provides a lot of APIs.

Advantages of SQLite

  • It is a very lightweight database.
  • It has better performance. Operations are very fast and easy to perform for SQLite databases.
  • It is very easy to use. We don’t have to install it. We can download the SQLite library.
  • It is very reliable. Since it updates your content regularly, you don’t have to keep track of losing data.
  • It can access through any third-party tool.

Disadvantages of SQLite

  • It cannot handle high-traffic HTTP requests.
  • Its database size is very restricted.

SQLite Command

  1. DDL: Data Definition Language
    • Create
    • Alter
    • Drop
  2. DML: Data Manipulation Language
    • Insert
    • Update
    • Delete
  3. DQL: Data Query Language
    • Select

SQLite Datatype

SQLite data types are used to represent the type of data of any object. In the database, each column, variable and expression are the type of database. These data types are used when we create a table. In SQLite, the data type is associated with the value itself but not the container in which it is stored.

Types of SQLite Data Types

  1. SQLite Storage Class
    • NULL: It is used to represent a null value.
    • INTEGER: It is used to represent the integer value of 1, 2, 3, 4, 6 and 8 bytes.
    • REAL: It represents a floating point value, stored as an 8-byte IEEE value.
    • text: It is used to represent a string value.
    • BLOB: It is used to represent data in a blob value, storing it as the data itself.
  2. SQLite Afinity Class
    • TEXT: This column is used to store data using the storage class data type( TEXT, NULL, BLOB).
    • NUMERIC: This column contain all five storage class data type.
    • INTEGER: This behaves similarly to the numeric affinity class.
    • REAL: It behaves the same as the numeric affinity class. In this, there is an exception that the integer type is forcefully converted into a floating type.
    • NONE: In this column class, we don’t prefer one storage class with another one.

Connecting SQLite with Python

To install the SQLite library, we use the following command.

sudo apt-get install sqlite3 libsqlite3-dev  

After installing SQLite, we can use the command to start prompting the library is:

sqlite3

After that, we can create a folder, and we can use the command

sqlite3 database_name.db

To check if the database is created, we can use the command

.databases

Example of using SQLite

# file location
  
import sqlite3  
 
conn = sqlite3.databs('tutorial.db')  
  
print "Opened database successfully";  
conn.execute('''''CREATE TABLE Employees 
       (ID INT PRIMARY KEY NOT NULL, 
       NAME  TEXT    NOT NULL, 
       AGE INT NOT NULL, 
       ADDRESS CHAR(50), 
       SALARY REAL);''')  
print "Table is created";  
  
conn.close()  

Explanation

In this example, we will create a python file databs.py and use the following code. After that, we created a database name as tutorial.db, and we can create a table name as an employee after connecting this database.

Inserting data into the database

import sqlite3  
  
conn = sqlite3.connect('tutorial.db')  
print "Opened database successfully";  
  
conn.execute("INSERT INTO Employees (ID,NAME,AGE,ADDRESS,SALARY) \  
      VALUES (1, 'Aryan', 22, 'Delhi', 40000.00 )");  
  
conn.execute("INSERT INTO Employees (ID,NAME,AGE,ADDRESS,SALARY) \  
      VALUES (2, 'Ben', 22, 'London', 25500.00 )");  
  
conn.execute("INSERT INTO Employees (ID,NAME,AGE,ADDRESS,SALARY) \  
      VALUES (3, 'Alan', 25, 'CA', 500000.00 )");  
  
conn.execute("INSERT INTO Employees (ID,NAME,AGE,ADDRESS,SALARY) \  
      VALUES (4, 'Krish', 27, 'Gujrat ', 45000.00 )");  
  
conn.commit()  
print "Records inserted successfully";  
conn.close()  

Selecting the data from the database

import sqlite3  
  
conn = sqlite3.connect('javatpoint.db')  
  
data = conn.execute("select * from Employees");    
for row in data:  
   print "ID = ", row[0]  
   print "NAME = ", row[1]  
   print "ADDRESS = ", row[2]  
   print "SALARY = ", row[3], "\n"  
  
conn.close();  

Related Topics

Python Glob

Methods for matching files that include particular patterns in accordance with UNIX shell-related expansion rules are referred to as "glob" methods. Similar methods for finding, locating, and searching for all of...

12 minutes read.

XXhash Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

6 minutes read.

CatPlot in Python

Python Seaborn Library Seaborn is a superb Python tool for displaying graphical statistics graphing. Seaborn provides different color schemes and attractive default styles to facilitate the creation of various statistics charts...

8 minutes read.

Python Multiprocessing Processor

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Image to NumPy Arrays in Python

Image is a simple process of working model confined to machine learning, Python works with the image in the data format of width and height i.e. Images are excelled into...

3 minutes read.

Python Super function

The super function is used to access and call the methods or functions involved in the parent class during Inheritance. What is Inheritance? The process where the parent class inherits some of...

3 minutes read.

Python List copy() method

Python List copy () method The list.copy () method returns a shallow copy of the list. Syntax list.copy() Parameter NA Return This function returns the copy for the given list. Example 1 # Python program explaining # the list.copy() method # passing the...

1 minute read.

Python logging Module

Python logging Module Introduction By logging word we understand the tracking of the events which happens when we run some software. Logging process is very important part for developing software, debugging...

12 minutes read.

IPython Display

A command shell, often known as IPython is a software application that makes an operating system's features accessible to users or other applications. Based on the purpose and specific functioning...

6 minutes read.

Anytree Python

Python's anytree module is frequently used within data science-related software. Anytree has a Tolerant License, a Construct File that is public, no errors, no risks, and excellent support. One may...

3 minutes read.

How to Download Python

How to Download Python Python is the most renowned programming language and developers across the globe are working on projects that are made using Python and its libraries. Here we will...

4 minutes read.

Python Basics

In this tutorial, we will learn about the basics of Python, a very famous programming language. This tutorial will help you understand the language better if you start with python....

8 minutes read.

Python Program to Find the Greatest Among Three Numbers

Python Program to find the greatest among three numbers We have many approaches to get the largest of three numbers, and here we will discuss a few of them. This python...

2 minutes read.

GUI to Shut down, Restart and Logout from the PC using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

4 minutes read.

Python Constructor

Introduction A constructor is defined as the special kind of function or method that is used for instance variables initialization during the creation of an object of a class. The constructor's task...

5 minutes read.

How to create a dictionary in Python?

How to create a dictionary in python Dictionary is a data structure in Python that represents our data in the form of keys and values. Each value in a dictionary can be...

5 minutes read.

ModuleNotFoundError No module named 'mysql' in Python

mysql module not found is an error that occurs in python. It appears like ModuleNotError: No module named 'mysql.' This error occurs when you forget to install a module called...

3 minutes read.

Commands in Python

In this tutorial, we will see some of the widely used python commands along with their syntaxes and examples. To make Python more user-friendly, developers have provided these commands to...

12 minutes read.

Python Boolean

In this article, you will learn the boolean variables in python, bool() function in python, and bool operators with examples, Boolean Objects in Python. There are the only two possible values...

6 minutes read.

Standard GUI Unit Converter using PyQt5 in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

6 minutes read.