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 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 Install and Use SQLite in Fedora Linux
SQLite is a versatile, lightweight, serverless, and self-contained database engine that finds applications in a variety of settings, ranging from embedded systems to mobile applications and serverless environments. In this article, we will delve into the installation and usage of SQLite on a Fedora
4 min read
How to Specify a Date Format on Creating a Table and Inserting Values in SQL?
Dates are a fundamental part of database management, as they provide a timeline for analyzing data trends, tracking records, and scheduling events. In SQL, handling and formatting date columns correctly ensures consistency and clarity in our database. One critical aspect of working with dates is spe
5 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 Insert a Line Break in a SQLite VARCHAR String
SQLite is a popular relational database management system known for its simplicity and flexibility. However, inserting a line break in a VARCHAR/NVARCHAR string can be challenging due to SQLite's limited support for special characters. In this article, we will explore different approaches to inserti
3 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?
In Python, you can convert a float to an integer using type conversion. This process changes the data type of a value However, such conversions may be lossy, as the decimal part is often discarded. For example: Converting 2.0 (float) to 2 (int) is safe because here, no data is lost.But converting 3.
5 min read
How to Enable SQLite Query Logging?
SQLite is used for its simplicity and flexibility which makes it a popular choice for embedded and small-scale database applications. However, when it comes to debugging, optimizing performance, and auditing, having visibility into the SQL queries executed is important. In this comprehensive guide,
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