How to Insert Double and Float Values in SQLite?
Last Updated :
12 Mar, 2024
SQLite, a lightweight and server-less relational database management system, provides powerful features for handling various data types, including double and float values.
In this article, we will explore various methods along with examples for inserting double and float values into SQLite tables, ensuring data integrity and precision.
How to Insert Double and Float Values?
When working with SQLite, it's important to understand how to insert double and float values correctly. Unlike other RDBMS, SQLite does not have any different data types for double and float. Below are the methods through which we can effectively insert double and float values in tables of SQLite.
- Using REAL Storage class
- Using the TEXT Storage Class
1. Using REAL Storage Class
We can store those values with the REAL storage class. REAL storage class is used to store floating point numbers. SQLite uses 8 bytes (64 bits) to store floating-point numbers. Therefore, we can store both types i.e. single precision and double precision value.
In this, we will see how we can store double and float values in our table in SQLite. We will first create a table namely 'geeksforgeeks' with (id, name, contest_score, and total_score ) as columns.
Query:
--table creation
CREATE TABLE geeksforgeeks (
id INTEGER,
name TEXT,
contest_score REAL,
total_score REAL
);
Explanation: We have created a table 'geeksforgeeks' and set contest_score and total_score as REAL. In those columns, we are going to store float and double values. After executing the above command, we can clearly see a table has been created in our database.
As we have done with creating a table, now lets insert some values and display it.
Query:
--Inserting values in our table
INSERT INTO geeksforgeeks(id, name, contest_score ,total_score)
VALUES(01,'Vishu', 22.5 , 505.623523);
INSERT INTO geeksforgeeks(id, name, contest_score ,total_score)
VALUES(02,'Neeraj', 20.5 , 501.2237562);
INSERT INTO geeksforgeeks(id, name, contest_score ,total_score)
VALUES(03,'Aayush', 22.3 , 495.3534532);
INSERT INTO geeksforgeeks(id, name, contest_score ,total_score)
VALUES(04,'Vivek', 18.2 , 491.10545610123);
--displaying values of our table
SELECT * FROM geeksforgeeks;
Output:
Table - geeksforgeeksExplanation: In the above image, we can clearly observe that float and double values are inserted in contest_score and toal_score columns. We can clearly see that both the columns are created with REAL storage class. Therefore, it can accommodate both values without losing any precision.
2. Using the TEXT Storage Class
We can also use the TEXT storage class to store double and float values. But it cannot be considered as an ideal case. Using the TEXT storage class for floating-point numbers can lead to a loss of precision. It is not recommended due to potential loss of precision. However, if we must store these values as text, ensure proper conversion to avoid data loss.
Query:
CREATE TABLE example_table ( id INTEGER PRIMARY KEY, value TEXT );
INSERT INTO example_table (id, value) VALUES (1, '123.456');
Output:
OutputExplanation: In this example, we create a table with an id column as INTEGER PRIMARY KEY and a value column as TEXT. We then insert a double value (123.456) as a string into the table.
Conclusion
Overall, inserting a double or a float value in a table is basically done with the help of REAL storage class. Unlike other RDBMS, SQLite do not have any separate data types of float and double types of values. We have to use REAL storage class so that we will not loose precision.
Using other class like INTEGER or TEXT will result in the loss of precision of values. The most optimal way to keep our precision is to choose REAL storage class. Now you have a good understanding of inserting float and double values in a table in SQLite. Now you can write queries related to it with ease.
Similar Reads
How to Insert a Value that Contains an Apostrophe in SQLite?
SQLite is a self-contained, serverless and transactional SQL database engine. It is used in many other applications that need a lightweight, embedded database. In this article, we are going to see how we can insert a value that contains an apostrophe in SQLite using various examples and so on Unders
4 min read
How to Set a Column Value to NULL in SQLite?
SQLite is a lightweight and self-contained relational database management system in short RDBMS. Its has a server-less architecture which makes it a better option for small desktop and mobile applications. It also requires very low configuration which eventually helps the developer to integrate it i
4 min read
How to Insert a Value that Contains an Apostrophe in SQL?
SQL is a standard database language used to access and manipulate data in databases. SQL stands for Structured Query Language. SQL was developed by IBM Computer Scientists in the 1970s. By executing queries SQL can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL
3 min read
How to GROUP BY Month and Year in SQLite?
SQLite is a lightweight and server-less relational database management system. It requires very minimal configurations therefore it is easy to integrate into applications. It is also considered as an ideal choice for small mobile and desktop applications. In SQLite, GROUP BY month and year is consid
4 min read
How to list the Tables in a SQLite Database File ?
SQLite is a database engine which is written in C programming language. SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is the most widely deployed SQL database engine in the world. The source code for SQLite is
4 min read
How to Execute a Script in SQLite using Python?
In this article, we are going to see how to execute a script in SQLite using Python. Here we are executing create table and insert records into table scripts through Python. In Python, the sqlite3 module supports SQLite database for storing the data in the database. Approach Step 1: First we need to
2 min read
Decimal vs Double vs Float in MySQL
In MySQL, Decimal, Double, and Float are numeric data types used to represent different precision levels for numerical values. Decimal offers precise numeric storage with fixed-point arithmetic, suitable for financial data. Double and Float are approximate numeric types, where Double provides higher
6 min read
How to convert Float to Int in Python?
Converting a float value to an int is done by Type conversion, which is an explicit method of converting an operand to a specific type. However, it is to be noted that such type of conversion may tend to be a lossy one (loss of data). Converting an int value like 2 to floating-point will result in 2
6 min read
How to Use PL SQL Insert, Update, Delete And Select Statement?
PL/SQL is a powerful extension of SQL specifically designed for Oracle databases. It enables developers to create procedural logic and execute SQL commands within Oracle database environments. In this article, we will explore the usage of fundamental PL/SQL statements such as INSERT, UPDATE, DELETE,
6 min read
How to import CSV file in SQLite database using Python ?
In this article, we'll learn how to import data from a CSV file and store it in a table in the SQLite database using Python. You can download the CSV file from here which contains sample data on the name and age of a few students. Approach: Importing necessary modulesRead data from CSV file DictRead
2 min read