0

I have a python script that reads the temp froma ds18b20 sensor and writes it to a sqlite3 db located in the same directory. The section of code that writes to the database looks like this:

def write_DB(devID, devNAME, temp):
    conn = sqlite3.connect('templog.db')
    curs = conn.cursor()

    curs.execute("INSERT INTO temps values((?),(?), datetime('now'), (?))", (devID, devNAME, temp))

    conn.commit()

When run from idle3 it runs fine, but when run from the command line, or cron, this is the output:

 ~/python/HydroMon $ /usr/bin/python /home/pi/python/HydroMon/ds18b20_scanner.py
Traceback (most recent call last):
 File "/home/pi/python/HydroMon/ds18b20_scanner.py", line 102, in <module>
  scan_dev()
 File "/home/pi/python/HydroMon/ds18b20_scanner.py", line 69, in scan_dev
  write_DB(file, dev, temp)
 File "/home/pi/python/HydroMon/ds18b20_scanner.py", line 49, in write_DB
  curs.execute("INSERT INTO temps values((?),(?), datetime('now'), (?))", (devID, devNAME, temp))
sqlite3.OperationalError: unable to open database file

Any idea why this won't run from the command line? All the permissions are correct.

EDIT: I added this to write_db()

global basedir
db_file = basedir + '/templog.db'
conn = sqlite3.connect(db_file)

This no longer produces and error. This is the basedir assignment function to open and read a conf file, which now causes an error. I have tried with python and python3.

#Configuratio Directory
fullpath = sys.argv[0]
basedir = os.path.dirname(fullpath)
confdir = basedir + "/conf"

#Variable to check if scan was good
reading = 0
def get_dev_name(devID):
    global confdir
    devName = 'NONE'
    #open conf file
    conf = confdir + '/devname.conf'
    print(conf)
    cfile = open(conf,mode='r')

When run from idle3 the result is: /home/pi/python/HydroMon/conf/devname.conf

When run from the command line:

~/python/HydroMon $ sudo python3 ds18b20_scanner.py 
/conf/devname.conf
Traceback (most recent call last):
 File "ds18b20_scanner.py", line 105, in <module>
  scan_dev()
 File "ds18b20_scanner.py", line 70, in scan_dev
  dev = get_dev_name(file)
 File "ds18b20_scanner.py", line 27, in get_dev_name
  cfile = open(conf,mode='r')
IOError: [Errno 2] No such file or directory: '/conf/devname.conf'
Edyoucaterself
  • 167
  • 1
  • 1
  • 9
  • 1
    possible duplicate of [Sqlite3, OperationalError: unable to open database file](http://stackoverflow.com/questions/4636970/sqlite3-operationalerror-unable-to-open-database-file) –  Jan 20 '14 at 02:50
  • @LegoStormtroopr: I really don't think this is a duplicate of that. The problem here is simply that the OP is computing an invalid path to the database file. It has nothing to do with sqlite. – Bryan Oakley Jan 20 '14 at 03:40

1 Answers1

0

when you run it from the command line, try printing out sys.argv[0]. You may be surprised to see that it is ds18b20_scanner.py, which makes the dirname an empty string.

Perhaps you want to use __file__ rather than sys.argv[0], if you're trying to write to the same location as the script.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • __file__ did not work for me, but using fullpath = os.path.abspath(sys.argv[0]) basedir = os.path.dirname(fullpath) did the trick. Thanks! – Edyoucaterself Jan 20 '14 at 04:55