×

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 And Operator

Python's and operator takes two operands that can be either object, Boolean expressions, or both. And operator creates more complex expressions using those operands. Conditions are the common name for...

8 minutes read.

Spotify API in Python

The application programming interface is referred to as API. In essence, an API serves as a layer of communication or, as the name suggests, an interface that enables systems to...

3 minutes read.

Expressions in Python

What is Expression in Python? The expression contains more than one operator as well as the operands with it. Expression helps us to produce some other values. In the Python programming...

12 minutes read.

Binary Search Visualization using Pygame in Python

A calculation like Binary Search is seen effectively by picturing. Here in this tutorial, a function that pictures the Binary Search Algorithm is carried out. The Graphical User’s Interface (GUI)...

15 minutes read.

Python Read Excel file

Python Read Excel file Excel is the spreadsheet application for Window, which is developed by Microsoft. The Excel stores data in the tabular form. It provides easy access to analyze and maintain the...

3 minutes read.

Python print() function

Python print() function The print() function prints the specified message to the screen or other standard output devices. Syntax print(*objects,sep=' ',end='\n',file=sys.stdout,flush=False) Parameter objects: This function represents an object, which will be converted to a string...

1 minute read.

Python Network Programming

In network programming, python plays a very important role. Python provides full support for encoding and decoding data and network protocol in its standard library. Writing a network program in...

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

Reverse a Number in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

Python Kwargs Example

In this article, we'll talk about Python's kwargs notion. In Python, kwargs has two stars and passes a variable number of keyworded argument lists to the function, whereas args has...

5 minutes read.

Python Hash Table

An Introduction to Hashing A technique which used to identify a particular object uniquely from a set of similar objects is known as Hashing. Some real-life examples of hashing implementations include: A...

9 minutes read.

Poolmanager in Python

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.

Decision Tree in Python

Decision Tree is one of the most essential algorithms in the area of machine learning for classification and regression. But let us first talk about the lifespan of every machine learning...

12 minutes read.

Android apps for coding in python

Nowadays, it is the generation of smartphones. The world can be seen and acknowledged with a single click on your mobile phone. Everyone uses mobile phones to do any work,...

9 minutes read.

Read JSON File in Python

JSON refers to the JavaScript Object Notation. It is a Data format used for representing structured data and it is used to transfer and store data. In JSON format, the...

5 minutes read.

Palindrome In Python

What is Palindrome? A Palindrome can be defined as the number or a string that resides unchanged when it is reversed. Example: 14341 Output: Yes, this is a Palindrome number Example: RACECAR Output: Yes, this...

2 minutes read.

Python CGI Programming

The Concept of CGI CGI is an abbreviation for Common Gateway Interface. It is not a type of language but a set of rules (specification) that establishes a dynamic interaction between...

9 minutes read.

Pointers in Python

In this tutorial, we will study what pointers are and if they have any utility in python. Now, let us understand what pointers are Pointers Pointers are special variables used to store the...

3 minutes read.

Nested List in Python

The list is an inbuilt sequential data type in Python. It is a very useful and frequently used data type. A list can store any number of data items of...

4 minutes read.

Io stringio Python

Python programming language: 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...

7 minutes read.