Bart De Wit
Bart De Wit

Reputation: 41

Python - MariaDB result of query is rounded down

When getting data out of my MariaDB database, I lose some precision --> every number is rounded down.

stored data as seen by DBeaver: (see atached picture) or

1737210600  0.0911
1737209700  0.103
1737208800  0.1004
1737207900  0.0697
1737207000  0.0824

my sample code to get the data

import mariadb

mariadb_host = "xxxxxx"
mariadb_db_verbruik = "powerdb"

conn_verbruik = mariadb.connect(
    host=mariadb_host,
    user='xxxxx',
    password='xxxxx',
    database=mariadb_db_verbruik
    )
cursor_verbruik = conn_verbruik.cursor()

sql = f"SELECT time, gridE_tot, 1000*gridE_tot as test from periode15 ORDER BY time DESC LIMIT 5"
cursor_verbruik.execute(sql)
result_verbruik = cursor_verbruik.fetchall()
if (result_verbruik):
    for line in result_verbruik:
        print(line)
        print(1000*line[1])

Result:

(1737210600, 0.0, 91.0)
0.0
(1737209700, 0.0, 103.0)
0.0
(1737208800, 0.0, 100.0)
0.0
(1737207900, 0.0, 69.0)
0.0
(1737207000, 0.0, 82.0)
0.0

why is the result 0.0 instead of 0.0911? (first line)

The data is stored as a double in the mariaDB

Edit in response @Rick James

CREATE TABLE `periode15` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `time` bigint(20) NOT NULL,
  `gridE_tot` double DEFAULT NULL,
  `gridE_tot_neg` double DEFAULT NULL,
  `solar1E_tot` double DEFAULT NULL,
  `solar2E_tot` double DEFAULT NULL,
  `gridE_tot_pos` double DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `time` (`time`)
) ENGINE=InnoDB AUTO_INCREMENT=387908 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

When running the same sql_Select_query in DBeaver, the correct data is shown. Only when running the query via python it is not correct

Upvotes: 4

Views: 60

Answers (0)

Related Questions