×

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

Number pattern in Python

Number pattern in Python: This article explains how to print number patterns in Python. The FOR loop, while loop, and range() functions are used in the following Python programs to...

4 minutes read.

How to check the version of the Python Interpreter?

As we all know what an interpreter is, and how important it is. We should also be aware of the fact that it is important to have knowledge of the...

2 minutes read.

Python System Command

To execute a program in Python, we need to execute some shell commands to run our program on the computer. Python will provide some shell commands in our background to...

3 minutes read.

How to append an Array in Python

A group of objects kept at adjacent memory regions is known as an array. It is a container with a set capacity for a certain number of things, all of...

7 minutes read.

Exponentiation in Python

What is an Exponent in Python? Exponent is a fundamental mathematical concept that is used in many different areas, such as engineering, physics, and finance. In mathematics, an exponent is a...

4 minutes read.

Standard Scalar in Python

Python is a vast and open-source language that contains many libraries, modules, and functions. These functions in python are reusable codes where we don’t need to type the whole code...

4 minutes read.

Sys Module in Python

What are Modules? The Modules are the kind of files that contain Python statements and definitions. The module is known by the name of the file followed by the suffix “.py”....

5 minutes read.

Convert String to Binary in Python

String to binary The strings can be defined as the array of Unicode code characters. Binary Binary is defined as the number system which consists two symbols 0 and 101. It is base-2...

2 minutes read.

Python Random shuffle( ) method

The shuffle() is used to change the positions of the elements in the mutable sequences. The shuffle( ) function will change the positions of the elements in the sequence of...

3 minutes read.

Find whether the given stringnumber is palindrome or not

Problem statement Sam is found of playing with strings. One day he thought of finding whether a string is a palindrome or not. He wanted to develop a computer process to...

2 minutes read.

Python Slice from Last Occurrence of K

Introduction We already know that in Python, cutting produces a sub-string out of a string. The variables start, stop, and step are used to set the slicing range. When dealing on...

3 minutes read.

Time. Sleep() in Python

Python time sleep () function suspends execution for a certain seconds given by user. Time sleep () syntax: Sleep(seconds) Limitations: The number of seconds to be suspends the code as per its requirements. Returns: Void The execution...

3 minutes read.

Python MongoDB Tutorial

An Introduction to MongoDB MongoDB is a document-oriented database application. It is an Open-source and platform-independent program. MongoDB is similar to few NoSQL databases that store the data in the documents...

17 minutes read.

Python Command line Arguments

Python Command line Arguments The command-line argument is used to the change functionality of the program. It's an extra command the programmer can use while launching a program. These commands have many uses...

7 minutes read.

Python String split() method

The string.split() method in Python splits a string into a list and returns a list of the words in the string. If the parameter maxsplit is given, at most maxsplit splits are done. If maxsplit is...

2 minutes read.

Python Logging Maxbytes

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....

6 minutes read.

Python Permutations and Combinations

In mathematics, we all studied what is meant by permutations and combinations. “Permutations” define the way of arranging the elements in sequential order. “Combinations” mean the way of selecting the...

7 minutes read.

Python TCP Server

The TCP server library is also known as the socket library. Socket programming is the method of connecting two nodes through a network to form communication between two nodes. In...

3 minutes read.

Read numpy array in Python

Numpy is a numerical python that deals with multidimensional arrays mostly used in storing multiple values. Python's core scientific computing package is called NumPy. This Python library provides multidimensional array...

9 minutes read.

How to comment multiple lines in python?

How to comment multiple lines in python There are two qualities that come up with every good and successful programmer- i) Indenting the code ii) Using comments in the code Let us see how...

4 minutes read.