×

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

How to print in same line in python

How to Print in the Same Line in Python To print in Python, we use the print () function and the syntax of print () goes like this: print (values, sep =...

3 minutes read.

Python Function

Python Function A Python function is a reusable, organized block of code that is used to perform the specific task. The functions are the appropriate way to divide an extensive program into a...

7 minutes read.

Math Module in Python

In this article, you are going to learn everything in detail about the “ math “ module in Python. We can normally work with general operations using python without importing or...

16 minutes read.

__GETITEM__ and __SETITEM__ in Python

These methods are used in assignment operations, unary comparison operations, binary comparison operations and binary operations. These are pre-defined methods that perform many operations on a class instance. Examples like...

3 minutes read.

Python Set symmetric_difference_update() method

Python Set symmetric_difference_update() method The set.symmetric_difference_update() method in Python updates the original set by removing items that are present in both sets and inserting the other items of the set. Syntax set.symmetric_difference_update(set1) Parameter set- This parameter represents the first...

2 minutes read.

Reason for Python So Popular

One of the languages that is witnessing outstanding development and acceptance every year in Python. Python has emerged as the programming language with the greatest rate of growth, and Stack...

3 minutes read.

Python Stack

Python Stack: The work Stack is defined as arranging a pile of objects or items on top of another. It is the same method of allocating memory in the stack...

10 minutes read.

Application to Search Installed Application using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Selection Sort Using Python

Selection sort is a type of sorting algorithm which is based on sorting elements in increasing order or ascending order through comparison. This sorting technique does not take extra space...

3 minutes read.

How to Sort a String in Python?

The characters in the string are sorted or put in alphabetical order using the sort string function in Python. Python has built-in techniques for sorting strings available. Since we occasionally...

6 minutes read.

Python String islower() method

Python String islower() method The string.islower() method returns a boolean value true if all cased characters in the string are lowercase and for  any other value is returns false otherwise. Syntax string.islower() Parameter NA Return This...

1 minute read.

Algorithm for Factorial of a number in Python

What is a Factorial Number? In mathematics, the factorial of a positive integer n, denoted by n!. It is the product of all positive integers less than or equal to n. For...

3 minutes read.

Python Multithreading

In python, we can implement multithreading. In this tutorial, we will understand the basics of implementing multithreading in a python programming language. Multithreading is just like multiprocessing. We use it...

4 minutes read.

How To Take Multiple Inputs In Python

In C language, we make use of the scanf() function to obtain the values from the user and store it in the variable. Coming to the Python language, we use the...

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

Python Knapsack problem

Python Knapsack problem Before we dig down about Knapsack problems in Python, first let's have a look at what is actually a knapsack problem. What is a knapsack problem? A problem from the...

5 minutes read.

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal We know that the most widely used number system is a decimal system, but the computer only understands binary values. The...

2 minutes read.

Excel Automation with Python

Data analysis is the upcoming technology in the IT sector; this data analysis can be performed easily with the help of data frames or by using excel sheets. The data...

4 minutes read.

Python String zfill() method

Python String zfill() method The string.zfill() method in Python returns a copy of the string while adding the zeros (0) at the beginning of the string, until it reaches the specified...

1 minute read.

Python Debugger

In this tutorial, we will understand what is debugging in general. Then we will realize what the python debugger means and what is the method to perform it. Now, let us...

3 minutes read.