Search code examples
pythonbashsqliteraspberry-pi

Python SQLite error only when run from terminal


So I'm using this script to save temp and humidity data on my raspberry pi, the relevant part looks something like this:

import sqlite3 as db

con = db.connect('hygolog.db')
cur = con.cursor()

#This part is commented because I used it when running the first time
#cur.execute("CREATE TABLE measure(time STR PRIMARY KEY, humidity REAL, tempertaure REAL)")
#con.commit()

while True:
    cur.execute(f"""INSERT INTO measure
VALUES(datetime('now','localtime'),{round(humidity,3)},{round(temperature,3)})""")
    con.commit()

And this works just fine when executing the file from the code editor (i checked and data is found in the DB after), but when I execute the file from my terminal I get the following error:

user@raspberrypi:~ $ python Desktop/hygometer.py
Traceback (most recent call last):
  File "/home/user/Desktop/hygometer.py", line 19, in <module>
    cur.execute(f"""INSERT INTO measure
sqlite3.OperationalError: no such table: measure

which is also a weird place to fail, why should it not find the very much existing table when executed through the terminal?


Solution

  • try specifying full path to hygolog.db, for example C:/Files/hygolog.db. When you run this from terminal, you could be running from a different folder, so it creates a new empty hygolog.db in that folder.