[Python] Python - MariaDB result of query is rounded down

Discussão em 'Python' iniciado por Stack, Janeiro 18, 2025.

  1. Stack

    Stack Membro Participativo

    When getting data out of my MariaDB database, I loose 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

    Continue reading...

Compartilhe esta Página